Tidy up previous commit.

This commit is contained in:
José Valim 2010-05-17 11:08:47 +02:00
parent 938166762f
commit 303fb1fb1f
3 changed files with 16 additions and 6 deletions

View File

@ -1,3 +1,6 @@
* enhancements
* added support to presence validation to check if attribute is required or not (by github.com/gcirne)
== 1.1.3
* deprecation

View File

@ -49,12 +49,13 @@ module SimpleForm
end
def attribute_required?
klass = object.class
if defined? klass.validators_on
return options[:required] unless options[:required].nil?
return klass.validators_on(attribute_name).map {|validator| validator.kind}.include?(:presence)
if options.key?(:required)
options[:required]
elsif defined?(object.class.validators_on)
object.class.validators_on(attribute_name).any? { |v| v.kind == :presence }
else
true
end
options[:required] != false
end
def required_class

View File

@ -229,6 +229,7 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'span.error#cool', "can't be blank"
end
# REQUIRED AND PRESENCE VALIDATION
test 'builder input should obtain required from ActiveModel::Validations when it is included' do
with_form_for @validating_user, :name
assert_select 'input.required#validating_user_name'
@ -347,9 +348,14 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'label.string[for=user_name]', /Name/
end
test 'builder should add a required class to label if the attribute is required' do
with_label_for @validating_user, :name
assert_select 'label.string[for=validating_user_name]', /Name/
end
test 'builder should allow passing options to label tag' do
with_label_for @user, :name, :label => 'My label', :id => 'name_label'
assert_select 'label.string.required#name_label', /My label/
assert_select 'label.string#name_label', /My label/
end
test 'builder should fallback to default label when string is given' do