diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 4ea546cd1..f293d52b0 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -83,7 +83,7 @@ def input(attribute_name, options={}) if klass = self.class.mappings[input_type] klass.new(self).render else - const_get(:"#{input_type.to_s.camelize}Input").new(self).render + self.class.const_get(:"#{input_type.to_s.camelize}Input").new(self).render end end alias :attribute :input diff --git a/lib/simple_form/inputs/text_field_input.rb b/lib/simple_form/inputs/text_field_input.rb index d06a65fa2..bc8aeb38a 100644 --- a/lib/simple_form/inputs/text_field_input.rb +++ b/lib/simple_form/inputs/text_field_input.rb @@ -8,7 +8,7 @@ def input def input_html_options input_options = super - input_options[:max_length] ||= column.limit if column + input_options[:maxlength] ||= column.limit if column input_options end end diff --git a/test/action_view_extensions/builder_test.rb b/test/action_view_extensions/builder_test.rb index ac182c167..a45b9b207 100644 --- a/test/action_view_extensions/builder_test.rb +++ b/test/action_view_extensions/builder_test.rb @@ -77,8 +77,8 @@ class BuilderTest < ActionView::TestCase concat f.collection_check_boxes :tag_ids, collection, :id, :name end - assert_select 'form label.collection_check_box[for=user_tag_ids_1]', 'Tag 1' - assert_select 'form label.collection_check_box[for=user_tag_ids_2]', 'Tag 2' + assert_select 'form label.collection_check_boxes[for=user_tag_ids_1]', 'Tag 1' + assert_select 'form label.collection_check_boxes[for=user_tag_ids_2]', 'Tag 2' end test 'collection check box accepts selected values as :checked option' do @@ -150,15 +150,15 @@ class BuilderTest < ActionView::TestCase collection = [Tag.new(1, 'Tag 1'), Tag.new(2, 'Tag 2')] form_for @user do |f| f.fields_for :post do |p| - concat p.collection_check_box :tag_ids, collection, :id, :name + concat p.collection_check_boxes :tag_ids, collection, :id, :name end end assert_select 'form input#user_post_tag_ids_1[type=checkbox][value=1]' assert_select 'form input#user_post_tag_ids_2[type=checkbox][value=2]' - assert_select 'form label.collection_check_box[for=user_post_tag_ids_1]', 'Tag 1' - assert_select 'form label.collection_check_box[for=user_post_tag_ids_2]', 'Tag 2' + assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_1]', 'Tag 1' + assert_select 'form label.collection_check_boxes[for=user_post_tag_ids_2]', 'Tag 2' end # SIMPLE FIELDS diff --git a/test/components/error_test.rb b/test/components/error_test.rb index 0a745a174..941563f1b 100644 --- a/test/components/error_test.rb +++ b/test/components/error_test.rb @@ -9,27 +9,19 @@ def with_error_for(object, attribute_name, type, options={}, &block) f.input_type = type f.options = options - concat(SimpleForm::Inputs::Base.new(f).error) + concat(SimpleForm::Inputs::Base.new(f).error.to_s) end end - # test 'error should not generate content for hidden fields' do - # with_error_for @user, :name, :hidden do |error| - # assert error.call.blank? - # end - # end - # - # test 'error should not generate content for attribute without errors' do - # with_error_for @user, :active, :boolean do |error| - # assert error.call.blank? - # end - # end - # - # test 'error should not generate messages when object is not present' do - # with_error_for :project, :name, :string do |error| - # assert error.call.blank? - # end - # end + test 'error should not generate content for attribute without errors' do + with_error_for @user, :active, :boolean + assert_no_select 'span.error' + end + + test 'error should not generate messages when object is not present' do + with_error_for :project, :name, :string + assert_no_select 'span.error' + end test 'error should generate messages for attribute with single error' do with_error_for @user, :name, :string diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb index cab67b6b2..d8e97ca37 100644 --- a/test/components/hint_test.rb +++ b/test/components/hint_test.rb @@ -9,17 +9,14 @@ def with_hint_for(object, attribute_name, type, options={}, &block) f.input_type = type f.options = options - concat(SimpleForm::Inputs::Base.new(f).hint) + concat(SimpleForm::Inputs::Base.new(f).hint.to_s) end end - # test 'hint should not be generated by default' do - # assert with_hint_for(@user, :name, :string).blank? - # end - # - # test 'hint should not be generated for hidden fields' do - # assert with_hint_for(@user, :name, :hidden, :hint => 'Use with care...').blank? - # end + test 'hint should not be generated by default' do + with_hint_for @user, :name, :string + assert_no_select 'span.hint' + end test 'hint should be generated with input text' do with_hint_for @user, :name, :string, :hint => 'Use with care...' @@ -27,7 +24,7 @@ def with_hint_for(object, attribute_name, type, options={}, &block) end test 'hint uses the current component tag set' do - swap SimpleForm, :component_tag => :p do + swap SimpleForm, :hint_tag => :p do with_hint_for @user, :name, :string, :hint => 'Use with care...' assert_select 'p.hint', 'Use with care...' end diff --git a/test/components/label_test.rb b/test/components/label_test.rb index 2f8acbad0..fca05f430 100644 --- a/test/components/label_test.rb +++ b/test/components/label_test.rb @@ -17,13 +17,6 @@ def with_label_for(object, attribute_name, type, options={}) end end - # Fix me! - # test 'label should not be generated for hidden inputs' do - # with_label_for @user, :name, :hidden do |label| - # assert label.call.blank? - # end - # end - test 'label should generate a default humanized description' do with_label_for @user, :name, :string assert_select 'label[for=user_name]', /Name/ @@ -174,19 +167,4 @@ def with_label_for(object, attribute_name, type, options={}) with_label_for :project, :description, :string, :required => false assert_no_select 'label.required[for=project_description]' end - - test 'label should point to first option when date input type' do - with_label_for :project, :created_at, :date - assert_select 'label[for=project_created_at_1i]' - end - - test 'label should point to first option when datetime input type' do - with_label_for :project, :created_at, :datetime - assert_select 'label[for=project_created_at_1i]' - end - - test 'label should point to first option when time input type' do - with_label_for :project, :created_at, :time - assert_select 'label[for=project_created_at_4i]' - end end diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb index 57d82b155..88591e5cf 100644 --- a/test/form_builder_test.rb +++ b/test/form_builder_test.rb @@ -38,6 +38,15 @@ def with_association_for(object, *args) end end + # All + test 'nested simple fields should yields an instance of FormBuilder' do + simple_form_for :user do |f| + f.simple_fields_for :posts do |posts_form| + assert posts_form.instance_of?(SimpleForm::FormBuilder) + end + end + end + # INPUT TYPES test 'builder should generate text fields for string columns' do with_form_for @user, :name @@ -209,19 +218,11 @@ def with_association_for(object, *args) test 'builder allows wrapper tag to be given on demand' do simple_form_for @user do |f| - concat f.input :name, :wrapper => :b + concat f.input :name, :wrapper_tag => :b end assert_select 'form b.required.string' end - test 'nested simple fields should yields an instance of FormBuilder' do - simple_form_for :user do |f| - f.simple_fields_for :posts do |posts_form| - assert posts_form.instance_of?(SimpleForm::FormBuilder) - end - end - end - # WITHOUT OBJECT test 'builder should generate properly when object is not present' do with_form_for :project, :name diff --git a/test/components/input_test.rb b/test/inputs_test.rb similarity index 91% rename from test/components/input_test.rb rename to test/inputs_test.rb index 948e00e61..0740b3f5e 100644 --- a/test/components/input_test.rb +++ b/test/inputs_test.rb @@ -3,27 +3,16 @@ class InputTest < ActionView::TestCase setup do - SimpleForm::Inputs::Base.reset_i18n_cache :boolean_collection + SimpleForm::Inputs::CollectionInput.reset_i18n_cache :boolean_collection end def with_input_for(object, attribute_name, type, options={}) simple_form_for object do |f| - f.attribute_name = attribute_name - f.column = object.column_for_attribute(attribute_name) if object.respond_to?(:column_for_attribute) - f.input_type = type - f.options = options - - klass = SimpleForm::FormBuilder.mappings[type] - klass ||= SimpleForm::Inputs.const_get(:"#{type.to_s.camelize}Input") - concat(klass.new(f).input) + concat f.input(attribute_name, options.merge(:as => type)) end end - test 'input should map text field to string attribute' do - with_input_for @user, :name, :string - assert_select 'input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]' - end - + # ALL test 'input should generate css class based on default input type' do with_input_for @user, :name, :string assert_select 'input.string' @@ -36,17 +25,29 @@ def with_input_for(object, attribute_name, type, options={}) with_input_for @user, :created_at, :datetime assert_select 'select.datetime' end - - test 'input should allow passing options to text field' do + + test 'input should allow passing options to input field' do with_input_for @user, :name, :string, :input_html => { :class => 'my_input', :id => 'my_input' } assert_select 'input#my_input.my_input' end - - test 'input should generate a text area for text attributes' do - with_input_for @user, :description, :text - assert_select 'textarea.text#user_description' + + test 'input should be required by default' do + with_input_for @user, :name, :string + assert_select 'input.required#user_name' end + test 'input should allow disabling required' do + with_input_for @user, :name, :string, :required => false + assert_no_select 'input.required' + assert_select 'input.optional#user_name' + end + + # TextFieldInput + test 'input should map text field to string attribute' do + with_input_for @user, :name, :string + assert_select 'input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]' + end + test 'input should generate an integer text field for integer attributes ' do with_input_for @user, :age, :integer assert_select 'input.integer#user_age' @@ -61,7 +62,23 @@ def with_input_for(object, attribute_name, type, options={}) with_input_for @user, :age, :decimal assert_select 'input.decimal#user_age' end + + test 'input should get options from column definition for string attributes' do + with_input_for @user, :name, :string + assert_select 'input.string[maxlength=100]' + end + test 'input should get options from column definition for decimal attributes' do + with_input_for @user, :credit_limit, :decimal + assert_select 'input.decimal[maxlength=15]' + end + + # MappingInput + test 'input should generate a text area for text attributes' do + with_input_for @user, :description, :text + assert_select 'textarea.text#user_description' + end + test 'input should generate a checkbox by default for boolean attributes' do with_input_for @user, :active, :boolean assert_select 'input[type=checkbox].boolean#user_active' @@ -71,18 +88,30 @@ def with_input_for(object, attribute_name, type, options={}) with_input_for @user, :password, :password assert_select 'input[type=password].password#user_password' end - + + test 'input should generate a file field' do + with_input_for @user, :name, :file + assert_select 'input#user_name[type=file]' + end + + # HiddenInput test 'input should generate a hidden field' do with_input_for @user, :name, :hidden assert_no_select 'input[type=text]' assert_select 'input#user_name[type=hidden]' end - - test 'input should generate a file field' do - with_input_for @user, :name, :file - assert_select 'input#user_name[type=file]' + + test 'hint should not be generated for hidden fields' do + with_input_for @user, :name, :hidden, :hint => 'Use with care...' + assert_no_select 'span.hint' end - + + test 'label should not be generated for hidden inputs' do + with_input_for @user, :name, :hidden + assert_no_select 'label' + end + + # PriorityInput test 'input should generate a country select field' do with_input_for @user, :country, :country assert_select 'select#user_country' @@ -96,7 +125,7 @@ def with_input_for(object, attribute_name, type, options={}) assert_select 'select option[value=][disabled=disabled]' end end - + test 'input should generate a time zone select field' do with_input_for @user, :time_zone, :time_zone assert_select 'select#user_time_zone' @@ -113,7 +142,8 @@ def with_input_for(object, attribute_name, type, options={}) with_input_for @user, :time_zone, :time_zone, :priority => /Brasilia/ assert_select 'select option[value=][disabled=disabled]' end - + + # DateTime input test 'input should generate a datetime select by default for datetime attributes' do with_input_for @user, :created_at, :datetime 1.upto(5) do |i| @@ -166,7 +196,23 @@ def with_input_for(object, attribute_name, type, options={}) assert_select 'select.time option', 'hora' assert_select 'select.time option', 'minuto' end - + + test 'label should point to first option when date input type' do + with_input_for :project, :created_at, :date + assert_select 'label[for=project_created_at_1i]' + end + + test 'label should point to first option when datetime input type' do + with_input_for :project, :created_at, :datetime + assert_select 'label[for=project_created_at_1i]' + end + + test 'label should point to first option when time input type' do + with_input_for :project, :created_at, :time + assert_select 'label[for=project_created_at_4i]' + end + + # CollectionInput test 'input should generate boolean radio buttons by default for radio types' do with_input_for @user, :active, :radio assert_select 'input[type=radio][value=true].radio#user_active_true' @@ -281,38 +327,7 @@ def with_input_for(object, attribute_name, type, options={}) assert_select 'label.collection_radio', 'CARLOS' end - test 'input should be required by default' do - with_input_for @user, :name, :string - assert_select 'input.required#user_name' - end - - test 'input should allow disabling required' do - with_input_for @user, :name, :string, :required => false - assert_no_select 'input.required' - assert_select 'input.optional#user_name' - end - - test 'input should get options from column definition for string attributes' do - with_input_for @user, :name, :string - assert_select 'input.string[maxlength=100]' - end - - test 'input should get options from column definition for decimal attributes' do - with_input_for @user, :credit_limit, :decimal - assert_select 'input.decimal[maxlength=15]' - end - - test 'input should get options from column definition for password attributes' do - with_input_for @user, :password, :password - assert_select 'input.password[maxlength=100]' - end - - test 'input should not generate options for different attributes' do - with_input_for @user, :description, :text - assert_select 'textarea' - assert_no_select 'textarea[maxlength]' - end - + # With no object test 'input should be generated properly when object is not present' do with_input_for :project, :name, :string assert_select 'input.string.required#project_name' diff --git a/test/simple_form_test.rb b/test/simple_form_test.rb index 0209d775e..662e13d96 100644 --- a/test/simple_form_test.rb +++ b/test/simple_form_test.rb @@ -1,7 +1,6 @@ require 'test_helper' class SimpleFormTest < ActiveSupport::TestCase - test 'setup block yields self' do SimpleForm.setup do |config| assert_equal SimpleForm, config