Module: ObjectidColumns::Arel::Visitors::ToSql
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/objectid_columns/arel/visitors/to_sql.rb
Overview
This module gets mixed into Arel::Visitors::ToSql, which is the class that the Arel gem (which is really the backbone of ActiveRecord’s query language) uses to generate SQL. This teaches Arel what to do when it bumps into an object of a BSON ID class – i.e., how to convert it to a SQL literal.
How this works depends on which version of ActiveRecord – and therefore AREL – you’re using:
-
In Arel 4.x, the #visit… methods get called with two arguments. The first is the actual BSON ID that needs to be converted; the second provides context. From the second parameter, we can get the table name and column name. We use this to get a hold of the ObjectidColumnsManager via its class method .for_table, and, from there, a converted, valid value for the column in question (whether hex or binary).
-
In Arel 2.x (AR 3.0.x) and 3.x, we have to monkeypatch the #visit_Arel_Attributes_Attribute method – it already picks up and stashes away the .last_column, but we need to add the .last_relation, too.
Instance Method Summary collapse
- #visit_Arel_Attributes_Attribute_with_objectid_columns(o, *args) ⇒ Object
- #visit_BSON_ObjectId(o, a = nil) ⇒ Object (also: #visit_Moped_BSON_ObjectId)
Instance Method Details
#visit_Arel_Attributes_Attribute_with_objectid_columns(o, *args) ⇒ Object
24 25 26 27 28 |
# File 'lib/objectid_columns/arel/visitors/to_sql.rb', line 24 def visit_Arel_Attributes_Attribute_with_objectid_columns(o, *args) out = visit_Arel_Attributes_Attribute_without_objectid_columns(o, *args) self.last_relation = o.relation out end |
#visit_BSON_ObjectId(o, a = nil) ⇒ Object Also known as: visit_Moped_BSON_ObjectId
44 45 46 47 48 49 50 51 |
# File 'lib/objectid_columns/arel/visitors/to_sql.rb', line 44 def visit_BSON_ObjectId(o, a = nil) column = if a then column_for(a) else last_column end relation = if a then a.relation else last_relation end return quote(o.to_s) unless column && relation quote(bson_objectid_value_from_parameter(o, column, relation), column) end |