Permalink
Browse files

fix resource loading for new action

  • Loading branch information...
ryanb committed Nov 17, 2009
1 parent aaed265 commit 7b299b50fc88fbd5c87405b3bafb2a24a13196d7
Showing with 14 additions and 5 deletions.
  1. +6 −4 lib/cancan/controller_additions.rb
  2. +8 −1 spec/cancan/controller_additions_spec.rb
@@ -17,10 +17,12 @@ def can?(*args)
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
@@ -44,7 +44,14 @@ class Ability
@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

0 comments on commit 7b299b5

Please sign in to comment.