Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/brick.rb

Instance Method Summary collapse

Instance Method Details

#build(associations, base_klass, root = nil, path = '') ⇒ Object



1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
# File 'lib/brick.rb', line 1809

def build(associations, base_klass, root = nil, path = '')
  root ||= associations
  # puts associations.map(&:first)

  associations.map do |name, right|
    link_path = path.blank? ? name.to_s : path + ".#{name}"
    ja = if name.is_a? ::Polyamorous::Join
           reflection = find_reflection base_klass, name.name
           reflection.check_validity!
           reflection.check_eager_loadable!

           klass = if reflection.polymorphic?
                     name.klass || base_klass
                   else
                     reflection.klass
                   end
           ::ActiveRecord::Associations::JoinDependency::JoinAssociation.new(
             reflection, build(right, klass, root, link_path), name.klass, name.type
           )
         else
           reflection = find_reflection base_klass, name
           reflection.check_validity!
           reflection.check_eager_loadable!

           if reflection.polymorphic?
             raise ActiveRecord::EagerLoadPolymorphicError.new(reflection)
           end
           ::ActiveRecord::Associations::JoinDependency::JoinAssociation.new(
             reflection, build(right, reflection.klass, root, link_path)
           )
         end
    ja.instance_variable_set(:@link_path, link_path) # Make note on the JoinAssociation of its AR path
    ja.instance_variable_set(:@assocs, root)
    ja
  end
end