Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/polyamorous/join_dependency.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/polyamorous/join_dependency.rb', line 4

def self.included(base)
  base.class_eval do
    alias_method_chain :build, :polymorphism
    alias_method_chain :graft, :polymorphism
    if base.method_defined?(:active_record)
      alias_method :base_klass, :active_record
    end
  end
end

Instance Method Details

#_join_partsObject



29
30
31
# File 'lib/polyamorous/join_dependency.rb', line 29

def _join_parts
  @joins
end

#build_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/polyamorous/join_dependency.rb', line 69

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_polymorphism(associations, parent = nil, join_type = Arel::InnerJoin) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/polyamorous/join_dependency.rb', line 38

def build_with_polymorphism(associations, parent = nil, join_type = Arel::InnerJoin)
  case associations
  when 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
  else
    build_without_polymorphism(associations, parent, join_type)
  end
end

#find_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/polyamorous/join_dependency.rb', line 59

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.base_klass == klass
    end
  end
end

#graft_with_polymorphism(*associations) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/polyamorous/join_dependency.rb', line 14

def graft_with_polymorphism(*associations)
  associations.each do |association|
    unless join_associations.detect {|a| association == a}
      if association.reflection.options[:polymorphic]
        build(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