Sha256: 23daa5ab58fbde2bf50163ca0f6441d8125187043d7a6672b85bfa17be56f4eb

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

require 'active_record/associations/builder/association'
require 'active_record/associations/preloader/association'

module ActiveRecord
  module Associations
    module Builder
      class Association #:nodoc:
        self.extensions << ActiveRecordAssociationQueryEconomizer::AssociationExtension.new
      end
    end

    class Preloader
      class Association #:nodoc:
        def preload_filters
          [options[:preload_if]].flatten.compact
        end

        def owners_filtered
          unless defined?(@owners_filtered)
            @owners_filtered = owners.dup
            preload_filters.each do |filter|
              case filter
              when Proc
                @owners_filtered.select!(&filter)
              when Symbol
                @owners_filtered.select! { |record| record.send(filter) }
              end
            end
          end
          @owners_filtered
        end

        remove_method :owner_keys
        def owner_keys
          unless defined?(@owner_keys)
            @owner_keys = owners_filtered.map do |owner|
              owner[owner_key_name]
            end
            @owner_keys.uniq!
            @owner_keys.compact!
          end
          @owner_keys
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_record_association_query_economizer-0.1.5 lib/active_record_association_query_economizer/rails5/associations.rb
active_record_association_query_economizer-0.1.4 lib/active_record_association_query_economizer/rails5/associations.rb
active_record_association_query_economizer-0.1.3 lib/active_record_association_query_economizer/rails5/associations.rb
active_record_association_query_economizer-0.1.2 lib/active_record_association_query_economizer/rails5/associations.rb