Module: Polyamorous::JoinDependencyExtensions

Defined in:
lib/brick.rb

Instance Method Summary collapse

Instance Method Details

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



1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
# File 'lib/brick.rb', line 1965

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