Sha256: a61c284e50be6110fb8eb3387f884c9f01b6d4e7b0d768d47d5d729ecd48dfde

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

module Protector
  module Adapters
    module ActiveRecord
      # Patches `ActiveRecord::Associations::Preloader`
      module Preloader extend ActiveSupport::Concern

        # Patches `ActiveRecord::Associations::Preloader::Association`
        module Association extend ActiveSupport::Concern
          included do
            # AR 4 has renamed `scoped` to `scope`
            if method_defined?(:scope)
              alias_method_chain :scope, :protector
            else
              alias_method "scope_without_protector", "scoped"
              alias_method "scoped", "scope_with_protector"
            end
          end

          # Gets current subject of preloading association
          def protector_subject
            # Owners are always loaded from the single source
            # having same protector_subject
            owners.first.protector_subject
          end

          def protector_subject?
            owners.first.protector_subject?
          end

          # Restricts preloading association scope with subject of the owner
          def scope_with_protector(*args)
            return scope_without_protector unless protector_subject?

            @meta ||= klass.protector_meta.evaluate(protector_subject)

            scope_without_protector.merge(@meta.relation)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
protector-0.6.4 lib/protector/adapters/active_record/preloader.rb
protector-0.6.3 lib/protector/adapters/active_record/preloader.rb
protector-0.6.2 lib/protector/adapters/active_record/preloader.rb
protector-0.6.1 lib/protector/adapters/active_record/preloader.rb
protector-0.6.0 lib/protector/adapters/active_record/preloader.rb
protector-0.6.0.beta.1 lib/protector/adapters/active_record/preloader.rb
protector-0.5.5 lib/protector/adapters/active_record/preloader.rb
protector-0.5.4 lib/protector/adapters/active_record/preloader.rb
protector-0.5.3 lib/protector/adapters/active_record/preloader.rb