Sha256: 7c07213ad0d2e091a14c221aeb0433716b24ac149aa7a343219a14e2828311a2

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

class Trailblazer::V2_1::Operation
  module Policy
    def self.Pundit(policy_class, action, name: :default)
      Policy.step( Pundit.build(policy_class, action), name: name )
    end

    module Pundit
      def self.build(*args, &block)
        Condition.new(*args, &block)
      end

      # Pundit::Condition is invoked at runtime when iterating the pipe.
      class Condition
        def initialize(policy_class, action)
          @policy_class, @action = policy_class, action
        end

        # Instantiate the actual policy object, and call it.
        def call((options), *)
          policy = build_policy(options)          # this translates to Pundit interface.
          result!(policy.send(@action), policy)
        end

      private
        def build_policy(options)
          @policy_class.new(options[:current_user], options[:model])
        end

        def result!(success, policy)
          data = { "policy" => policy }
          data["message"] = "Breach" if !success # TODO: how to allow messages here?

          Result.new(success, data)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-future-2.1.0.rc1 lib/trailblazer/v2_1/operation/pundit.rb