Class: AssociationScope::Scope::HasManyReflection

Inherits:
Scope
  • Object
show all
Defined in:
lib/association_scope/scope/has_many_reflection.rb

Instance Method Summary collapse

Instance Method Details

#apply ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/association_scope/scope/has_many_reflection.rb', line 6

def apply
  class_name = reflection_details.klass

  association = @association.pluralize
  column_name = reflection_details.options[:as] || model.to_s.underscore
  inverse_association = inverse_reflection(class_name, column_name)

  raise AssociationMissingError.new(missing_in: class_name, association: column_name) unless inverse_association

  association_scope = reflection_details.scope
  validate_scope!(reflection_details)
  polymorphic = reflection_details.options[:as]
  foreign_key = reflection_details.foreign_key
  owner_key = model.arel_table[reflection_details.active_record_primary_key]
  target_key = class_name.arel_table[foreign_key]
  foreign_type = reflection_details.type if polymorphic
  owner_type = model.polymorphic_name if polymorphic
  singular = reflection_details.macro == :has_one

  model.scope association, -> do
    relation = Scope.target_relation(class_name, association_scope)
      .where(target_key.in(reselect(owner_key).arel))
    relation = relation.where(foreign_type => owner_type) if polymorphic
    singular ? Scope.one_per_owner(relation, target_key) : relation.distinct
  end
end