Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/brick.rb

Instance Method Summary collapse

Instance Method Details

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



1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
# File 'lib/brick.rb', line 1859

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