From e39e0e54cbea9e3da1b0c24765fe72f1207670a2 Mon Sep 17 00:00:00 2001 From: Vasiliy Ermolovich Date: Tue, 3 Jan 2012 21:10:50 +0300 Subject: [PATCH] handle validates_length_of :is option --- lib/simple_form/components/maxlength.rb | 2 +- test/form_builder/error_notification_test.rb | 1 + test/inputs/string_input_test.rb | 5 +++++ test/support/models.rb | 1 + test/test_helper.rb | 1 + 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/components/maxlength.rb b/lib/simple_form/components/maxlength.rb index eb01748b..f02b6ade 100644 --- a/lib/simple_form/components/maxlength.rb +++ b/lib/simple_form/components/maxlength.rb @@ -12,7 +12,7 @@ module SimpleForm def maximum_length_from_validation if options[:maxlength] == true if (length_validator = find_length_validator) && !length_validator.options[:tokenizer] - length_validator.options[:maximum] + length_validator.options[:is] || length_validator.options[:maximum] end else options[:maxlength] diff --git a/test/form_builder/error_notification_test.rb b/test/form_builder/error_notification_test.rb index 5a24fc84..2fb1cd36 100644 --- a/test/form_builder/error_notification_test.rb +++ b/test/form_builder/error_notification_test.rb @@ -11,6 +11,7 @@ class ErrorNotificationTest < ActionView::TestCase test 'error notification is not generated when the object has no error' do assert @validating_user.valid? + with_error_notification_for @validating_user assert_no_select 'p.error_notification' end diff --git a/test/inputs/string_input_test.rb b/test/inputs/string_input_test.rb index fbce4479..105755fc 100644 --- a/test/inputs/string_input_test.rb +++ b/test/inputs/string_input_test.rb @@ -52,6 +52,11 @@ class StringInputTest < ActionView::TestCase assert_no_select 'input.string[maxlength]' end + test 'input should get maxlength from validation when :is option present' do + with_input_for @validating_user, :home_picture, :string + assert_select 'input.string[maxlength=12]' + end + test 'input should not generate placeholder by default' do with_input_for @user, :name, :string assert_no_select 'input[placeholder]' diff --git a/test/support/models.rb b/test/support/models.rb index 1bdf3c75..cd8816f3 100644 --- a/test/support/models.rb +++ b/test/support/models.rb @@ -156,6 +156,7 @@ class ValidatingUser < User validates_length_of :name, :maximum => 25 validates_length_of :description, :maximum => 50 validates_length_of :action, :maximum => 10, :tokenizer => lambda { |str| str.scan(/\w+/) } + validates_length_of :home_picture, :is => 12 def min_amount 10 diff --git a/test/test_helper.rb b/test/test_helper.rb index b36d5b06..b762ab03 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -62,6 +62,7 @@ class ActionView::TestCase :id => 1, :name => 'New in Simple Form!', :description => 'Hello!', + :home_picture => 'Home picture', :created_at => Time.now, :age => 19, :amount => 15,