Class: Trailblazer::Operation::Policy::Permission

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/operation/policy.rb

Overview

Encapsulate building the Policy object and calling the defined query action. This assumes the policy class is “pundit-style”, as in Policy.new(user, model).edit?.

Instance Method Summary collapse

Constructor Details

#initialize(policy_class, action) ⇒ Permission

Returns a new instance of Permission.



56
57
58
# File 'lib/trailblazer/operation/policy.rb', line 56

def initialize(policy_class, action)
  @policy_class, @action = policy_class, action
end

Instance Method Details

#call(user, model, external_policy = nil) ⇒ Object

Without a block, return the policy object (which is usually a Pundit-style class). When block is passed evaluate the default rule and run block when false.



62
63
64
65
66
# File 'lib/trailblazer/operation/policy.rb', line 62

def call(user, model, external_policy=nil)
  build_policy(user, model, external_policy).tap do |policy|
    policy.send(@action) || yield(policy, @action) if block_given?
  end
end