Class: ActiveModel::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model/policy.rb,
lib/active_model/version.rb

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#modelObject (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

Raises:

  • (ArgumentError)


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

Returns:

  • (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

Returns:

  • (Boolean)


24
25
26
# File 'lib/active_model/policy.rb', line 24

def cannot? *args
  !can? *args
end

#to_policyObject



11
12
13
# File 'lib/active_model/policy.rb', line 11

def to_policy
  self
end