Class: AssociationScope::Scope::ThroughReflection

Inherits:
AssociationScope::Scope show all
Defined in:
lib/association_scope/scope/through_reflection.rb

Instance Attribute Summary

Attributes inherited from AssociationScope::Scope

#association, #model

Instance Method Summary collapse

Methods inherited from AssociationScope::Scope

#initialize, inject_scopes

Constructor Details

This class inherits a constructor from AssociationScope::Scope

Instance Method Details

#applyObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/association_scope/scope/through_reflection.rb', line 6

def apply
  association = @association
  class_name = reflection_details.options[:class_name]&.constantize || association.singularize.camelize.constantize
  first_join = inverse_reflection(class_name)&.options&.fetch(:through, nil) || inverse_reflection(class_name)&.options&.fetch(:source, nil)
  second_join = compute_second_join class_name

  raise AssociationMissingError.new missing_in: class_name, association: inverse unless inverse_reflection(class_name)

  model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    scope association.pluralize, -> do
      class_name
        .joins(first_join => second_join)
        .distinct
    end
  RUBY
end