Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/polyamorous/activerecord_4.1/join_dependency.rb,
lib/polyamorous/activerecord_3_and_4.0/join_dependency.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/polyamorous/activerecord_4.1/join_dependency.rb', line 3

def self.included(base)
  base.extend ClassMethods

  base.class_eval do
    class << self
      alias_method_chain :walk_tree, :polymorphism
    end

    alias_method_chain :build, :polymorphism
    if base.method_defined?(:active_record)
      alias_method :base_klass, :active_record
    end
  end
end

Instance Method Details

#_join_partsObject



39
40
41
# File 'lib/polyamorous/activerecord_3_and_4.0/join_dependency.rb', line 39

def _join_parts
  @joins
end

#build_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/polyamorous/activerecord_4.1/join_dependency.rb', line 52

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

#build_with_polymorphism(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
# File 'lib/polyamorous/activerecord_4.1/join_dependency.rb', line 18

def build_with_polymorphism(associations, base_klass)
  associations.map do |name, right|
    if name.is_a? Join
      reflection = find_reflection base_klass, name.name
      reflection.check_validity!

      if reflection.options[:polymorphic]
        JoinAssociation.new reflection, build(right, name.klass || base_klass), name.klass
      else
        JoinAssociation.new reflection, build(right, reflection.klass), name.klass
      end
    else
      reflection = find_reflection base_klass, name
      reflection.check_validity!

      if reflection.options[:polymorphic]
        raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
      end

      JoinAssociation.new reflection, build(right, reflection.klass)
    end
  end
end

#find_join_association_respecting_polymorphism(reflection, parent, klass) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/polyamorous/activerecord_4.1/join_dependency.rb', line 42

def find_join_association_respecting_polymorphism(reflection, parent, klass)
  if association = parent.children.find { |j| j.reflection == reflection }
    unless reflection.options[:polymorphic]
      association
    else
      association if association.base_klass == klass
    end
  end
end

#graft_with_polymorphism(*associations) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/polyamorous/activerecord_3_and_4.0/join_dependency.rb', line 13

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