Class: ActiveRecordDataLoader::ActiveRecord::BelongsToDataProvider

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ar_association, query, strategy) ⇒ BelongsToDataProvider

Returns a new instance of BelongsToDataProvider.



12
13
14
15
16
# File 'lib/active_record_data_loader/active_record/belongs_to_data_provider.rb', line 12

def initialize(ar_association, query, strategy)
  @ar_association = ar_association
  @query = query
  @strategy = strategy
end

Class Method Details

.provider_for(ar_association:, query: nil, strategy: :random) ⇒ Object



6
7
8
9
10
# File 'lib/active_record_data_loader/active_record/belongs_to_data_provider.rb', line 6

def self.provider_for(ar_association:, query: nil, strategy: :random)
  raise "#{name} does not support polymorphic associations" if ar_association.polymorphic?

  { ar_association.join_foreign_key.to_sym => new(ar_association, query, strategy).foreign_key_func }
end

Instance Method Details

#foreign_key_funcObject



18
19
20
# File 'lib/active_record_data_loader/active_record/belongs_to_data_provider.rb', line 18

def foreign_key_func
  -> { possible_values.next }
end