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

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.



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

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

Instance Method Details

#classify(object) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/squeel/adapters/active_record/context.rb', line 47

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

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



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

def find(object, parent = @base)
  if JoinPart === parent
    case object
    when String, Symbol, Nodes::Stub
      assoc_name = object.to_s
      @object.join_associations.detect { |j|
        j.reflection.name.to_s == assoc_name && 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_arel_visitorObject (private)



71
72
73
# File 'lib/squeel/adapters/active_record/context.rb', line 71

def get_arel_visitor
  @engine.connection.visitor
end

#get_table(object) ⇒ Object (private)



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/squeel/adapters/active_record/context.rb', line 59

def get_table(object)
  if [Symbol, String, Nodes::Stub].include?(object.class)
    Arel::Table.new(object.to_s, :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



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

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

  parent
end