Skip to content

Commit

Permalink
The reason for the reversion in c21f35c has been fixed. Thanks Jason …
Browse files Browse the repository at this point in the history
…Lee for the bug report.

The problem was that I had changed resource_saved?'s behaviour to *not* saving the model if it had
already been saved.  In the future resource_saved? will be deprecated, but not yet.

BTW.  All of these changes to resource_saved? behaviour is aimed at making RC drop in compatible with
rspec's generated controller specs (try rake spec:generate).

To do that I need the default update action to use :update_attributes. This meant that the old strategy
of keeping track of saves by using save_resource wont work.  Instead, we keep track by looking at the
AR's state (see lib/ardes/active_record/saved.rb) which is a far better solution anyway.

Squashed commit of the following:

commit f8a589f9b3711140fa7e125a42681acc1ad97e10
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 05:21:23 2008 +1000

    Docfixes, CHANGELOG, SPECDOC

commit e670fd2e18678d94292bc684e57f8bf64a2e2dc9
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 05:09:24 2008 +1000

    Fixed logic error in AR.saved?

commit b3e2838e1515503820e49eaf7853099df33a0d8e
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 05:05:29 2008 +1000

    Adding specs for AR.saved? and AR.validation_attempted?

commit 3e649c53d06a788b5e70878b2cb7e386571fce31
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 04:56:56 2008 +1000

    Fixed bug in init.rb

commit c72b1a917339b7ee94700d8eb6b6bd939ad7e4b2
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 04:56:28 2008 +1000

    Fixed bug in init.rb

commit d5702c06d8d54b80dd011f79ff3d2ac782ce902e
Author: Ian White <ian.w.white@gmail.com>
Date:   Sun Sep 14 04:55:18 2008 +1000

    Added AR.saved? & AR.validation_atempted? convenience methods.  Returned save_resource?'s behaviour to that of saving the record if it has not had save attempted

commit af11af7848811da5ca87efdd8eb678251c5bd6d5
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 09:58:15 2008 +1000

    Added resource_saved? specs, and removed .first and .last for BC in specs to 2.0.x

commit 5efb59f7c0fb8be72b12b5476291e59e6af658c0
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 09:57:30 2008 +1000

    Added resource_saved? specs, and removed .first and .last for BC in specs to 2.0.x

commit 8b73130de97ab87369ec395be4ee696de76e773a
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 09:46:52 2008 +1000

    Small fix for comments_with_models spec

commit 3a2426cd00fa945d155d4f3ebff6ccc48a7b0cf2
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 09:29:26 2008 +1000

    Added specs for comments_controller which use real DB objects

commit 79a93132f6e49a556775e6508a5773686ccd6332
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 06:27:51 2008 +1000

    Fixing comments with models spec

commit 05f551f5755e6db41a83dde0c9b0db99a4e168d8
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 06:24:19 2008 +1000

    Added some controller specs with real models

commit 6163aba8c5315598d173cd205bc2d69286ae4271
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 06:02:36 2008 +1000

    Updated 2.1.x target

commit b3c6d13a77aff7d831e136221ead8b7fa7bed4e1
Author: Ian White <ian.w.white@gmail.com>
Date:   Sat Sep 13 04:54:40 2008 +1000

    Add resource_saved stuff back in (2f49687, 7423cf1)

    This reverts commit c21f35c.
  • Loading branch information
ianwhite committed Sep 13, 2008
1 parent c21f35c commit ceaf1b9
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 48 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
@@ -1,3 +1,8 @@
* API change: save_resource deprecated

So save_resource is now deprecated, just use resource.save
ActiveRecords can now be asked if they are saved?

* rspec compat: Added new rake task to test that an RC controller passes the default rspec_scaffold
controller specs.

Expand Down
1 change: 1 addition & 0 deletions README.rdoc
Expand Up @@ -14,6 +14,7 @@ resources_controller works with rails 2.x and edge.

* The SPECDOC lists the specifications
* Coverage is 100% (C0), and the spec suite is quite comprehensive
* Rspec's generated rspec_scaffold controller specs are tested against a simple rc controller (see rake spec:generate)

