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 !
API change: save_resource and resource_saved? deprecated

  ActiveRecord::Base now has a saved? and attempted_save? method, which makes 
  response_for much easier
  to write, and leaves less artefacts in the controller instance.

  So save_resource, and resource_saved? are now deprecated, just use 
  resource.save, and resource.saved?
  instead.  See response_for_resources_controller if you're into response_for.
ianwhite (author)
Mon Sep 08 03:46:15 -0700 2008
commit  2f496874436e334346d4b0d8029a055c6821c93b
tree    502fe0e15d5956c4aa2ff9f2bb869d86e7116540
parent  633fe459e681a3dd3c24434d431ef4f0600b45eb
...
 
 
 
 
 
 
 
 
1
2
3
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1,3 +1,11 @@
0
+* API change: save_resource and resource_saved? deprecated
0
+
0
+  ActiveRecord::Base now has a saved? and attempted_save? method, which makes response_for much easier
0
+  to write, and leaves less artefacts in the controller instance.
0
+  
0
+  So save_resource, and resource_saved? are now deprecated, just use resource.save, and resource.saved?
0
+  instead.  See response_for_resources_controller if you're into response_for.
0
+  
0
 * rspec compat:  Added new rake task to test that an RC controller passes the default rspec_scaffold
0
   controller specs.
0
 
...
13
14
15
 
16
17
18
...
13
14
15
16
17
18
19
0
@@ -13,6 +13,7 @@ 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', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
0
+  t.rdoc_files.include('README.rdoc', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
0
   t.rdoc_files.include('lib/**/*.rb')
0
 end
0
 
...
768
769
770
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771
772
773
...
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
 
...
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
...
812
813
814
 
 
 
 
 
 
815
816
817
...
853
854
855
856
857
858
859
860
861
862
863
864
...
869
870
871
872
873
874
875
876
877
878
...
894
895
896
 
897
898
 
899
0
@@ -768,6 +768,26 @@ UsersController handling PUT /users/dave
0
 UsersController handling DELETE /users/dave
0
 - should be unknown action
0
 
0
+(re: saved?) Comment.new(<invalid attrs>)
0
+- should not be attempted save
0
+- should not be saved
0
+
0
+(re: saved?) Comment.new(<invalid attrs>).save
0
+- should be attempted save
0
+- should not be saved
0
+
0
+(re: saved?) Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>)
0
+- should be attempted save
0
+- should be saved
0
+
0
+(re: saved?) Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>).reload
0
+- should not be attempted save
0
+- should not be saved
0
+
0
+(re: saved?) Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>).reload.save
0
+- should be attempted save
0
+- should be saved
0
+
0
 ActionView with resources_controller Helper
0
 - should forward #resource_name to controller
0
 - should forward #resources_name to controller
0
@@ -792,12 +812,6 @@ 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
@@ -839,6 +853,12 @@ ResourcesController.load_enclosing_resources_filter_exists? when :find_filter no
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
@@ -849,6 +869,10 @@ 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
+- #resource_saved? should send resource.saved?
0
+
0
 #route_enclosing_names TagsController for named_route:
0
 - :tags should be []
0
 - :new_tag should be []
0
@@ -870,6 +894,6 @@ A controller's resource_service
0
 #route_enclosing_names Admin::Superduper::ForumsController for named_route:
0
 - :admin_superduper_forums should be []
0
 
0
-Finished in 5.097926 seconds
0
+Finished in 8.867688 seconds
0
 
0
-593 examples, 0 failures
0
+605 examples, 0 failures
...
36
37
38
39
40
 
41
42
43
...
36
37
38
 
 
39
40
41
42
0
@@ -36,8 +36,7 @@ garlic do
0
   
0
     run do
0
       cd "vendor/plugins/resources_controller" do
0
-        sh "rake spec:rcov:verify"
0
-        sh "rake spec:generate"
0
+        sh "rake spec:rcov:verify && rake spec:generate"
0
       end
0
     end
0
   end
...
1
 
 
2
3
4
 
 
...
 
1
2
3
 
4
5
6
0
@@ -1,3 +1,5 @@
0
-require 'ardes/resources_controller'
0
+require 'ardes/active_record/saved'
0
+ActiveRecord::Base.send :include, Ardes::ActiveRecord::Saved
0
 
0
-ActionController::Base.extend Ardes::ResourcesController
0
\ No newline at end of file
0
+require 'ardes/resources_controller'
0
+ActionController::Base.extend Ardes::ResourcesController
...
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
683
684
685
686
687
0
@@ -665,18 +665,23 @@ module Ardes#:nodoc:
0
         @enclosing_collection_resources ||= []
0
       end
0
       
0
+      # DEPRECATED: just use resource.saved?
0
+      #
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
       # 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
