Class: ActiveRecord::Blockwhere::WhereProxy

Inherits:
ActiveSupport::BasicObject
Defined in:
lib/active_record/blockwhere/where_proxy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation, context = nil) ⇒ WhereProxy

Returns a new instance of WhereProxy.



14
15
16
17
# File 'lib/active_record/blockwhere/where_proxy.rb', line 14

def initialize(relation, context = nil)
  @relation = relation
  @context  = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_record/blockwhere/where_proxy.rb', line 19

def method_missing(name, *args, &block)
  if @relation.columns_hash.key?(name.to_s)
    return @relation.arel_table[name]
  end
  reflection = @relation.reflections[name]
  if ::ActiveRecord::Reflection::AssociationReflection === reflection
    @relation = @relation.joins(name) unless @relation.joins_values.include?(name)
    return WhereProxy.new(reflection.klass.scoped)
  end
  if @context && @context.respond_to?(name)
    return @context.__send__(name, *args, &block)
  end
  @relation.__send__(name, *args, &block)
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/active_record/blockwhere/where_proxy.rb', line 6

def context
  @context
end

#relationObject (readonly)

Returns the value of attribute relation.



6
7
8
# File 'lib/active_record/blockwhere/where_proxy.rb', line 6

def relation
  @relation
end

Class Method Details

.where(relation, context = nil, &block) ⇒ Object



8
9
10
11
12
# File 'lib/active_record/blockwhere/where_proxy.rb', line 8

def self.where(relation, context = nil, &block)
  proxy = new(relation, context)
  condition = proxy.instance_eval(&block)
  condition ? proxy.relation.where(condition) : proxy.relation
end