RSpec is used for testing, so the tests are in <tt>spec/</tt> rather than
<tt>test/</tt> Do rake --tasks for more details.
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -60,7 +60,7 @@ Rake::RDocTask.new(:doc) do |t|
t.title = "#{plugin_name}"
t.template = ENV['RDOC_TEMPLATE']
t.options = ['--line-numbers', '--inline-source', '--all']
t.rdoc_files.include('README', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
t.rdoc_files.include('README.rdoc', 'SPECDOC', 'MIT-LICENSE', 'CHANGELOG')
t.rdoc_files.include('lib/**/*.rb')
end

Expand Down
97 changes: 89 additions & 8 deletions SPECDOC
Expand Up @@ -256,6 +256,53 @@ Requesting /forums/3/posts/3/comments/1 using DELETE
- should call destroy on the found comment
- should redirect to the comments list

CommentsController without stubs responding to GET index
- should expose all comments as @comments

CommentsController without stubs responding to GET index with mime type of xml
- should render all comments as xml

CommentsController without stubs responding to GET show
- should expose the requested comment as @comment

CommentsController without stubs responding to GET show with mime type of xml
- should render the requested comment as xml

CommentsController without stubs responding to GET new
- should expose a new comment as @comment

CommentsController without stubs responding to GET edit
- should expose the requested comment as @comment

CommentsController without stubs responding to POST create with valid params
- should create a comment
- should expose the newly created comment as @comment
- should be resource_saved?
- should redirect to the created comment

CommentsController without stubs responding to POST create with invalid params
- should not create a comment
- should expose a newly created but unsaved comment as @comment
- should not be resource_saved?
- should re-render the 'new' template

CommentsController without stubs responding to PUT udpate with valid params
- should update the requested comment
- should not contain errors on comment
- should be resource_saved?
- should expose the requested comment as @comment
- should redirect to the comment

CommentsController without stubs responding to PUT udpate with invalid params
- should fail to update the requested comment
- should not be resource_saved?
- should expose the requested comment as @comment
- should re-render the 'edit' template

CommentsController without stubs responding to DELETE destroy
- should delete the requested comment
- should redirect to the comments list

Routing shortcuts for ForumPosts (forums/2/posts/1) should map
- resources_path to /forums/2/posts
- resource_path to /forums/2/posts/1
Expand Down Expand Up @@ -575,6 +622,25 @@ Requesting /forums/2/owner using DELETE
- should set the flash notice
- should redirect to forums/2

CommentsController#resource_saved Comment.new(<invalid attrs>)
- should not be resource saved

CommentsController#resource_saved Comment.new(<invalid attrs>).save
- should not be resource saved

CommentsController#resource_saved Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>)
- should be resource saved

CommentsController#resource_saved Comment.find(<id>)
- should be resource saved
- .save should be saved

CommentsController#resource_saved Comment.find(<id>) then update_attributes(<invalid attrs>)
- should not be resource saved

CommentsController#resource_saved Comment.find(<id>) then update_attributes(<new valid attrs>)
- should be resource saved

Routing shortcuts for Tags should map
- resources_path to /tags
- resource_path to /tags/2
Expand Down Expand Up @@ -768,6 +834,18 @@ UsersController handling PUT /users/dave
UsersController handling DELETE /users/dave
- should be unknown action

(re: saved?) Comment.new(<invalid attrs>)
- should not be validation attempted
- should not be saved

(re: saved?) Comment.new(<invalid attrs>).save
- should be validation attempted
- should not be saved

(re: saved?) Comment.new(<invalid attrs>).save then update_attributes(<valid attrs>)
- should be validation attempted
- should be saved

ActionView with resources_controller Helper
- should forward #resource_name to controller
- should forward #resources_name to controller
Expand All @@ -792,12 +870,6 @@ Helper#form_for_resource (when resource is existing record)
Helper#remote_form_for_resource (when resource is existing record)
- should call remote_form_for with update form options

ResourcesController.load_enclosing_resources_filter_exists? when :find_filter defined
- should call :find_filter with :load_enclosing_resources

ResourcesController.load_enclosing_resources_filter_exists? when :find_filter not defined
- should call :filter_chain

#load_enclosing_resources for resources_controller_for :tags (when route_enclosing_names is [['users', false]])
- should call load_wildcard once
- should call Specification.new('user', :singleton => false, :as => nil)
Expand Down Expand Up @@ -839,6 +911,12 @@ ResourcesController.load_enclosing_resources_filter_exists? when :find_filter no
- should call load_enclosing_resource_from_specification with user spec, then load_wildcard once with 'taggable'
- should call Specification.new with ('comment', :singleton => false, :as => 'taggable')

ResourcesController.load_enclosing_resources_filter_exists? when :find_filter defined
- should call :find_filter with :load_enclosing_resources

ResourcesController.load_enclosing_resources_filter_exists? when :find_filter not defined
- should call :filter_chain

ResourcesController (in general)
- nested_in :foo, :polymorphic => true, :class => User should raise argument error (no options or block with polymorphic)
- resources_controller_for :forums, :in => [:user, '*', '*', :comment] should raise argument error (no multiple wildcards in a row)
Expand All @@ -849,6 +927,9 @@ ResourcesController#enclosing_resource_name
A controller's resource_service
- may be explicitly set with #resource_service=

deprecated methods
- #save_resource should send resource.save

#route_enclosing_names TagsController for named_route:
- :tags should be []
- :new_tag should be []
Expand All @@ -870,6 +951,6 @@ A controller's resource_service
#route_enclosing_names Admin::Superduper::ForumsController for named_route:
- :admin_superduper_forums should be []

Finished in 5.097926 seconds
Finished in 8.293483 seconds

593 examples, 0 failures
632 examples, 0 failures
5 changes: 2 additions & 3 deletions garlic_example.rb
Expand Up @@ -23,7 +23,7 @@
target '2.0-stable', :branch => 'origin/2-0-stable'
target '2.1-stable', :branch => 'origin/2-1-stable'
target '2.0.3', :tag => 'v2.0.3'
target '2.1.0', :tag => 'v2.1.0'
target '2.1.1', :tag => 'v2.1.1'

all_targets do
prepare do
Expand All @@ -36,8 +36,7 @@

run do
cd "vendor/plugins/resources_controller" do
sh "rake spec:rcov:verify"
sh "rake spec:generate"
sh "rake spec:rcov:verify && rake spec:generate"
end
end
end
Expand Down
4 changes: 3 additions & 1 deletion init.rb
@@ -1,3 +1,5 @@
require 'ardes/resources_controller'
ActionController::Base.extend Ardes::ResourcesController

ActionController::Base.extend Ardes::ResourcesController
require 'ardes/active_record/saved'
ActiveRecord::Base.send :include, Ardes::ActiveRecord::Saved
15 changes: 15 additions & 0 deletions lib/ardes/active_record/saved.rb
@@ -0,0 +1,15 @@
module Ardes
module ActiveRecord
module Saved
# returns true if this record is not new, and has no errors
def saved?
!new_record? && (@errors.nil? || errors.empty?)
end

# returns true if this instance has had validation (maybe via save) attempted
def validation_attempted?
!@errors.nil?
end
end
end
end
17 changes: 9 additions & 8 deletions lib/ardes/resources_controller.rb
Expand Up @@ -665,18 +665,19 @@ def enclosing_collection_resources
@enclosing_collection_resources ||= []
end

# Returns self.resource.save and caches the result for future calls.
# This is useful when you want to know outside of an action whether the resource was saved.
#
# Pass true to ignore the cached value
def resource_saved?(reload = false)
save_resource if reload || @resource_saved.nil?
@resource_saved
# Has the resource been saved successfully?
# If the record has not had validation attempted, it is saved.
# Returns true if the record is not new, and there are no errors
def resource_saved?
resource.save unless resource.validation_attempted?
resource.saved?
end

# DEPRECATED: just use resource.save
def save_resource
@resource_saved = resource.save
resource.save
end
deprecate :save_resource => 'Use resource.save'

private
# returns the route that was used to invoke this controller and current action. The path is found first from params[:resource_path]
Expand Down
9 changes: 4 additions & 5 deletions lib/ardes/resources_controller/actions.rb
Expand Up @@ -93,9 +93,9 @@ def edit
# POST /events.xml
def create
self.resource = new_resource

respond_to do |format|
if resource_saved?
if resource.save
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully created."
redirect_to resource_url
Expand All @@ -114,10 +114,9 @@ def create
# PUT /events/1.xml
def update
self.resource = find_resource
resource.attributes = params[resource_name]


respond_to do |format|
if resource_saved?
if resource.update_attributes(params[resource_name])
format.html do
flash[:notice] = "#{resource_name.humanize} was successfully updated."
redirect_to resource_url
Expand Down
3 changes: 2 additions & 1 deletion spec/app.rb
Expand Up @@ -156,12 +156,13 @@ class Post < ActiveRecord::Base
end

class Comment < ActiveRecord::Base
validates_presence_of :user, :post

belongs_to :user
belongs_to :post
has_many :tags, :as => :taggable
end


##############
# Controllers
##############
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/addresses_controller_spec.rb
Expand Up @@ -309,7 +309,7 @@ def do_update
end

it "should update the found address" do
@address.should_receive(:attributes=)
@address.should_receive(:update_attributes).and_return(true)
do_update
end

Expand Down
6 changes: 3 additions & 3 deletions spec/controllers/admin_forums_controller_spec.rb
Expand Up @@ -522,7 +522,7 @@ def do_update
end

it "should update the found forum" do
@mock_forum.should_receive(:attributes=)
@mock_forum.should_receive(:update_attributes).and_return(true)
do_update
assigns(:forum).should == @mock_forum
end
Expand Down Expand Up @@ -558,7 +558,7 @@ def do_update
end

it "should update the found forum" do
@mock_forum.should_receive(:attributes=)
@mock_forum.should_receive(:update_attributes).and_return(true)
do_update
assigns(:forum).should == @mock_forum
end
Expand All @@ -579,7 +579,7 @@ def do_update
end

it "should render edit.rjs, on unsuccessful save" do
@mock_forum.stub!(:save).and_return(false)
@mock_forum.stub!(:update_attributes).and_return(false)
do_update
response.should render_template('edit')
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/comments_controller_spec.rb
Expand Up @@ -77,7 +77,7 @@ def setup_mocks
before(:each) do
@forum = Forum.create
@post = Post.create :forum_id => @forum.id
@comment = Comment.create :post_id => @post.id
@comment = Comment.create :post_id => @post.id, :user => User.create
@other_post = Post.create :forum_id => @forum.id
@other_comment = Comment.create :post_id => @other_post.id

Expand Down Expand Up @@ -342,7 +342,7 @@ def do_update
end

it "should update the found comment" do
@comment.should_receive(:attributes=)
@comment.should_receive(:update_attributes).and_return(true)
do_update
end

Expand Down

0 comments on commit ceaf1b9

Please sign in to comment.