+        resource.save unless resource.attempted_save?
0
+        resource.saved?
0
       end
0
+      deprecate :resource_saved? => 'Use resource.saved?'
0
       
0
+      # DEPRECATED: just use resource.save
0
       def save_resource
0
-        @resource_saved = resource.save
0
+        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
123
...
93
94
95
 
96
97
 
98
99
100
101
...
114
115
116
 
 
117
118
 
119
120
121
122
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_saved?
0
+          if resource.save
0
             format.html do
0
               flash[:notice] = "#{resource_name.humanize} was successfully created."
0
               redirect_to resource_url
0
@@ -114,10 +114,9 @@ module Ardes#:nodoc:
0
       # PUT /events/1.xml
0
       def update
0
         self.resource = find_resource
0
-        resource.attributes = params[resource_name]
0
-
0
+        
0
         respond_to do |format|
0
-          if resource_saved?
0
+          if resource.update_attributes(params[resource_name])
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
...
156
157
158
159
160
161
162
163
164
165
 
166
167
168
0
@@ -156,12 +156,13 @@ 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(:attributes=)
0
+    @address.should_receive(:update_attributes).and_return(true)
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(:attributes=)
0
+    @mock_forum.should_receive(:update_attributes).and_return(true)
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(:attributes=)
0
+    @mock_forum.should_receive(:update_attributes).and_return(true)
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!(:save).and_return(false)
0
+    @mock_forum.stub!(:update_attributes).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
0
+    @comment        = Comment.create :post_id => @post.id, :user => User.create
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(:attributes=)
0
+    @comment.should_receive(:update_attributes).and_return(true)
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(:attributes=)
0
+    @post.should_receive(:update_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(:attributes=)
0
+    @mock_forum.should_receive(:update_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(:attributes=)
0
+    @mock_forum.should_receive(:update_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!(:save).and_return(false)
0
+    @mock_forum.stub!(:update_attributes).and_return(false)
0
     do_update
0
     response.should render_template('edit')
0
   end
...
52
53
54
55
56
 
57
58
 
59
60
61
...
52
53
54
 
 
55
56
 
57
58
59
60
0
@@ -52,10 +52,9 @@ describe InfosController, " (its actions)" do
0
   end
0
   
0
   it "PUT /account/info should be successful" do
0
-    @info.stub!(:attributes=)
0
-    @info.stub!(:save)
0
+    @info.stub!(:update_attributes).and_return(true)
0
     put :update
0
-    response.should be_success
0
+    response.should be_redirect
0
   end
0
   
0
   it "GET /account/info/new should raise UnknownAction" do
...
220
221
222
223
224
 
225
226
227
...
239
240
241
242
 
243
244
245
...
220
221
222
 
 
223
224
225
226
...
238
239
240
 
241
242
243
244
0
@@ -220,8 +220,7 @@ describe "Requesting /forums/2/owner using PUT" do
0
 
0
   before(:each) do
0
     setup_mocks
0
-    @owner.stub!(:save).and_return(true)
0
-    @owner.stub!(:attributes=)
0
+    @owner.stub!(:update_attributes).and_return(true)
0
   end
0
   
0
   def do_update
0
@@ -239,7 +238,7 @@ describe "Requesting /forums/2/owner using PUT" do
0
   end
0
 
0
   it "should update the owner" do
0
-    @owner.should_receive(:attributes=).with('name' => 'Fred')
0
+    @owner.should_receive(:update_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
0
+    @comment       = Comment.create :post_id => @post.id, :user => User.create!
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
247
248
...
235
236
237
 
 
238
239
240
241
242
 
 
243
244
245
246
0
@@ -235,14 +235,12 @@ describe UsersController, "handling PUT /users/dave" do
0
   end
0
   
0
   def put_with_successful_update
0
-    @user.should_receive(:attributes=).once.ordered
0
-    @user.should_receive(:save).once.ordered.and_return(true)
0
+    @user.should_receive(:update_attributes).and_return(true)
0
     put :update, :id => "dave"
0
   end
0
   
0
   def put_with_failed_update
0
-    @user.should_receive(:attributes=).once.ordered
0
-    @user.should_receive(:save).once.ordered.and_return(false)
0
+    @user.should_receive(:update_attributes).and_return(false)
0
     put :update, :id => "dave"
0
   end
0
   
...
40
41
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
44
...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
0
@@ -40,4 +40,25 @@ 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
+  
0
+  it "#resource_saved? should send resource.saved?" do
0
+    ActiveSupport::Deprecation.silence do
0
+      @controller.resource.should_receive(:saved?)
0
+      @controller.resource_saved?
0
+    end
0
+  end
0
 end
0
\ No newline at end of file

Comments