Sha256: dc5769ae2585ab957d8919b849e5e8cf94857256c7281a3ae6d2ce18f769669b

Contents?: true

Size: 976 Bytes

Versions: 2

Compression:

Stored size: 976 Bytes

Contents

# frozen_string_literal: true

module ActiveRecordDataLoader
  module ActiveRecord
    class BelongsToDataProvider
      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

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

      def foreign_key_func
        -> { possible_values.next }
      end

      private

      def possible_values
        @possible_values ||= List.for(base_query.pluck(@ar_association.join_primary_key), strategy: @strategy)
      end

      def base_query
        if @query.respond_to?(:call)
          @query.call.all
        else
          @ar_association.klass.all
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_record_data_loader-1.3.1 lib/active_record_data_loader/active_record/belongs_to_data_provider.rb
active_record_data_loader-1.3.0 lib/active_record_data_loader/active_record/belongs_to_data_provider.rb