Class: ActiveModel::Policy
- Inherits:
-
Object
- Object
- ActiveModel::Policy
- Defined in:
- lib/active_model/policy.rb,
lib/active_model/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#model ⇒ Object
(also: #to_model)
readonly
Returns the value of attribute model.
Class Method Summary collapse
Instance Method Summary collapse
- #can!(action, *args) ⇒ Object
- #can?(action, *args) ⇒ Boolean
- #cannot!(action, *args) ⇒ Object
- #cannot?(*args) ⇒ Boolean
-
#initialize(model) ⇒ Policy
constructor
A new instance of Policy.
- #to_policy ⇒ Object
Constructor Details
#initialize(model) ⇒ Policy
Returns a new instance of Policy.
5 6 7 |
# File 'lib/active_model/policy.rb', line 5 def initialize model @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly) Also known as: to_model
Returns the value of attribute model.
3 4 5 |
# File 'lib/active_model/policy.rb', line 3 def model @model end |
Class Method Details
.can(*actions, &block) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/active_model/policy.rb', line 32 def self.can *actions, &block raise ArgumentError if actions.empty? actions.flatten.each do |action| define_method "can_#{action}?", &block end end |
Instance Method Details
#can!(action, *args) ⇒ Object
20 21 22 |
# File 'lib/active_model/policy.rb', line 20 def can! action, *args can?(action, *args) || raise(ActiveModel::ActionNotAllowed, action) end |
#can?(action, *args) ⇒ Boolean
15 16 17 18 |
# File 'lib/active_model/policy.rb', line 15 def can? action, *args method = "can_#{action}?" respond_to?(method) && !!safe_send(method, args) end |
#cannot!(action, *args) ⇒ Object
28 29 30 |
# File 'lib/active_model/policy.rb', line 28 def cannot! action, *args cannot?(action, *args) || raise(ActiveModel::ActionUnexpected, action) end |
#cannot?(*args) ⇒ Boolean
24 25 26 |
# File 'lib/active_model/policy.rb', line 24 def cannot? *args !can? *args end |
#to_policy ⇒ Object
11 12 13 |
# File 'lib/active_model/policy.rb', line 11 def to_policy self end |