Class: Squeel::Adapters::ActiveRecord::Context

Inherits:
Context
  • Object
show all
Defined in:
lib/squeel/adapters/active_record/context.rb,
lib/squeel/adapters/active_record/3.0/context.rb

Constant Summary collapse

JoinPart =

Because the AR::Associations namespace is insane

::ActiveRecord::Associations::JoinDependency::JoinPart
JoinBase =

Because the AR::Associations namespace is insane

::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase

Instance Attribute Summary

Attributes inherited from Context

#arel_visitor, #base, #engine

Instance Method Summary collapse

Methods inherited from Context

#contextualize

Constructor Details

#initialize(object) ⇒ Context

Returns a new instance of Context.



10
11
12
13
# File 'lib/squeel/adapters/active_record/context.rb', line 10

def initialize(object)
  @base = object.join_base
  super
end

Instance Method Details

#find(object, parent = @base) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/squeel/adapters/active_record/context.rb', line 15

def find(object, parent = @base)
  if JoinPart === parent
    object = object.to_sym if String === object
    case object
    when Symbol, Nodes::Stub
      @object.join_associations.detect { |j|
        j.reflection.name == object.to_sym && j.parent == parent
      }
    when Nodes::Join
      @object.join_associations.detect { |j|
        j.reflection.name == object.name && j.parent == parent &&
        (object.polymorphic? ? j.reflection.klass == object.klass : true)
      }
    else
      @object.join_associations.detect { |j|
        j.reflection == object && j.parent == parent
      }
    end
  end
end

#sanitize_sql(conditions, parent) ⇒ Object



46
47
48
# File 'lib/squeel/adapters/active_record/context.rb', line 46

def sanitize_sql(conditions, parent)
  parent.active_record.send(:sanitize_sql, conditions, parent.aliased_table_name)
end

#traverse(keypath, parent = @base, include_endpoint = false) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/squeel/adapters/active_record/context.rb', line 36

def traverse(keypath, parent = @base, include_endpoint = false)
  parent = @base if keypath.absolute?
  keypath.path.each do |key|
    parent = find(key, parent) || key
  end
  parent = find(keypath.endpoint, parent) if include_endpoint

  parent
end