class Ardm::Query::Expression

Attributes

operator[R]
relation[R]
target[R]
value[R]

Public Class Methods

build_scope(relation, target, value) click to toggle source
# File lib/ardm/query/expression.rb, line 6
def self.build_scope(relation, target, value)
  new(relation, target, value).scope
end
new(relation, target, operator, value) click to toggle source
# File lib/ardm/query/expression.rb, line 10
def initialize(relation, target, operator, value)
  @relation   = relation
  @value      = value
  @target     = target
  @operator   = operator
end

Public Instance Methods

arel_target() click to toggle source
# File lib/ardm/query/expression.rb, line 21
def arel_target
  arel_table[resolved_target]
end
resolved_target() click to toggle source
# File lib/ardm/query/expression.rb, line 17
def resolved_target
  target_from_association || target
end
scope() click to toggle source
# File lib/ardm/query/expression.rb, line 29
def scope
  relation.where to_arel
end
to_arel() click to toggle source
# File lib/ardm/query/expression.rb, line 25
def to_arel
  arel_target.send(arel_operator, arel_value)
end

Private Instance Methods

arel_operator() click to toggle source
# File lib/ardm/query/expression.rb, line 53
def arel_operator
  value.respond_to?(:to_ary) ? operator.for_array : operator.operator
end
arel_table() click to toggle source
# File lib/ardm/query/expression.rb, line 35
def arel_table
  relation.arel_table
end
arel_value(val = value) click to toggle source
# File lib/ardm/query/expression.rb, line 57
def arel_value(val = value)
  if val.respond_to?(:to_ary)
    return val.map {|v| arel_value(v) }
  end

  case val
  when ::ActiveRecord::Base
    val.id
  when ::ActiveRecord::Relation
    arel_value(val.to_ary)
  when ::Array
    val.map {|v| arel_value(v) }
  else
    val
  end
end
association() click to toggle source
# File lib/ardm/query/expression.rb, line 39
def association
  @association ||= relation.reflect_on_association(target)
end
target_from_association() click to toggle source
# File lib/ardm/query/expression.rb, line 43
def target_from_association
  if association
    if association.macro == :belongs_to
      association.foreign_key.to_sym
    else
      association.klass.primary_key.to_sym
    end
  end
end