Class: ActiveRecordDataLoader::ActiveRecord::PolymorphicBelongsToDataProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_data_loader/active_record/polymorphic_belongs_to_data_provider.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings, ar_association, strategy) ⇒ PolymorphicBelongsToDataProvider

Returns a new instance of PolymorphicBelongsToDataProvider.



15
16
17
18
19
20
# File 'lib/active_record_data_loader/active_record/polymorphic_belongs_to_data_provider.rb', line 15

def initialize(settings, ar_association, strategy)
  @settings = settings
  @ar_association = ar_association
  @model_count = settings.weighted_models.size
  @strategy = strategy
end

Class Method Details

.provider_for(polymorphic_settings:, strategy: :random) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/active_record_data_loader/active_record/polymorphic_belongs_to_data_provider.rb', line 6

def self.provider_for(polymorphic_settings:, strategy: :random)
  ar_association = polymorphic_settings.model_class.reflect_on_association(
    polymorphic_settings.name
  )
  raise "#{name} only supports polymorphic associations" unless ar_association.polymorphic?

  new(polymorphic_settings, ar_association, strategy).polymorphic_config
end

Instance Method Details

#polymorphic_configObject



22
23
24
25
26
27
# File 'lib/active_record_data_loader/active_record/polymorphic_belongs_to_data_provider.rb', line 22

def polymorphic_config
  {
    @ar_association.foreign_type.to_sym => ->(row_number) { foreign_type(row_number) },
    @ar_association.foreign_key.to_sym => ->(row_number) { foreign_key(row_number) },
  }
end