Class: NaturalResource::Policy
- Inherits:
-
Object
- Object
- NaturalResource::Policy
- Includes:
- PolicyPreconditions
- Defined in:
- lib/natural_resource/policy.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Scope
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #anon? ⇒ Boolean
- #create? ⇒ Boolean
- #destroy? ⇒ Boolean
- #edit? ⇒ Boolean
- #errors ⇒ Object
- #failure_status ⇒ Object
- #index? ⇒ Boolean
-
#initialize(user, record) ⇒ Policy
constructor
A new instance of Policy.
- #new? ⇒ Boolean
- #own_record? ⇒ Boolean
- #scope ⇒ Object
- #show? ⇒ Boolean
- #update? ⇒ Boolean
Constructor Details
#initialize(user, record) ⇒ Policy
Returns a new instance of Policy.
10 11 12 13 14 15 |
# File 'lib/natural_resource/policy.rb', line 10 def initialize(user, record) @user = user @record = record @errors = Hash.new { |h,k| h[k]= Set.new([]) } @failure_status = nil end |
Instance Attribute Details
#record ⇒ Object (readonly)
Returns the value of attribute record.
8 9 10 |
# File 'lib/natural_resource/policy.rb', line 8 def record @record end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
8 9 10 |
# File 'lib/natural_resource/policy.rb', line 8 def user @user end |
Instance Method Details
#anon? ⇒ Boolean
61 62 63 |
# File 'lib/natural_resource/policy.rb', line 61 def anon? user.nil? end |
#create? ⇒ Boolean
37 38 39 |
# File 'lib/natural_resource/policy.rb', line 37 def create? false end |
#destroy? ⇒ Boolean
53 54 55 |
# File 'lib/natural_resource/policy.rb', line 53 def destroy? false end |
#edit? ⇒ Boolean
49 50 51 |
# File 'lib/natural_resource/policy.rb', line 49 def edit? update? end |
#errors ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/natural_resource/policy.rb', line 21 def errors if @errors.keys.empty? {user: ['does not have permission to perform this action']} else @errors end end |
#failure_status ⇒ Object
17 18 19 |
# File 'lib/natural_resource/policy.rb', line 17 def failure_status @failure_status.presence || :unauthorized end |
#index? ⇒ Boolean
29 30 31 |
# File 'lib/natural_resource/policy.rb', line 29 def index? false end |
#new? ⇒ Boolean
41 42 43 |
# File 'lib/natural_resource/policy.rb', line 41 def new? create? end |
#own_record? ⇒ Boolean
57 58 59 |
# File 'lib/natural_resource/policy.rb', line 57 def own_record? user && record.user == user end |
#scope ⇒ Object
65 66 67 |
# File 'lib/natural_resource/policy.rb', line 65 def scope Pundit.policy_scope!(user, record.class) end |
#show? ⇒ Boolean
33 34 35 |
# File 'lib/natural_resource/policy.rb', line 33 def show? scope.where(id: record.id).exists? end |
#update? ⇒ Boolean
45 46 47 |
# File 'lib/natural_resource/policy.rb', line 45 def update? false end |