fix resource loading for new action

This commit is contained in:
Ryan Bates 2009-11-16 19:26:33 -08:00
parent aaed265c91
commit 7b299b50fc
2 changed files with 14 additions and 5 deletions

View File

@ -17,10 +17,12 @@ module CanCan
end
def load_resource # TODO this could use some refactoring
if params[:id]
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.find(params[:id]))
elsif params[params[:controller].singularize.to_sym]
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.new(params[params[:controller].singularize.to_sym]))
unless params[:action] == "index"
if params[:id]
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.find(params[:id]))
else
instance_variable_set("@#{params[:controller].singularize}", params[:controller].singularize.camelcase.constantize.new(params[params[:controller].singularize.to_sym]))
end
end
end

View File

@ -44,7 +44,14 @@ describe CanCan::ControllerAdditions do
@controller.instance_variable_get(:@ability).should == :some_resource
end
it "should not build a resource of neither id nor attributes are specified" do
it "should build a new resource even if attribute hash isn't specified" do
stub(@controller).params { {:controller => "abilities", :action => "new"} }
stub(Ability).new(nil) { :some_resource }
@controller.load_resource
@controller.instance_variable_get(:@ability).should == :some_resource
end
it "should not build a resource when on index action" do
stub(@controller).params { {:controller => "abilities", :action => "index"} }
@controller.load_resource
@controller.instance_variable_get(:@ability).should be_nil