Module: HasDynamicColumns::ActiveRecord::QueryMethods::WhereChainCompatibility

Defined in:
lib/has_dynamic_columns/active_record/v4/query_methods.rb

Overview

Instance Method Summary collapse

Instance Method Details

#has_dynamic_columns(opts = :chain, *rest) ⇒ Object

Extends where to chain a has_dynamic_columns method This builds all the joins needed to search the has_dynamic_columns_data tables



18
19
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
# File 'lib/has_dynamic_columns/active_record/v4/query_methods.rb', line 18

def has_dynamic_columns(opts = :chain, *rest)
  # Map
  dynamic_columns_value = {
    :scope => nil,
    :where => @scope.send(:build_where, opts, rest)
  }
  @scope.where_dynamic_columns_values = dynamic_columns_value

  chain = ::ActiveRecord::QueryMethods::WhereChain.new(@scope)
  chain.instance_eval do
    # Make outer scope variable accessible
    @dynamic_columns_value = dynamic_columns_value

    # Extends where to chain with a has_scope method
    # This scopes the where from above
    def with_scope(opt)
      @dynamic_columns_value[:scope] = opt

      @scope
    end
    def without_scope
      @scope
    end
  end

  chain
end