Sha256: a09853709c738233f17830bce08becadb5e6b9d29601149c6829fe31ed862837

Contents?: true

Size: 965 Bytes

Versions: 11

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

require 'active_model/associations/hooks'
require 'activemodel/associations'

module EacRailsUtils
  module Patches
    module ActiveModelAssociations
      module ScopeExtensionPatch
        def add_constraints(scope, owner, association_klass, *extra_args)
          if extra_args.any?
            refl = extra_args.first
            if refl.options[:active_model]
              target_ids = refl.options[:target_ids]
              return scope.where(id: owner[target_ids])
            end
          end

          super
        end
      end
    end
  end
end

module ActiveModel
  module Associations
    module Hooks
      def self.init
        return unless ::Rails.version < '5'

        ActiveSupport.on_load(:active_record) do
          ActiveRecord::Associations::AssociationScope.prepend(
            ::EacRailsUtils::Patches::ActiveModelAssociations::ScopeExtensionPatch
          )
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
eac_rails_utils-0.13.3 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.13.2 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.13.1 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.13.0 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.12.3 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.12.2 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.12.1 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.12.0 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.11.6 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.11.5 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.11.4 lib/eac_rails_utils/patches/active_model_associations.rb