Module: HasDynamicColumns::ActiveRecord::QueryMethods
- Defined in:
- lib/has_dynamic_columns/active_record/query_methods.rb
Defined Under Namespace
Modules: WhereChainCompatibility
Class Method Summary collapse
Instance Method Summary collapse
-
#build_arel_with_dynamic_columns ⇒ Object
When arel starts building - filter.
- #where_with_dynamic_columns(opts = :chain, *rest) ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 4 def self.included(base) base.class_eval do alias_method_chain :where, :dynamic_columns alias_method_chain :build_arel, :dynamic_columns end base.instance_eval do def has_dynamic_column_values=a end def has_dynamic_column_values end end end |
Instance Method Details
#build_arel_with_dynamic_columns ⇒ Object
When arel starts building - filter
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 20 def build_arel_with_dynamic_columns #arel = Arel::SelectManager.new(table.engine, table) # Calculate any dynamic scope that was passed self.has_dynamic_columns_values.each { |dynamic_scope| field_scope = dynamic_scope[:scope] field_scope_id = (!field_scope.nil?) ? field_scope.id : nil field_scope_type = (!field_scope.nil?) ? field_scope.class.name.constantize.to_s : nil # TODO - make this work on compound arel queries like: table[:last_name].eq("Paterson").or(table[:first_name].eq("John")) #collapsed = collapse_wheres(arel, dynamic_scope[:where]) dynamic_scope[:where].each_with_index { |rel, index| case rel when String next else dynamic_type = rel.left.relation.engine.to_s col_name = rel.left.name value = rel.right end column_table = HasDynamicColumns::DynamicColumn.arel_table.alias("dynamic_where_#{index}_#{col_name}") column_datum_table = HasDynamicColumns::DynamicColumnDatum.arel_table.alias("dynamic_where_data_#{index}_#{col_name}") # Join on the column with the key on_query = column_table[:key].eq(col_name) on_query = on_query.and( column_table[:field_scope_type].eq(field_scope_type) ) unless field_scope_type.nil? on_query = on_query.and( column_table[:field_scope_id].eq(field_scope_id) ) unless field_scope_id.nil? column_table_join_on = column_table .create_on( on_query ) column_table_join = table.create_join(column_table, column_table_join_on) self.joins_values += [column_table_join] arel_node = case rel when Arel::Nodes::Equality column_datum_table[:value].eq(value) else column_datum_table[:value].matches(value) end # Join on all the data with the provided key column_table_datum_join_on = column_datum_table .create_on( column_datum_table[:owner_id].eq(table[:id]).and( column_datum_table[:owner_type].eq(dynamic_type) ).and( column_datum_table[:dynamic_column_id].eq(column_table[:id]) ).and( arel_node ) ) column_table_datum_join = table.create_join(column_datum_table, column_table_datum_join_on) self.joins_values += [column_table_datum_join] } } build_arel_without_dynamic_columns end |
#where_with_dynamic_columns(opts = :chain, *rest) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/has_dynamic_columns/active_record/query_methods.rb', line 128 def where_with_dynamic_columns(opts = :chain, *rest) if opts == :chain scope = spawn chain = ::ActiveRecord::QueryMethods::WhereChain.new(scope) chain.send(:extend, WhereChainCompatibility) else where_without_dynamic_columns(opts, rest) end end |