Sha256: 51f4bc337ac1a4ad68a732ded042fc365d1cf5e1909c8fbfa441b8d984d41164

Contents?: true

Size: 920 Bytes

Versions: 2

Compression:

Stored size: 920 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
        ActiveSupport.on_load(:active_record) do
          ActiveRecord::Associations::AssociationScope.prepend(
            ::EacRailsUtils::Patches::ActiveModelAssociations::ScopeExtensionPatch
          )
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
eac_rails_utils-0.11.3 lib/eac_rails_utils/patches/active_model_associations.rb
eac_rails_utils-0.11.2 lib/eac_rails_utils/patches/active_model_associations.rb