public
Description: resources_controller rails plugin: rc makes RESTful controllers fun
Homepage: http://plugins.ardes.com/doc/resources_controller
Clone URL: git://github.com/ianwhite/resources_controller.git
Click here to lend your support to: resources_controller and make a donation at www.pledgie.com !
Reverted 2f49687, 7423cf1 (save_resource and resource_saved? stuff) because of 
aberrant behaviour.
Will add them back in with some extra specs.  [Jason Lee]
ianwhite (author)
Fri Sep 12 03:27:32 -0700 2008
commit  c21f35c35ef71576c99d412bc562ab10278c652b
tree    aa15e604404d44822bb39593e15322e530e62582
parent  2a6493b1f7b812aa4da7c1c8d6a97a9667296ba5
...
1
2
3
4
5
6
7
8
9
10
11
12
...
 
 
 
 
 
 
 
 
 
1
2
3
0
@@ -1,12 +1,3 @@
0
-* API change: save_resource deprecated
0
-
0
-  So save_resource is now deprecated, just use resource.save
0
-  
0
-  resource_saved? has slightly different semantics - it returns true if the record is not new and has no errors
0
-  
0
-  WTF?: save_resource used to set an instance var to track whether the resource was saved.  This is so that
0
-        controller can make decisions based on this outside the scope of an action (in response_for, and after_filters)
0
-  
0
 * rspec compat:  Added new rake task to test that an RC controller passes the default rspec_scaffold
0
   controller specs.
0
 
...
14
15
16
17
18
19
20
...
14
15
16
 
17
18
19
0
@@ -14,7 +14,6 @@ resources_controller works with rails 2.x and edge.
0
 
0
 * The SPECDOC lists the specifications
0
 * Coverage is 100% (C0), and the spec suite is quite comprehensive
0
-* Rspec's generated rspec_scaffold controller specs are tested against a simple rc controller (see rake spec:generate)
0
 
0
 RSpec is used for testing, so the tests are in <tt>spec/</tt> rather than
0
 <tt>test/</tt> Do rake --tasks for more details.
...
60
61
62
63
 
64
65
66
...
60
61
62
 
63
64
65
66
0
@@ -60,7 +60,7 @@ Rake::RDocTask.new(:doc) do |t|
0
   t.title    = "#{plugin_name}"
0
   t.template = ENV['RDOC_TEMPLATE']
0
   t.options  = ['--line-numbers', '--inline-source', '--all']
