Class: Sipity::Policies::BasePolicy

Inherits:
Object
  • Object
show all
Defined in:
app/policies/sipity/policies/base_policy.rb

Overview

At its core a policy must implement the following API

  • .call(user:, entity:, policy_question:)

  • #initialize(user, entity)

Direct Known Subclasses

EnrichSipByFormSubmissionPolicy, SipPolicy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, entity) ⇒ BasePolicy

Returns a new instance of BasePolicy


33
34
35
36
# File 'app/policies/sipity/policies/base_policy.rb', line 33

def initialize(user, entity)
  self.user = user
  self.entity = entity
end

Instance Attribute Details

#entityObject (private)

Returns the value of attribute entity


37
38
39
# File 'app/policies/sipity/policies/base_policy.rb', line 37

def entity
  @entity
end

#userObject (private)

Returns the value of attribute user


37
38
39
# File 'app/policies/sipity/policies/base_policy.rb', line 37

def user
  @user
end

Class Method Details

.call(user:, entity:, policy_question:) ⇒ Boolean

Exposed as a convenience method and the public interface into the Policy subsystem.

Parameters:

  • user (User)
  • entity (#persisted?)
  • policy_question (Symbol)

    In the general case this will be :show?, :create?, :update?, or :destroy?; However in other cases that may not be the correct answer.

Returns:

  • (Boolean)

    If the user can take the action, then return true. otherwise return false.


19
20
21
# File 'app/policies/sipity/policies/base_policy.rb', line 19

def self.call(user:, entity:, policy_question:)
  new(user, entity).public_send(policy_question)
end

.define_policy_question(method_name, &block) ⇒ Object (private)


27
28
29
30
# File 'app/policies/sipity/policies/base_policy.rb', line 27

def self.define_policy_question(method_name, &block)
  self.registered_policy_questions += [method_name]
  define_method(method_name, &block)
end

Instance Method Details

#available_actions_by_policyObject


40
41
42
43
44
45
# File 'app/policies/sipity/policies/base_policy.rb', line 40

def available_actions_by_policy
  registered_policy_questions.each_with_object([]) do |question, mem|
    mem << question if public_send(question)
    mem
  end
end

#registered_policy_questionsObject (private)

#registered_policy_questions?Object (private)