Class: AssociationScope::Scope::HasAndBelongsToManyReflection

Inherits:
AssociationScope::Scope show all
Defined in:
lib/association_scope/scope/has_and_belongs_to_many_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/has_and_belongs_to_many_reflection.rb', line 6

def apply
  association = @association.pluralize
  class_name = reflection_details.options[:class_name]&.constantize || association.singularize.camelize.constantize
  table_name = model.to_s.underscore.pluralize.to_sym

  raise AssociationMissingError.new(missing_in: class_name, association: table_name) unless class_name.reflections.has_key?(table_name.to_s)

  model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    scope association, -> do
      class_name
        .joins(table_name)
        .where(table_name => self)
        .distinct
    end
  RUBY
end