Class: AssociationScope::Scope::HasManyReflection

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

Direct Known Subclasses

HasOneReflection

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_many_reflection.rb', line 6

def apply
  class_name = reflection_details.options[:class_name]&.constantize || association.singularize.camelize.constantize

  association = @association.pluralize
  column_name = reflection_details.options[:as] || model.to_s.underscore

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

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