Class: CanCan::ResourceAuthorization

Inherits:
Object
  • Object
show all
Defined in:
lib/cancan/resource_authorization.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, params, options = {}) ⇒ ResourceAuthorization

Returns a new instance of ResourceAuthorization.



5
6
7
8
9
# File 'lib/cancan/resource_authorization.rb', line 5

def initialize(controller, params, options = {})
  @controller = controller
  @params = params
  @options = options
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/cancan/resource_authorization.rb', line 3

def params
  @params
end

Instance Method Details

#authorize_resourceObject



26
27
28
29
30
# File 'lib/cancan/resource_authorization.rb', line 26

def authorize_resource
  if @controller.cannot?(params[:action].to_sym, resource.model_instance || resource.model_class)
    @options[:message] ? @controller.unauthorized!(@options[:message]) : @controller.unauthorized!
  end
end

#load_and_authorize_resourceObject



11
12
13
14
# File 'lib/cancan/resource_authorization.rb', line 11

def load_and_authorize_resource
  load_resource
  authorize_resource
end

#load_resourceObject



16
17
18
19
20
21
22
23
24
# File 'lib/cancan/resource_authorization.rb', line 16

def load_resource
  unless collection_actions.include? params[:action].to_sym
    if new_actions.include? params[:action].to_sym
      resource.build(params[model_name.to_sym])
    elsif params[:id]
      resource.find(params[:id])
    end
  end
end