0
-  t.rdoc_files.include('README.rdoc', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
0
+  t.rdoc_files.include('README', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
0
   t.rdoc_files.include('lib/**/*.rb')
0
 end
0
 
...
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
...
811
812
813
 
 
 
 
 
 
814
815
816
...
852
853
854
855
856
857
858
859
860
861
862
863
...
868
869
870
871
872
873
874
875
876
...
892
893
894
895
 
896
897
 
...
575
576
577
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
579
580
...
792
793
794
795
796
797
798
799
800
801
802
803
...
839
840
841
 
 
 
 
 
 
842
843
844
...
849
850
851
 
 
 
852
853
854
...
870
871
872
 
873
874
 
875
0
@@ -575,25 +575,6 @@ Requesting /forums/2/owner using DELETE
0
 - should set the flash notice
0
 - should redirect to forums/2
0
 
0
-CommentsController#resource_saved Comment.new(<invalid attrs>)
0
-- should not be resource saved
0
-
0
-CommentsController#resource_saved Comment.new(<invalid attrs>).save
0
-- should not be resource saved
0
-
0
-CommentsController#resource_saved Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>)
0
-- should be resource saved
0
-
0
-CommentsController#resource_saved Comment.find(<id>)
0
-- should be resource saved
0
-- .save should be saved
0
-
0
-CommentsController#resource_saved Comment.find(<id>) then update_attributes(<invalid attrs>)
0
-- should not be resource saved
0
-
0
-CommentsController#resource_saved Comment.find(<id>) then update_attributes(<new valid attrs>)
0
-- should be resource saved
0
-
0
 Routing shortcuts for Tags should map
0
 - resources_path to /tags
0
 - resource_path to /tags/2
0
@@ -811,6 +792,12 @@ Helper#form_for_resource (when resource is existing record)
0
 Helper#remote_form_for_resource (when resource is existing record)
0
 - should call remote_form_for with update form options
0
 
0
+ResourcesController.load_enclosing_resources_filter_exists? when :find_filter defined
0
+- should call :find_filter with :load_enclosing_resources
0
+
0
+ResourcesController.load_enclosing_resources_filter_exists? when :find_filter not defined
0
+- should call :filter_chain
0
+
0
 #load_enclosing_resources for resources_controller_for :tags (when route_enclosing_names is [['users', false]])
0
 - should call load_wildcard once
0
 - should call Specification.new('user', :singleton => false, :as => nil)
0
@@ -852,12 +839,6 @@ Helper#remote_form_for_resource (when resource is existing record)
0
 - should call load_enclosing_resource_from_specification with user spec, then load_wildcard once with 'taggable'
0
 - should call Specification.new with ('comment', :singleton => false, :as => 'taggable')
0
 
0
-ResourcesController.load_enclosing_resources_filter_exists? when :find_filter defined
0
-- should call :find_filter with :load_enclosing_resources
0
-
0
-ResourcesController.load_enclosing_resources_filter_exists? when :find_filter not defined
0
-- should call :filter_chain
0
-
0
 ResourcesController (in general)
0
 - nested_in :foo, :polymorphic => true, :class => User should raise argument error (no options or block with polymorphic)
0
 - resources_controller_for :forums, :in => [:user, '*', '*', :comment] should raise argument error (no multiple wildcards in a row)
0
@@ -868,9 +849,6 @@ ResourcesController#enclosing_resource_name
0
 A controller's resource_service
0
 - may be explicitly set with #resource_service=
0
 
0
-deprecated methods
0
-- #save_resource should send resource.save
0
-
0
 #route_enclosing_names TagsController for named_route:
0
 - :tags should be []
0
 - :new_tag should be []
0
@@ -892,6 +870,6 @@ deprecated methods
0
 #route_enclosing_names Admin::Superduper::ForumsController for named_route:
0
 - :admin_superduper_forums should be []
0
 
0
-Finished in 8.299069 seconds
0
+Finished in 5.097926 seconds
0
 
0
-601 examples, 0 failures
0
+593 examples, 0 failures
...
36
37
38
39
 
 
40
41
42
...
36
37
38
 
39
40
41
42
43
0
@@ -36,7 +36,8 @@ garlic do
0
   
0
     run do
0
       cd "vendor/plugins/resources_controller" do
0
-        sh "rake spec:rcov:verify && rake spec:generate"
0
+        sh "rake spec:rcov:verify"
0
+        sh "rake spec:generate"
0
       end
0
     end
0
   end
...
1
 
2
3
...
1
2
3
4
0
@@ -1,2 +1,3 @@
0
 require 'ardes/resources_controller'
0
+
0
 ActionController::Base.extend Ardes::ResourcesController
0
\ No newline at end of file
...
665
666
667
668
 
 
669
670
671
672
 
 
 
 
673
674
675
676
677
 
678
679
680
681
682
...
665
666
667
 
668
669
670
 
 
 
671
672
673
674
675
676
 
677
 
678
679
 
680
681
682
0
@@ -665,18 +665,18 @@ module Ardes#:nodoc:
0
         @enclosing_collection_resources ||= []
0
       end
0
       
0
-      # Has the resource been saved?
0
+      # Returns self.resource.save and caches the result for future calls.
0
+      # This is useful when you want to know outside of an action whether the resource was saved.
0
       #
0
-      # Returns true if the record is not new, and there are no errors
0
-      def resource_saved?
0
-        !resource.new_record? && resource.errors.empty?
0
+      # Pass true to ignore the cached value
0
+      def resource_saved?(reload = false)
0
+        save_resource if reload || @resource_saved.nil?
0
+        @resource_saved
0
       end
0
       
0
-      # DEPRECATED: just use resource.save
0
       def save_resource
0
-        resource.save
0
+        @resource_saved = resource.save
0
       end
0
-      deprecate :save_resource => 'Use resource.save'
0
       
0
     private
0
       # returns the route that was used to invoke this controller and current action.  The path is found first from params[:resource_path]
...
93
94
95
96
 
97
98
 
99
100
101
...
114
115
116
117
 
 
118
119
 
120
121
122
...
93
94
95
 
96
97
 
98
99
100
101
...
114
115
116
 
117
118
119
 
120
121
122
123
0
@@ -93,9 +93,9 @@ module Ardes#:nodoc:
0
       # POST /events.xml
0
       def create
0
         self.resource = new_resource
0
-        
0
+
0
         respond_to do |format|
0
-          if resource.save
0
+          if resource_saved?
0
             format.html do
0
               flash[:notice] = "#{resource_name.humanize} was successfully created."
0
               redirect_to resource_url
0
@@ -114,9 +114,10 @@ module Ardes#:nodoc:
0
       # PUT /events/1.xml
0
       def update
0
         self.resource = find_resource
0
-        
0
+        resource.attributes = params[resource_name]
0
+
0
         respond_to do |format|
0
-          if resource.update_attributes(params[resource_name])
0
+          if resource_saved?
0
             format.html do
0
               flash[:notice] = "#{resource_name.humanize} was successfully updated."
0
               redirect_to resource_url
...
156
157
158
159
160
161
162
163
164
165
 
166
167
168
...
156
157
158
 
 
159
160
161
162
163
164
165
166
167
0
@@ -156,13 +156,12 @@ class Post < ActiveRecord::Base
0
 end
0
 
0
 class Comment < ActiveRecord::Base
0
-  validates_presence_of :user, :post
0
-  
0
   belongs_to :user
0
   belongs_to :post
0
   has_many :tags, :as => :taggable
0
 end
0
 
0
+
0
 ##############
0
 # Controllers
0
 ##############
...
309
310
311
312
 
313
314
315
...
309
310
311
 
312
313
314
315
0
@@ -309,7 +309,7 @@ describe "Requesting /users/dave/addresses/1 using PUT" do
0
   end
0
 
0
   it "should update the found address" do
0
-    @address.should_receive(:update_attributes).and_return(true)
0
+    @address.should_receive(:attributes=)
0
     do_update
0
   end
0
 
...
522
523
524
525
 
526
527
528
...
558
559
560
561
 
562
563
564
...
579
580
581
582
 
583
584
585
...
522
523
524
 
525
526
527
528
...
558
559
560
 
561
562
563
564
...
579
580
581
 
582
583
584
585
0
@@ -522,7 +522,7 @@ describe "Requesting /admin/forums/1 using PUT" do
0
   end
0
 
0
   it "should update the found forum" do
0
-    @mock_forum.should_receive(:update_attributes).and_return(true)
0
+    @mock_forum.should_receive(:attributes=)
0
     do_update
0
     assigns(:forum).should == @mock_forum
0
   end
0
@@ -558,7 +558,7 @@ describe "Requesting /admin/forums/1 using XHR PUT" do
0
   end
0
 
0
   it "should update the found forum" do
0
-    @mock_forum.should_receive(:update_attributes).and_return(true)
0
+    @mock_forum.should_receive(:attributes=)
0
     do_update
0
     assigns(:forum).should == @mock_forum
0
   end
0
@@ -579,7 +579,7 @@ describe "Requesting /admin/forums/1 using XHR PUT" do
0
   end
0
   
0
   it "should render edit.rjs, on unsuccessful save" do
0
-    @mock_forum.stub!(:update_attributes).and_return(false)
0
+    @mock_forum.stub!(:save).and_return(false)
0
     do_update
0
     response.should render_template('edit')
0
   end
...
77
78
79
80
 
81
82
83
...
342
343
344
345
 
346
347
348
...
77
78
79
 
80
81
82
83
...
342
343
344
 
345
346
347
348
0
@@ -77,7 +77,7 @@ describe "resource_service in CommentsController" do
0
   before(:each) do
0
     @forum          = Forum.create
0
     @post           = Post.create :forum_id => @forum.id
0
-    @comment        = Comment.create :post_id => @post.id, :user => User.create
0
+    @comment        = Comment.create :post_id => @post.id
0
     @other_post     = Post.create :forum_id => @forum.id
0
     @other_comment  = Comment.create :post_id => @other_post.id
0
     
0
@@ -342,7 +342,7 @@ describe "Requesting /forums/3/posts/3/comments/1 using PUT" do
0
   end
0
 
0
   it "should update the found comment" do
0
-    @comment.should_receive(:update_attributes).and_return(true)
0
+    @comment.should_receive(:attributes=)
0
     do_update
0
   end
0
 
...
395
396
397
398
 
399
400
401
...
395
396
397
 
398
399
400
401
0
@@ -395,7 +395,7 @@ describe "Requesting /forums/2/posts/1 using PUT" do
0
   end
0
 
0
   it "should update the found post" do
0
-    @post.should_receive(:update_attributes)
0
+    @post.should_receive(:attributes=)
0
     do_update
0
   end
0
 
...
599
600
601
602
 
603
604
605
...
635
636
637
638
 
639
640
641
...
656
657
658
659
 
660
661
662
...
599
600
601
 
602
603
604
605
...
635
636
637
 
638
639
640
641
...
656
657
658
 
659
660
661
662
0
@@ -599,7 +599,7 @@ describe "Requesting /forums/1 using PUT" do
0
   end
0
 
0
   it "should update the found forum" do
0
-    @mock_forum.should_receive(:update_attributes)
0
+    @mock_forum.should_receive(:attributes=)
0
     do_update
0
     assigns(:forum).should == @mock_forum
0
   end
0
@@ -635,7 +635,7 @@ describe "Requesting /forums/1 using XHR PUT" do
0
   end
0
 
0
   it "should update the found forum" do
0
-    @mock_forum.should_receive(:update_attributes)
0
+    @mock_forum.should_receive(:attributes=)
0
     do_update
0
     assigns(:forum).should == @mock_forum
0
   end
0
@@ -656,7 +656,7 @@ describe "Requesting /forums/1 using XHR PUT" do
0
   end
0
   
0
   it "should render edit.rjs, on unsuccessful save" do
0
-    @mock_forum.stub!(:update_attributes).and_return(false)
0
+    @mock_forum.stub!(:save).and_return(false)
0
     do_update
0
     response.should render_template('edit')
0
   end
...
52
53
54
55
 
 
56
57
 
58
59
60
...
52
53
54
 
55
56
57
 
58
59
60
61
0
@@ -52,9 +52,10 @@ describe InfosController, " (its actions)" do
0
   end
0
   
0
   it "PUT /account/info should be successful" do
0
-    @info.stub!(:update_attributes).and_return(true)
0
+    @info.stub!(:attributes=)
0
+    @info.stub!(:save)
0
     put :update
0
-    response.should be_redirect
0
+    response.should be_success
0
   end
0
   
0
   it "GET /account/info/new should raise UnknownAction" do
...
220
221
222
223
 
 
224
225
226
...
238
239
240
241
 
242
243
244
...
220
221
222
 
223
224
225
226
227
...
239
240
241
 
242
243
244
245
0
@@ -220,7 +220,8 @@ describe "Requesting /forums/2/owner using PUT" do
0
 
0
   before(:each) do
0
     setup_mocks
0
-    @owner.stub!(:update_attributes).and_return(true)
0
+    @owner.stub!(:save).and_return(true)
0
+    @owner.stub!(:attributes=)
0
   end
0
   
0
   def do_update
0
@@ -238,7 +239,7 @@ describe "Requesting /forums/2/owner using PUT" do
0
   end
0
 
0
   it "should update the owner" do
0
-    @owner.should_receive(:update_attributes).with('name' => 'Fred')
0
+    @owner.should_receive(:attributes=).with('name' => 'Fred')
0
     do_update
0
   end
0
 
...
71
72
73
74
 
75
76
77
...
71
72
73
 
74
75
76
77
0
@@ -71,7 +71,7 @@ describe "resource_service in TagsController via Forum, Post and Comment" do
0
   before(:each) do
0
     @forum         = Forum.create
0
     @post          = Post.create :forum_id => @forum.id
0
-    @comment       = Comment.create :post_id => @post.id, :user => User.create!
0
+    @comment       = Comment.create :post_id => @post.id
0
     @tag           = Tag.create :taggable_id => @comment.id, :taggable_type => 'Comment'
0
     @other_comment = Comment.create :post_id => @forum.id
0
     @other_tag     = Tag.create :taggable_id => @other_comment.id, :taggable_type => 'Comment'
...
235
236
237
238
 
 
239
240
241
242
243
 
 
244
245
246
...
235
236
237
 
238
239
240
241
242
243
 
244
245
246
247
248
0
@@ -235,12 +235,14 @@ describe UsersController, "handling PUT /users/dave" do
0
   end
0
   
0
   def put_with_successful_update
0
-    @user.should_receive(:update_attributes).and_return(true)
0
+    @user.should_receive(:attributes=).once.ordered
0
+    @user.should_receive(:save).once.ordered.and_return(true)
0
     put :update, :id => "dave"
0
   end
0
   
0
   def put_with_failed_update
0
-    @user.should_receive(:update_attributes).and_return(false)
0
+    @user.should_receive(:attributes=).once.ordered
0
+    @user.should_receive(:save).once.ordered.and_return(false)
0
     put :update, :id => "dave"
0
   end
0
   
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
0
@@ -40,18 +40,4 @@ describe "A controller's resource_service" do
0
     @controller.resource_service = 'foo'
0
     @controller.resource_service.should == 'foo'
0
   end
0
-end
0
-
0
-describe "deprecated methods" do
0
-  before do 
0
-    @controller = ForumsController.new
0
-    @controller.resource = Forum.new
0
-  end
0
-  
0
-  it "#save_resource should send resource.save" do
0
-    ActiveSupport::Deprecation.silence do
0
-      @controller.resource.should_receive :save
0
-      @controller.save_resource
0
-    end
0
-  end
0
 end
0
\ No newline at end of file

Comments