Sha256: 0b4d7b1bbd0c04522b85387a76dcaa12e2a65f7a6b7a4360921a618fa37d2f7e
Contents?: true
Size: 1.43 KB
Versions: 6
Compression:
Stored size: 1.43 KB
Contents
module EdgeRider module TraverseAssociation class UnsupportedAssociation < StandardError; end class UnknownAssociation < StandardError; end def traverse_association(*associations) scope = scoped({}) associations.each_with_index do |association, index| reflection = scope.reflect_on_association(association) or raise UnknownAssociation, "Could not find association: #{self.name}##{association}" foreign_key = reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name raise NotImplementedError if reflection.options[:conditions].present? if reflection.macro == :belongs_to # belongs_to ids = scope.collect_column(foreign_key, :distinct => true) scope = EdgeRider::Util.exclusive_query(reflection.klass, :id => ids) elsif reflection.macro == :has_many || reflection.macro == :has_one if reflection.through_reflection # has_many :through scope = scope.traverse_association(reflection.through_reflection.name, reflection.source_reflection.name) else # has_many or has_one ids = scope.collect_ids scope = EdgeRider::Util.exclusive_query(reflection.klass, foreign_key => ids) end else raise UnsupportedAssociation, "Unsupport association type: #{reflection.macro}" end end scope end ActiveRecord::Base.extend(self) end end
Version data entries
6 entries across 6 versions & 1 rubygems