Module: Pundit
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/pundit.rb,
lib/pundit/rspec.rb,
lib/pundit/version.rb,
lib/pundit/policy_finder.rb,
lib/generators/pundit/policy/policy_generator.rb,
lib/generators/pundit/install/install_generator.rb
Defined Under Namespace
Modules: RSpec Classes: AuthorizationNotPerformedError, InvalidConstructorError, NotAuthorizedError, NotDefinedError, PolicyFinder, PolicyScopingNotPerformedError
Constant Summary collapse
- SUFFIX =
"Policy".freeze
- VERSION =
"2.0.1".freeze
Class Method Summary collapse
-
.authorize(user, record, query, policy_class: nil) ⇒ Object
Retrieves the policy for the given record, initializing it with the record and user and finally throwing an error if the user is not authorized to perform the given action.
-
.policy(user, record) ⇒ Object?
Retrieves the policy for the given record.
-
.policy!(user, record) ⇒ Object
Retrieves the policy for the given record.
-
.policy_scope(user, scope) ⇒ Scope{#resolve}?
Retrieves the policy scope for the given record.
-
.policy_scope!(user, scope) ⇒ Scope{#resolve}
Retrieves the policy scope for the given record.
Class Method Details
.authorize(user, record, query, policy_class: nil) ⇒ Object
Retrieves the policy for the given record, initializing it with the record and user and finally throwing an error if the user is not authorized to perform the given action.
67 68 69 70 71 72 73 |
# File 'lib/pundit.rb', line 67 def (user, record, query, policy_class: nil) policy = policy_class ? policy_class.new(user, record) : policy!(user, record) raise NotAuthorizedError, query: query, record: record, policy: policy unless policy.public_send(query) record end |
.policy(user, record) ⇒ Object?
Retrieves the policy for the given record.
123 124 125 126 127 128 |
# File 'lib/pundit.rb', line 123 def policy(user, record) policy = PolicyFinder.new(record).policy policy.new(user, pundit_model(record)) if policy rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called" end |
.policy!(user, record) ⇒ Object
Retrieves the policy for the given record.
138 139 140 141 142 143 |
# File 'lib/pundit.rb', line 138 def policy!(user, record) policy = PolicyFinder.new(record).policy! policy.new(user, pundit_model(record)) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called" end |
.policy_scope(user, scope) ⇒ Scope{#resolve}?
Retrieves the policy scope for the given record.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pundit.rb', line 82 def policy_scope(user, scope) policy_scope_class = PolicyFinder.new(scope).scope return unless policy_scope_class begin policy_scope = policy_scope_class.new(user, pundit_model(scope)) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy_scope_class}> constructor is called" end policy_scope.resolve end |
.policy_scope!(user, scope) ⇒ Scope{#resolve}
Retrieves the policy scope for the given record.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/pundit.rb', line 103 def policy_scope!(user, scope) policy_scope_class = PolicyFinder.new(scope).scope! return unless policy_scope_class begin policy_scope = policy_scope_class.new(user, pundit_model(scope)) rescue ArgumentError raise InvalidConstructorError, "Invalid #<#{policy_scope_class}> constructor is called" end policy_scope.resolve end |