adding :nested option for load_resource - closes #10
This commit is contained in:
@@ -35,7 +35,12 @@ module CanCan
|
||||
#
|
||||
# [:+except+]
|
||||
# Does not apply before filter to given actions.
|
||||
#
|
||||
#
|
||||
# [:+nested+]
|
||||
# Specify which resource this is nested under.
|
||||
#
|
||||
# load_resource :nested => :author
|
||||
#
|
||||
# [:+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+
|
||||
|
||||
@@ -14,11 +14,20 @@ module CanCan
|
||||
end
|
||||
|
||||
def load_resource
|
||||
load_parent if @options[:nested]
|
||||
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]
|
||||
if parent_instance
|
||||
self.model_instance = parent_instance.send(model_name.pluralize).build(params[model_name.to_sym])
|
||||
else
|
||||
self.model_instance = model_class.new(params[model_name.to_sym])
|
||||
end
|
||||
elsif params[:id]
|
||||
if parent_instance
|
||||
self.model_instance = parent_instance.send(model_name.pluralize).find(params[:id])
|
||||
else
|
||||
self.model_instance = model_class.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -37,6 +46,18 @@ module CanCan
|
||||
model_name.camelcase.constantize
|
||||
end
|
||||
|
||||
def load_parent
|
||||
self.parent_instance = parent_class.find(parent_id)
|
||||
end
|
||||
|
||||
def parent_class
|
||||
@options[:nested].to_s.camelcase.constantize
|
||||
end
|
||||
|
||||
def parent_id
|
||||
@params["#{@options[:nested]}_id".to_sym]
|
||||
end
|
||||
|
||||
def model_instance
|
||||
@controller.instance_variable_get("@#{model_name}")
|
||||
end
|
||||
@@ -45,6 +66,14 @@ module CanCan
|
||||
@controller.instance_variable_set("@#{model_name}", instance)
|
||||
end
|
||||
|
||||
def parent_instance
|
||||
@controller.instance_variable_get("@#{@options[:nested]}")
|
||||
end
|
||||
|
||||
def parent_instance=(instance)
|
||||
@controller.instance_variable_set("@#{@options[:nested]}", instance)
|
||||
end
|
||||
|
||||
def collection_actions
|
||||
[:index] + [@options[:collection]].flatten
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user