Class: CanCan::ControllerResource
- Inherits:
-
Object
- Object
- CanCan::ControllerResource
- 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
Class Method Summary collapse
Instance Method Summary collapse
- #authorize_resource ⇒ Object
-
#initialize(controller, *args) ⇒ ControllerResource
constructor
A new instance of ControllerResource.
- #load_and_authorize_resource ⇒ Object
- #load_resource ⇒ Object
- #parent? ⇒ Boolean
-
#skip?(behavior) ⇒ Boolean
This could probably use some refactoring.
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. @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) = args. resource_name = args.first before_filter_method = .delete(:prepend) ? :prepend_before_filter : :before_filter controller_class.send(before_filter_method, .slice(:only, :except)) do |controller| controller.class.cancan_resource_class.new(controller, resource_name, .except(:only, :except)).send(method) end end |
Instance Method Details
#authorize_resource ⇒ Object
39 40 41 42 43 |
# File 'lib/cancan/controller_resource.rb', line 39 def unless skip?(:authorize) @controller.(, resource_instance || resource_class_with_parent) end end |
#load_and_authorize_resource ⇒ Object
24 25 26 27 |
# File 'lib/cancan/controller_resource.rb', line 24 def load_resource end |
#load_resource ⇒ Object
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
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
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 = @controller.class.cancan_skipper[behavior][@name] if .nil? false elsif == {} true elsif [:except] && ![[:except]].flatten.include?(@params[:action].to_sym) true elsif [[:only]].flatten.include?(@params[:action].to_sym) true end end |