Skip to content

Commit

Permalink
API change: save_resource and resource_saved? deprecated
Browse files Browse the repository at this point in the history
  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.
  • Loading branch information
ianwhite committed Sep 8, 2008
1 parent 633fe45 commit 2f49687
Show file tree
Hide file tree
Showing 21 changed files with 195 additions and 43 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
@@ -1,3 +1,11 @@
* 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.

* 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 @@ -13,6 +13,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
40 changes: 32 additions & 8 deletions SPECDOC
Expand Up @@ -768,6 +768,26 @@ UsersController handling PUT /users/dave
UsersController handling DELETE /users/dave
- should be unknown action

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

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

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

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

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

ActionView with resources_controller Helper
- should forward #resource_name to controller
- should forward #resources_name to controller
Expand All @@ -792,12 +812,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 +853,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 +869,10 @@ ResourcesController#enclosing_resource_name
A controller's resource_service
- may be explicitly set with #resource_service=

deprecated methods
- #save_resource should send resource.save
- #resource_saved? should send resource.saved?

#route_enclosing_names TagsController for named_route:
- :tags should be []
- :new_tag should be []
Expand All @@ -870,6 +894,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.867688 seconds

593 examples, 0 failures
605 examples, 0 failures
3 changes: 1 addition & 2 deletions garlic_example.rb
Expand Up @@ -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
6 changes: 4 additions & 2 deletions init.rb
@@ -1,3 +1,5 @@
require 'ardes/resources_controller'
require 'ardes/active_record/saved'
ActiveRecord::Base.send :include, Ardes::ActiveRecord::Saved

ActionController::Base.extend Ardes::ResourcesController
require 'ardes/resources_controller'
ActionController::Base.extend Ardes::ResourcesController
57 changes: 57 additions & 0 deletions lib/ardes/active_record/saved.rb
@@ -0,0 +1,57 @@
module Ardes#:nodoc:
module ActiveRecord#:nodoc:
# Small mixin which lets you find the result of the last save on an active record
#
# Example usage:
#
# foo = Foo.new
#
# foo.attempted_save? # => false
# foo.saved? # => nil
#
# foo.save # => true
# foo.attempted_save? # => true
# foo.saved? # => true
#
# foo.update_attributes(:invalid => true) # => false
# foo.attempted_save? # => true
# foo.saved? # => false
#
# foo.reload
# foo.attempted_save? # => false
# foo.saved? # => false
module Saved
def self.included(base)
base.class_eval do
def save_with_saved(*args)
@_saved = save_without_saved(*args)
end
alias_method_chain :save, :saved

def reload_with_saved(*args)
@_saved = nil
reload_without_saved(*args)
end
alias_method_chain :reload, :saved

# returns:
# nil - if the record has had no save attempt
# true - if the record was saved successfuly
# false - if the record was saved unsuccesfuly
def saved?
@_saved
end

# returns:
# true - if the record has had a save attempt
# false - if the record has not had a save attempt
# (this is simply a predicate method for checking saved?.nil?)
def attempted_save?
!@_saved.nil?
end
end
end
end
end
end

11 changes: 8 additions & 3 deletions lib/ardes/resources_controller.rb
Expand Up @@ -665,18 +665,23 @@ def enclosing_collection_resources
@enclosing_collection_resources ||= []
end

# DEPRECATED: just use resource.saved?
#
# 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
resource.save unless resource.attempted_save?
resource.saved?
end
deprecate :resource_saved? => 'Use resource.saved?'

# 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
2 changes: 1 addition & 1 deletion spec/controllers/forum_posts_controller_spec.rb
Expand Up @@ -395,7 +395,7 @@ def do_update
end

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

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

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

it "should update the found forum" do
@mock_forum.should_receive(:attributes=)
@mock_forum.should_receive(:update_attributes)
do_update
assigns(:forum).should == @mock_forum
end
Expand All @@ -656,7 +656,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
5 changes: 2 additions & 3 deletions spec/controllers/infos_controller_spec.rb
Expand Up @@ -52,10 +52,9 @@ def setup_mocks
end

it "PUT /account/info should be successful" do
@info.stub!(:attributes=)
@info.stub!(:save)
@info.stub!(:update_attributes).and_return(true)
put :update
response.should be_success
response.should be_redirect
end

it "GET /account/info/new should raise UnknownAction" do
Expand Down
5 changes: 2 additions & 3 deletions spec/controllers/owners_controller_spec.rb
Expand Up @@ -220,8 +220,7 @@ def do_post

before(:each) do
setup_mocks
@owner.stub!(:save).and_return(true)
@owner.stub!(:attributes=)
@owner.stub!(:update_attributes).and_return(true)
end

def do_update
Expand All @@ -239,7 +238,7 @@ def do_update
end

it "should update the owner" do
@owner.should_receive(:attributes=).with('name' => 'Fred')
@owner.should_receive(:update_attributes).with('name' => 'Fred')
do_update
end

Expand Down
Expand Up @@ -71,7 +71,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!
@tag = Tag.create :taggable_id => @comment.id, :taggable_type => 'Comment'
@other_comment = Comment.create :post_id => @forum.id
@other_tag = Tag.create :taggable_id => @other_comment.id, :taggable_type => 'Comment'
Expand Down
6 changes: 2 additions & 4 deletions spec/controllers/users_controller_spec.rb
Expand Up @@ -235,14 +235,12 @@ def do_get
end

def put_with_successful_update
@user.should_receive(:attributes=).once.ordered
@user.should_receive(:save).once.ordered.and_return(true)
@user.should_receive(:update_attributes).and_return(true)
put :update, :id => "dave"
end

def put_with_failed_update
@user.should_receive(:attributes=).once.ordered
@user.should_receive(:save).once.ordered.and_return(false)
@user.should_receive(:update_attributes).and_return(false)
put :update, :id => "dave"
end

Expand Down

0 comments on commit 2f49687

Please sign in to comment.