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

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/1.1/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.



53
54
55
# File 'lib/trailblazer/1.1/operation/policy.rb', line 53

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.



59
60
61
62
63
64
# File 'lib/trailblazer/1.1/operation/policy.rb', line 59

def call(user, model, external_policy=nil)
  policy = build_policy(user, model, external_policy)

  policy.send(@action) || yield(policy, @action) if block_given?
  policy
end