Adding :collection and :new options to load_resource method so we can specify behavior of additional actions if needed.
This commit is contained in:
@@ -28,7 +28,23 @@ module CanCan
|
||||
# end
|
||||
#
|
||||
# See load_and_authorize_resource to automatically authorize the resource too.
|
||||
def load_resource(*args) # TODO add documentation for options which can be passed.
|
||||
#
|
||||
# Options:
|
||||
# [:+collection+]
|
||||
# Specify which actions are resource collection actions in addition to :+index+. This
|
||||
# is usually not necessary because it will try to guess depending on if an :+id+
|
||||
# is present in +params+.
|
||||
#
|
||||
# load_resource :collection => [:sort, :list]
|
||||
#
|
||||
# [:+new+]
|
||||
# Specify which actions are new resource actions in addition to :+new+ and :+create+.
|
||||
# Pass an action name into here if you would like to build a new resource instead of
|
||||
# fetch one.
|
||||
#
|
||||
# load_resource :new => :build
|
||||
#
|
||||
def load_resource(*args)
|
||||
before_filter { |c| ResourceAuthorization.new(c, c.params, *args).load_resource }
|
||||
end
|
||||
|
||||
|
||||
@@ -2,9 +2,10 @@ module CanCan
|
||||
class ResourceAuthorization # :nodoc:
|
||||
attr_reader :params
|
||||
|
||||
def initialize(controller, params)
|
||||
def initialize(controller, params, options = {})
|
||||
@controller = controller
|
||||
@params = params
|
||||
@options = options
|
||||
end
|
||||
|
||||
def load_and_authorize_resource
|
||||
@@ -13,7 +14,13 @@ module CanCan
|
||||
end
|
||||
|
||||
def load_resource
|
||||
self.model_instance = params[:id] ? model_class.find(params[:id]) : model_class.new(params[model_name.to_sym]) unless params[:action] == "index"
|
||||
unless collection_actions.include? params[:action].to_sym
|
||||
if new_actions.include? params[:action].to_sym
|
||||
self.model_instance = model_class.new(params[model_name.to_sym])
|
||||
else
|
||||
self.model_instance = model_class.find(params[:id]) if params[:id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def authorize_resource
|
||||
@@ -37,5 +44,13 @@ module CanCan
|
||||
def model_instance=(instance)
|
||||
@controller.instance_variable_set("@#{model_name}", instance)
|
||||
end
|
||||
|
||||
def collection_actions
|
||||
[:index] + [@options[:collection]].flatten
|
||||
end
|
||||
|
||||
def new_actions
|
||||
[:new, :create] + [@options[:new]].flatten
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user