Sha256: d12177ddaf0a712eefbb9cceb49f11158d05e938cc2012b745f77d4aa8ffae0b

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module IIPolicy
  module Core
    extend ActiveSupport::Concern
    include Coactive::Initializer

    included do
      self.context_class = IIPolicy::Context
      context :user, :item
      attr_reader :_result
    end

    def initialize(args = {})
      super
    end

    def call_all(action)
      planned = case IIPolicy.config.traversal
        when :preorder
          [self] + coactors
        when :postorder
          coactors + [self]
        when :inorder
          planned = coactors.in_groups(2, false)
          planned[0] + [self] + planned[1]
        end

      planned.each do |policy|
        result = policy == self ? call(action) : policy.new(@context).call_all(action)
        return false unless result
      end

      return true
    end

    def call(action)
      if respond_to?(action) && !send(action)
        @_result = false
      else
        @_result = true
      end
    end

    def allowed(action)
      call_all(action)
    end

    def policy(item)
      context = self.class.context_class.new(@context.to_h.dup.merge(item: item))
      self.class.lookup(item).new(context)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ii_policy-2.3.0 lib/ii_policy/core.rb
ii_policy-2.2.2 lib/ii_policy/core.rb
ii_policy-2.2.1 lib/ii_policy/core.rb
ii_policy-2.2.0 lib/ii_policy/core.rb
ii_policy-2.1.0 lib/ii_policy/core.rb