Class: CanCan::ControllerResource

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

Overview

Handle the load and authorization controller logic so we don’t clutter up all controllers with non-interface methods. This class is used internally, so you do not need to call methods directly on it.

Direct Known Subclasses

InheritedResource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, *args) ⇒ ControllerResource

Returns a new instance of ControllerResource.



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

def initialize(controller, *args)
  @controller = controller
  @params = controller.params
  @options = args.extract_options!
  @name = args.first
  raise CanCan::ImplementationRemoved, "The :nested option is no longer supported, instead use :through with separate load/authorize call." if @options[:nested]
  raise CanCan::ImplementationRemoved, "The :name option is no longer supported, instead pass the name as the first argument." if @options[:name]
  raise CanCan::ImplementationRemoved, "The :resource option has been renamed back to :class, use false if no class." if @options[:resource]
end

Class Method Details

.add_before_filter(controller_class, method, *args) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
12
13
14
# File 'lib/cancan/controller_resource.rb', line 5

def self.add_before_filter(controller_class, method, *args)
  options = args.extract_options!
  resource_name = args.first
  before_filter_method = options.delete(:prepend) ? :prepend_before_filter : :before_filter
  controller_class.send(before_filter_method, options.slice(:only, :except)) do |controller|
    options[:current_user] = controller.current_user
    Thread.current[:cancan_resource] = controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except))
    Thread.current[:cancan_resource].send(method)
  end
end

Instance Method Details

#authorize_resourceObject



41
42
43
44
45
# File 'lib/cancan/controller_resource.rb', line 41

def authorize_resource
  unless skip?(:authorize)
    @controller.authorize!(authorization_action, resource_instance || resource_class_with_parent, @options[:current_user])
  end
end

#load_and_authorize_resourceObject



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

def load_and_authorize_resource
  load_resource
  authorize_resource
end

#load_resourceObject



31
32
33
34
35
36
37
38
39
# File 'lib/cancan/controller_resource.rb', line 31

def load_resource
  unless skip?(:load)
    if load_instance?
      self.resource_instance ||= load_resource_instance
    elsif load_collection?
      self.collection_instance ||= load_collection
    end
  end
end

#parent?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/cancan/controller_resource.rb', line 47

def parent?
  @options.has_key?(:parent) ? @options[:parent] : @name && @name != name_from_controller.to_sym
end

#skip?(behavior) ⇒ Boolean

This could probably use some refactoring

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cancan/controller_resource.rb', line 51

def skip?(behavior) # This could probably use some refactoring
  options = @controller.class.cancan_skipper[behavior][@name]
  if options.nil?
    false
  elsif options == {}
    true
  elsif options[:except] && ![options[:except]].flatten.include?(@params[:action].to_sym)
    true
  elsif [options[:only]].flatten.include?(@params[:action].to_sym)
    true
  end
end