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
14
15
16
# File 'lib/squeel/adapters/active_record/context.rb', line 10

def initialize(object)
  super
  @base = object.join_base
  @engine = @base.arel_engine
  @arel_visitor = @engine.connection.visitor
  @default_table = Arel::Table.new(@base.table_name, :as => @base.aliased_table_name, :engine => @engine)
end

Instance Method Details

#classify(object) ⇒ Object (private)



63
64
65
66
67
68
69
70
71
# File 'lib/squeel/adapters/active_record/context.rb', line 63

def classify(object)
  if Class === object
    object
  elsif object.respond_to? :active_record
    object.active_record
  else
    raise ArgumentError, "#{object} can't be converted to a class"
  end
end

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



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

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

#get_table(object) ⇒ Object (private)



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/squeel/adapters/active_record/context.rb', line 51

def get_table(object)
  if [Symbol, String, Nodes::Stub].include?(object.class)
    Arel::Table.new(object.to_sym, :engine => @engine)
  elsif Nodes::Join === object
    object._klass ? object._klass.arel_table : Arel::Table.new(object._name, :engine => @engine)
  elsif object.respond_to?(:aliased_table_name)
    Arel::Table.new(object.table_name, :as => object.aliased_table_name, :engine => @engine)
  else
    raise ArgumentError, "Unable to get table for #{object}"
  end
end

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



39
40
41
42
43
44
45
46
47
# File 'lib/squeel/adapters/active_record/context.rb', line 39

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