Module: Squeel::Adapters::ActiveRecord::JoinDependency

Defined in:
lib/squeel/adapters/active_record/join_dependency.rb,
lib/squeel/adapters/active_record/3.0/join_dependency.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.class_eval do
    alias_method_chain :build, :squeel
    alias_method_chain :graft, :squeel
  end
end

Instance Method Details

#build_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 68

def build_join_association_respecting_polymorphism(reflection, parent, klass)
  if reflection.options[:polymorphic] && klass
    JoinAssociation.new(reflection, self, parent, klass)
  else
    JoinAssociation.new(reflection, self, parent)
  end
end

#build_with_squeel(associations, parent = nil, join_type = Arel::InnerJoin) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 29

def build_with_squeel(associations, parent = nil, join_type = Arel::InnerJoin)
  associations = associations.symbol if Nodes::Stub === associations

  case associations
  when Nodes::Join
    parent ||= join_parts.last
    reflection = parent.reflections[associations._name] or
      raise ::ActiveRecord::ConfigurationError, "Association named '#{ associations._name }' was not found; perhaps you misspelled it?"

    unless join_association = find_join_association_respecting_polymorphism(reflection, parent, associations._klass)
      @reflections << reflection
      join_association = build_join_association_respecting_polymorphism(reflection, parent, associations._klass)
      join_association.join_type = associations._type
      @join_parts << join_association
      cache_joined_association(join_association)
    end

    join_association
  when Nodes::KeyPath
    parent ||= join_parts.last
    associations.path_with_endpoint.each do |key|
      parent = build(key, parent, join_type)
    end
    parent
  else
    build_without_squeel(associations, parent, join_type)
  end
end

#find_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 58

def find_join_association_respecting_polymorphism(reflection, parent, klass)
  if association = find_join_association(reflection, parent)
    unless reflection.options[:polymorphic]
      association
    else
      association if association.active_record == klass
    end
  end
end

#graft_with_squeel(*associations) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 15

def graft_with_squeel(*associations)
  associations.each do |association|
    unless join_associations.detect {|a| association == a}
      if association.reflection.options[:polymorphic]
        build(Nodes::Join.new(association.reflection.name, association.join_type, association.reflection.klass),
              association.find_parent_in(self) || join_base, association.join_type)
      else
        build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
      end
    end
  end
  self
end