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.



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

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
# 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, :if, :unless)) do |controller|
    controller.class.cancan_resource_class.new(controller, resource_name, options.except(:only, :except, :if, :unless)).send(method)
  end
end

Instance Method Details

#authorize_resourceObject



39
40
41
42
43
# File 'lib/cancan/controller_resource.rb', line 39

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

#load_and_authorize_resourceObject



24
25
26
27
# File 'lib/cancan/controller_resource.rb', line 24

def load_and_authorize_resource
  load_resource
  authorize_resource
end

#load_resourceObject



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

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)


45
46
47
# File 'lib/cancan/controller_resource.rb', line 45

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)


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

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