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

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Yes, I’m using alias_method_chain here. No, I don’t feel too bad about it. JoinDependency, or, to call it by its full proper name, ::ActiveRecord::Associations::JoinDependency, is one of the most “for internal use only” chunks of ActiveRecord.



12
13
14
15
16
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 12

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

Instance Method Details

#build_join_association_respecting_polymorphism(reflection, parent, join) ⇒ Object



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

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

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



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/squeel/adapters/active_record/join_dependency.rb', line 18

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)
      @reflections << reflection
      join_association = build_join_association_respecting_polymorphism(reflection, parent, associations)
      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, join) ⇒ Object



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

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