class ActiveRecord::Blockwhere::WhereProxy

Attributes

context[R]
relation[R]

Public Class Methods

new(relation, context = nil) click to toggle source
# File lib/active_record/blockwhere/where_proxy.rb, line 14
def initialize(relation, context = nil)
  @relation = relation
  @context  = context
end
where(relation, context = nil, &block) click to toggle source
# 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

Public Instance Methods

method_missing(name, *args, &block) click to toggle source
# 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