Sha256: d31bb1dfa1582edff68fc3bad24fb2f7761cd378b14be13eca1b9d909e4aef7b

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

# encoding: UTF-8

module Goldiloader
  module AssociationOptions
    extend self

    OPTIONS = [:auto_include, :fully_load].freeze

    # This is only used in Rails 5+
    module AssociationBuilderExtension
      def self.build(model, reflection)
        # We have no callbacks to register
      end

      def self.valid_options
        OPTIONS
      end
    end

    def register
      if ::ActiveRecord::VERSION::MAJOR >= 5
        ActiveRecord::Associations::Builder::Association.extensions << AssociationBuilderExtension
      elsif ::ActiveRecord::VERSION::MAJOR >= 4
        ActiveRecord::Associations::Builder::Association.valid_options.concat(OPTIONS)
      else
        # Each subclass of CollectionAssociation will have its own copy of valid_options so we need
        # to register the valid option for each one.
        collection_association_classes.each do |assoc_class|
          assoc_class.valid_options.concat(OPTIONS)
        end
      end
    end

    private

    def collection_association_classes
      # Association.descendants doesn't work well with lazy classloading :(
      [
        ActiveRecord::Associations::Builder::Association,
        ActiveRecord::Associations::Builder::BelongsTo,
        ActiveRecord::Associations::Builder::HasAndBelongsToMany,
        ActiveRecord::Associations::Builder::HasMany,
        ActiveRecord::Associations::Builder::HasOne,
        ActiveRecord::Associations::Builder::SingularAssociation
      ]
    end
  end
end

Goldiloader::AssociationOptions.register

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
goldiloader-1.0.1 lib/goldiloader/association_options.rb
goldiloader-1.0.0 lib/goldiloader/association_options.rb
goldiloader-0.0.12 lib/goldiloader/association_options.rb
goldiloader-0.0.11 lib/goldiloader/association_options.rb
goldiloader-0.0.10 lib/goldiloader/association_options.rb