Created
August 23, 2010 23:02
-
-
Save jfirebaugh/546523 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6ff2d333271b104de652603ce8049c80ccb08141 Mon Sep 17 00:00:00 2001 | |
From: John Firebaugh <[email protected]> | |
Date: Mon, 23 Aug 2010 14:28:16 -0700 | |
Subject: [PATCH] Prefer persisted? to new_record?, so any ActiveModel-compliant object can be used. | |
--- | |
lib/formtastic.rb | 10 +++++++--- | |
spec/commit_button_spec.rb | 39 ++++++++++++++++++++++++++++++++++++++- | |
2 files changed, 45 insertions(+), 4 deletions(-) | |
diff --git a/lib/formtastic.rb b/lib/formtastic.rb | |
index 92cd127..96da952 100644 | |
--- a/lib/formtastic.rb | |
+++ b/lib/formtastic.rb | |
@@ -322,9 +322,13 @@ module Formtastic #:nodoc: | |
options = args.extract_options! | |
text = options.delete(:label) || args.shift | |
- if @object && @object.respond_to?(:new_record?) | |
- key = @object.new_record? ? :create : :update | |
- | |
+ if @object && (@object.respond_to?(:persisted?) || @object.respond_to?(:new_record?)) | |
+ if @object.respond_to?(:persisted?) # ActiveModel | |
+ key = @object.persisted? ? :update : :create | |
+ else # Rails 2 | |
+ key = @object.new_record? ? :create : :update | |
+ end | |
+ | |
# Deal with some complications with ActiveRecord::Base.human_name and two name models (eg UserPost) | |
# ActiveRecord::Base.human_name falls back to ActiveRecord::Base.name.humanize ("Userpost") | |
# if there's no i18n, which is pretty crappy. In this circumstance we want to detect this | |
diff --git a/spec/commit_button_spec.rb b/spec/commit_button_spec.rb | |
index 0053118..7cac899 100644 | |
--- a/spec/commit_button_spec.rb | |
+++ b/spec/commit_button_spec.rb | |
@@ -10,6 +10,43 @@ describe 'SemanticFormBuilder#commit_button' do | |
mock_everything | |
end | |
+ describe 'when the object responds to :new_record? (Rails 2)' do | |
+ | |
+ before do | |
+ @new_post.stub(:respond_to?).with(:to_model).and_return("X") | |
+ @new_post.stub(:respond_to?).with(:persisted?).and_return(false) | |
+ @new_post.stub(:respond_to?).with(:new_record?).and_return(true) | |
+ end | |
+ | |
+ it 'should call :new_record?' do | |
+ @new_post.should_receive(:new_record?) | |
+ @new_post.should_not_receive(:persisted?) | |
+ semantic_form_for(@new_post) do |builder| | |
+ concat(builder.commit_button) | |
+ end | |
+ end | |
+ | |
+ end | |
+ | |
+ describe 'when the object responds to :persisted? (ActiveModel)' do | |
+ | |
+ before do | |
+ @new_post.stub(:respond_to?).with(:to_model).and_return("X") | |
+ @new_post.stub(:respond_to?).with(:persisted?).and_return(true) | |
+ @new_post.stub(:respond_to?).with(:new_record?).and_return(false) | |
+ end | |
+ | |
+ it 'should call :persisted?' do | |
+ @new_post.should_receive(:persisted?) | |
+ @new_post.should_not_receive(:new_record?) | |
+ semantic_form_for(@new_post) do |builder| | |
+ concat(builder.commit_button) | |
+ end | |
+ end | |
+ | |
+ end | |
+ | |
+ | |
describe 'when used on any record' do | |
before do | |
@@ -287,7 +324,7 @@ describe 'SemanticFormBuilder#commit_button' do | |
# Existing record | |
describe 'when used on an existing record' do | |
before do | |
- @new_post.stub!(:new_record?).and_return(false) | |
+ @new_post.stub!(:persisted?).and_return(true) | |
end | |
describe 'when explicit label is provided' do | |
-- | |
1.7.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment