Permalink
Browse files

Made method_required? respect the :if conditional on validates_presen…

…ce_of, and squashing a bug related to this causing all fields become required if just one field is required (issue #29). Also fixed an old incorrect string interpolation in the spec - RSpec matchers was smart enough to handle that it seems.

Signed-off-by: Justin French <justin@indent.com.au>
  • Loading branch information...
1 parent 542d308 commit 67a799f5721070da7cac3c2b9afb100d15a75426 @grimen grimen committed with Sep 3, 2009
Showing with 37 additions and 15 deletions.
  1. +5 −0 README.textile
  2. +6 −4 lib/formtastic.rb
  3. +25 −10 spec/formtastic_spec.rb
  4. +1 −1 spec/test_helper.rb
View
@@ -494,6 +494,11 @@ A proof-of-concept (very much a work-in-progress) stylesheet is provided which y
<%= stylesheet_link_tag "formtastic_changes" %>
</pre>
+h2. Got TextMate?
+
+Well...there's a TextMate-bundle in town, dedicated to make usage of Formtastic in the TextMate editor even more of a breeze:
+
+"Formtastic.tmbundle":http://github.com/grimen/formtastic_tmbundle
h2. Contributors
View
@@ -463,11 +463,13 @@ def save_or_create_button_text(prefix='Submit') #:nodoc:
# configuration option @@all_fields_required_by_default is used.
#
def method_required?(attribute) #:nodoc:
- if @object && @object.class.respond_to?(:reflect_on_all_validations)
+ if @object && @object.class.respond_to?(:reflect_on_validations_for)
attribute_sym = attribute.to_s.sub(/_id$/, '').to_sym
-
- @object.class.reflect_on_all_validations.any? do |validation|
- validation.macro == :validates_presence_of && validation.name == attribute_sym
+
+ @object.class.reflect_on_validations_for(attribute_sym).any? do |validation|
+ validation.macro == :validates_presence_of &&
+ validation.name == attribute_sym &&
+ (validation.options.present? ? (validation.options[:if] == true) : true)
end
else
@@all_fields_required_by_default
View
@@ -78,9 +78,9 @@ class Author; end
Author.stub!(:find).and_return([@fred, @bob])
Author.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
Author.stub!(:human_name).and_return('Author')
- Author.stub!(:reflect_on_all_validations).and_return([])
+ Author.stub!(:reflect_on_validations_for).and_return([]) # new
Author.stub!(:reflect_on_association).and_return { |column_name| mock('reflection', :options => {}, :klass => Post, :macro => :has_many) if column_name == :posts }
-
+
# Sometimes we need a mock @post object and some Authors for belongs_to
@new_post = mock('post')
@new_post.stub!(:class).and_return(Post)
@@ -105,6 +105,7 @@ class Author; end
Post.stub!(:human_attribute_name).and_return { |column_name| column_name.humanize }
Post.stub!(:human_name).and_return('Post')
Post.stub!(:reflect_on_all_validations).and_return([])
+ Post.stub!(:reflect_on_validations_for).and_return([])
Post.stub!(:reflect_on_association).and_return do |column_name|
case column_name
when :author, :author_status
@@ -512,28 +513,42 @@ def custom(arg1, arg2, options = {})
describe 'and the validation reflection plugin is available' do
before do
- @new_post.class.stub!(:method_defined?).with(:reflect_on_all_validations).and_return(true)
+ @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true)
end
describe 'and validates_presence_of was called for the method' do
- before do
- @new_post.class.should_receive(:reflect_on_all_validations).and_return([
- mock('MacroReflection', :macro => :validates_presence_of, :name => :title)
+ it 'should be required' do
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil)
+ ])
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => true})
])
- end
- it 'should be required' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title))
+ concat(builder.input(:body))
end
output_buffer.should have_tag('form li.required')
output_buffer.should_not have_tag('form li.optional')
end
+
+ it 'should be not be required if the optional :if condition is not satisifed' do
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
+ mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => {:if => false})
+ ])
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:body))
+ end
+ output_buffer.should have_tag('form li.optional')
+ output_buffer.should_not have_tag('form li.required')
+ end
end
describe 'and validates_presence_of was not called for the method' do
before do
- @new_post.class.should_receive(:reflect_on_all_validations).and_return([])
+ @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([])
end
it 'should not be required' do
@@ -1123,7 +1138,7 @@ def custom(arg1, arg2, options = {})
end
it "should have a #{type} class on the wrapper" do
- output_buffer.should have_tag('form li.#{type}') # << # FIXME: This seems to be incorrect, but passes. Eh? (grimen)
+ output_buffer.should have_tag("form li.#{type}")
end
it 'should have a post_title_input id on the wrapper' do
View
@@ -8,7 +8,7 @@
gem 'rspec', '>= 1.2.6'
gem 'rspec-rails', '>= 1.2.6'
gem 'rspec_hpricot_matchers', '>= 1.0.0'
-gem 'hpricot', '>= 0.8.1'
+gem 'hpricot', '>= 0.6.1'
require 'spec'
require 'activesupport'

0 comments on commit 67a799f

Please sign in to comment.