diff --git a/lib/simple_form/components/label.rb b/lib/simple_form/components/label.rb
index 5e894d19..1f6accf4 100644
--- a/lib/simple_form/components/label.rb
+++ b/lib/simple_form/components/label.rb
@@ -5,19 +5,23 @@ module SimpleForm
extend I18nCache
def self.translate_required_string
- i18n_cache :translate_required_string do
- I18n.t(:"simple_form.required.string", :default =>
- %[#{translate_required_mark} ]
- )
- end
+ i18n_cache :translate_required_string do
+ I18n.t(:"simple_form.required.string", :default =>
+ %[#{translate_required_mark} ]
+ )
end
+ end
def self.translate_required_text
- I18n.t(:"simple_form.required.text", :default => 'required')
+ i18n_cache :translate_required_text do
+ I18n.t(:"simple_form.required.text", :default => 'required')
+ end
end
def self.translate_required_mark
- I18n.t(:"simple_form.required.mark", :default => '*')
+ i18n_cache :translate_required_mark do
+ I18n.t(:"simple_form.required.mark", :default => '*')
+ end
end
def valid?
diff --git a/test/builder_extensions_test.rb b/test/action_view_extensions/builder_test.rb
similarity index 95%
rename from test/builder_extensions_test.rb
rename to test/action_view_extensions/builder_test.rb
index 3ac07dc5..56701f30 100644
--- a/test/builder_extensions_test.rb
+++ b/test/action_view_extensions/builder_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'
-class BuilderExtensionsTest < ActionView::TestCase
+class BuilderTest < ActionView::TestCase
test "collection radio accepts a collection and generate inputs from value method" do
form_for @user do |f|
concat f.collection_radio :active, [true, false], :to_s, :to_s
diff --git a/test/form_helper_test.rb b/test/action_view_extensions/form_helper_test.rb
similarity index 100%
rename from test/form_helper_test.rb
rename to test/action_view_extensions/form_helper_test.rb
diff --git a/test/components/error_test.rb b/test/components/error_test.rb
new file mode 100644
index 00000000..4e203c1d
--- /dev/null
+++ b/test/components/error_test.rb
@@ -0,0 +1,35 @@
+require 'test_helper'
+
+class ErrorTest < ActionView::TestCase
+
+ def with_error_for(attribute, type, &block)
+ simple_form_for @user do |f|
+ error = SimpleForm::Components::Error.new(f, attribute, type, {})
+ concat(error.generate)
+ yield error if block_given?
+ end
+ end
+
+ test 'error should not generate content for hidden fields' do
+ with_error_for(:name, :hidden) do |error|
+ assert error.generate.blank?
+ end
+ end
+
+ test 'error should not generate content for attribute without errors' do
+ with_error_for(:active, :boolean) do |error|
+ assert error.generate.blank?
+ end
+ end
+
+ test 'error should generate messages for attribute with single error' do
+ with_error_for(:name, :string)
+ assert_select 'span.error', "can't be blank"
+ end
+
+ test 'error should generate messages for attribute with several errors' do
+ with_error_for(:age, :numeric)
+ assert_select 'span.error', 'is not a number and must be greater than 18'
+ end
+
+end
diff --git a/test/components/hint_test.rb b/test/components/hint_test.rb
new file mode 100644
index 00000000..1cdc96ba
--- /dev/null
+++ b/test/components/hint_test.rb
@@ -0,0 +1,48 @@
+require 'test_helper'
+
+class ErrorTest < ActionView::TestCase
+
+ def with_hint_for(attribute, type, options={})
+ simple_form_for @user do |f|
+ hint = SimpleForm::Components::Hint.new(f, attribute, type, options)
+ concat(hint.generate)
+ yield hint if block_given?
+ end
+ end
+
+ test 'hint should not be generated by default' do
+ with_hint_for(:name, :string) do |hint|
+ assert hint.generate.blank?
+ end
+ end
+
+ test 'hint should not be generated for hidden fields' do
+ with_hint_for(:name, :hidden, :hint => 'Use with care...') do |hint|
+ assert hint.generate.blank?
+ end
+ end
+
+ test 'hint should be generated with input text' do
+ with_hint_for(:name, :string, :hint => 'Use with care...')
+ assert_select 'span.hint', 'Use with care...'
+ end
+
+ test 'hint should use i18n with model and attribute to lookup translation' do
+ store_translations(:en, :simple_form => { :hints => { :user => { :name =>
+ 'Content of this input will be capitalized...'
+ } } }) do
+ with_hint_for(:name, :string)
+ assert_select 'span.hint', 'Content of this input will be capitalized...'
+ end
+ end
+
+ test 'hint should use i18n just with attribute to lookup translation' do
+ store_translations(:en, :simple_form => { :hints => { :name =>
+ 'Content of this input will be downcased...'
+ } }) do
+ with_hint_for(:name, :string)
+ assert_select 'span.hint', 'Content of this input will be downcased...'
+ end
+ end
+
+end
diff --git a/test/components/input_test.rb b/test/components/input_test.rb
new file mode 100644
index 00000000..0657a2e4
--- /dev/null
+++ b/test/components/input_test.rb
@@ -0,0 +1,198 @@
+require 'test_helper'
+
+class InputTest < ActionView::TestCase
+
+ setup do
+ SimpleForm::Components::Input.reset_i18n_cache :boolean_collection
+ end
+
+ def with_input_for(attribute, type, options={})
+ simple_form_for @user do |f|
+ input = SimpleForm::Components::Input.new(f, attribute, type, options)
+ concat(input.generate)
+ yield input if block_given?
+ end
+ end
+
+ test 'input should map text field to string attribute' do
+ with_input_for(:name, :string)
+ assert_select 'input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]'
+ end
+
+ test 'input should generate css class based on default input type' do
+ with_input_for(:name, :string)
+ assert_select 'input.string'
+ with_input_for(:description, :text)
+ assert_select 'textarea.text'
+ with_input_for(:age, :numeric)
+ assert_select 'input.numeric'
+ with_input_for(:born_at, :date)
+ assert_select 'select.date'
+ with_input_for(:created_at, :datetime)
+ assert_select 'select.datetime'
+ end
+
+ test 'input should allow passing options to text field' do
+ with_input_for :name, :string, :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 :description, :text
+ assert_select 'textarea.text#user_description'
+ end
+
+ test 'input should generate a numeric text field for numeric attributes ' do
+ with_input_for :age, :numeric
+ assert_select 'input.numeric#user_age'
+ end
+
+ test 'input should generate a checkbox by default for boolean attributes' do
+ with_input_for :active, :boolean
+ assert_select 'input[type=checkbox].boolean#user_active'
+ end
+
+ test 'input should generate a password field for password attributes' do
+ with_input_for :password, :password
+ assert_select 'input[type=password].password#user_password'
+ end
+
+ test 'input should generate a hidden field' do
+ with_input_for :name, :hidden
+ assert_no_select 'input[type=text]'
+ assert_select 'input#user_name[type=hidden]'
+ end
+
+ test 'input should generate a datetime select by default for datetime attributes' do
+ with_input_for :created_at, :datetime
+ 1.upto(5) do |i|
+ assert_select "form select.datetime#user_created_at_#{i}i"
+ end
+ end
+
+ test 'input should be able to pass options to datetime select' do
+ with_input_for :created_at, :datetime, :options => {
+ :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
+ }
+ assert_select 'select.datetime[disabled=disabled]'
+ assert_select 'select.datetime option', 'ano'
+ assert_select 'select.datetime option', 'mês'
+ assert_select 'select.datetime option', 'dia'
+ end
+
+ test 'input should generate a date select for date attributes' do
+ with_input_for :born_at, :date
+ assert_select 'select.date#user_born_at_1i'
+ assert_select 'select.date#user_born_at_2i'
+ assert_select 'select.date#user_born_at_3i'
+ assert_no_select 'select.date#user_born_at_4i'
+ end
+
+ test 'input should be able to pass options to date select' do
+ with_input_for :born_at, :date, :options => {
+ :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
+ }
+ assert_select 'select.date[disabled=disabled]'
+ assert_select 'select.date option', 'ano'
+ assert_select 'select.date option', 'mês'
+ assert_select 'select.date option', 'dia'
+ end
+
+ test 'input should generate a time select for time attributes' do
+ with_input_for :delivery_time, :time
+ assert_select 'input[type=hidden]#user_delivery_time_1i'
+ assert_select 'input[type=hidden]#user_delivery_time_2i'
+ assert_select 'input[type=hidden]#user_delivery_time_3i'
+ assert_select 'select.time#user_delivery_time_4i'
+ assert_select 'select.time#user_delivery_time_5i'
+ end
+
+ test 'input should be able to pass options to time select' do
+ with_input_for :delivery_time, :time, :options => {
+ :disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
+ }
+ assert_select 'select.time[disabled=disabled]'
+ assert_select 'select.time option', 'hora'
+ assert_select 'select.time option', 'minuto'
+ end
+
+ test 'input should generate boolean radio buttons by default for radio types' do
+ with_input_for :active, :radio
+ assert_select 'input[type=radio][value=true].radio#user_active_true'
+ assert_select 'input[type=radio][value=false].radio#user_active_false'
+ end
+
+ test 'input as radio should generate internal labels by default' do
+ with_input_for :active, :radio
+ assert_select 'label.radio[for=user_active_true]', 'Yes'
+ assert_select 'label.radio[for=user_active_false]', 'No'
+ end
+
+ test 'input as radio should use i18n to translate internal labels' do
+ store_translations(:en, :simple_form => { :true => 'Sim', :false => 'Não' }) do
+ with_input_for :active, :radio
+ assert_select 'label.radio[for=user_active_true]', 'Sim'
+ assert_select 'label.radio[for=user_active_false]', 'Não'
+ end
+ end
+
+ test 'input should generate a boolean select with options by default for select types' do
+ with_input_for :active, :select
+ assert_select 'select.select#user_active'
+ assert_select 'select option[value=true]', 'Yes'
+ assert_select 'select option[value=false]', 'No'
+ end
+
+ test 'input as select should use i18n to translate select boolean options' do
+ store_translations(:en, :simple_form => { :true => 'Sim', :false => 'Não' }) do
+ with_input_for :active, :select
+ assert_select 'select option[value=true]', 'Sim'
+ assert_select 'select option[value=false]', 'Não'
+ end
+ end
+
+ test 'input should allow overriding collection for select types' do
+ with_input_for :name, :select, :collection => ['Jose', 'Carlos']
+ assert_select 'select.select#user_name'
+ assert_select 'select option', 'Jose'
+ assert_select 'select option', 'Carlos'
+ end
+
+ test 'inputs should allow overriding collection for radio types' do
+ with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
+ assert_select 'input[type=radio][value=Jose]'
+ assert_select 'input[type=radio][value=Carlos]'
+ assert_select 'label.radio', 'Jose'
+ assert_select 'label.radio', 'Carlos'
+ end
+
+ test 'input should allow using a collection with text/value arrays' do
+ with_input_for :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
+ assert_select 'input[type=radio][value=jose]'
+ assert_select 'input[type=radio][value=carlos]'
+ assert_select 'label.radio', 'Jose'
+ assert_select 'label.radio', 'Carlos'
+ end
+
+ test 'input should allow overriding label and value method for collections' do
+ with_input_for :name, :radio,
+ :collection => ['Jose' , 'Carlos'],
+ :label_method => :upcase,
+ :value_method => :downcase
+ assert_select 'input[type=radio][value=jose]'
+ assert_select 'input[type=radio][value=carlos]'
+ assert_select 'label.radio', 'JOSE'
+ assert_select 'label.radio', 'CARLOS'
+ end
+
+
+ test 'input should be required by default' do
+ with_input_for :name, :string
+ assert_select 'input.required#user_name'
+ end
+
+ test 'input should allow disabling required' do
+ with_input_for :name, :string, :required => false
+ assert_no_select 'input.required'
+ end
+end
diff --git a/test/components/label_test.rb b/test/components/label_test.rb
new file mode 100644
index 00000000..5c118535
--- /dev/null
+++ b/test/components/label_test.rb
@@ -0,0 +1,120 @@
+require 'test_helper'
+
+class LabelTest < ActionView::TestCase
+
+ setup do
+ [:string, :text, :mark].each do |cache|
+ SimpleForm::Components::Label.reset_i18n_cache :"translate_required_#{cache}"
+ end
+ end
+
+ def with_label_for(attribute, type, options={})
+ simple_form_for @user do |f|
+ label = SimpleForm::Components::Label.new(f, attribute, type, options)
+ concat(label.generate)
+ yield label if block_given?
+ end
+ end
+
+ test 'label should not be generated for hidden inputs' do
+ with_label_for(:name, :hidden) do |label|
+ assert label.generate.blank?
+ end
+ end
+
+ test 'label should generate a default humanized description' do
+ with_label_for(:name, :string)
+ assert_select 'label[for=user_name]', /Name/
+ end
+
+ test 'label should allow a customized description' do
+ with_label_for(:name, :string, :label => 'My label!')
+ assert_select 'label[for=user_name]', /My label!/
+ end
+
+ test 'label should use human attribute name from object when available' do
+ with_label_for(:description, :text)
+ assert_select 'label[for=user_description]', /User Description!/
+ end
+
+ test 'label should use i18n based on model and attribute to lookup translation' do
+ store_translations(:en, :simple_form => { :labels => { :user => {
+ :description => 'Descrição'
+ } } } ) do
+ with_label_for(:description, :text)
+ assert_select 'label[for=user_description]', /Descrição/
+ end
+ end
+
+ test 'input should use i18n based only on attribute to lookup translation' do
+ store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do
+ with_label_for(:age, :numeric)
+ assert_select 'label[for=user_age]', /Idade/
+ end
+ end
+
+ test 'label should have css class from type' do
+ with_label_for(:name, :string)
+ assert_select 'label.string'
+ with_label_for(:description, :text)
+ assert_select 'label.text'
+ with_label_for(:age, :numeric)
+ assert_select 'label.numeric'
+ with_label_for(:born_at, :date)
+ assert_select 'label.date'
+ with_label_for(:created_at, :datetime)
+ assert_select 'label.datetime'
+ end
+
+ test 'label should be required by default' do
+ with_label_for(:name, :string)
+ assert_select 'label.required'
+ end
+
+ test 'label should be able to disable required' do
+ with_label_for(:name, :string, :required => false)
+ assert_no_select 'label.required'
+ end
+
+ test 'label should add required text when required' do
+ with_label_for(:name, :string)
+ assert_select 'label.required abbr[title=required]', '*'
+ end
+
+ test 'label should not have required text in no required inputs' do
+ with_label_for(:name, :string, :required => false)
+ assert_no_select 'form label abbr'
+ end
+
+ test 'label should use i18n to find required text' do
+ store_translations(:en, :simple_form => { :required => { :text => 'campo requerido' }}) do
+ with_label_for(:name, :string)
+ assert_select 'form label abbr[title=campo requerido]', '*'
+ end
+ end
+
+ test 'label should use i18n to find required mark' do
+ store_translations(:en, :simple_form => { :required => { :mark => '*-*' }}) do
+ with_label_for(:name, :string)
+ assert_select 'form label abbr', '*-*'
+ end
+ end
+
+ test 'label should use i18n to find required string tag' do
+ store_translations(:en, :simple_form => { :required => { :string => '*' }}) do
+ with_label_for(:name, :string)
+ assert_no_select 'form label abbr'
+ assert_select 'form label span.required[title=requerido]', '*'
+ end
+ end
+
+ test 'label should allow overwriting input id' do
+ with_label_for(:name, :string, :html => { :id => 'my_new_id' })
+ assert_select 'label[for=my_new_id]'
+ end
+
+ test 'label should use default input id when it was not overridden' do
+ with_label_for(:name, :string, :html => { :class => 'my_new_id' })
+ assert_select 'label[for=user_name]'
+ end
+end
diff --git a/test/error_test.rb b/test/error_test.rb
deleted file mode 100644
index 6d9b472b..00000000
--- a/test/error_test.rb
+++ /dev/null
@@ -1,40 +0,0 @@
-require 'test_helper'
-
-class ErrorTest < ActionView::TestCase
-
- def setup
- @super_user = SuperUser.new
- end
-
- test 'input should not generate error by default' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_no_select 'form span.error'
- end
-
- test 'input should generate error messages for attributes with a single error' do
- simple_form_for @super_user do |f|
- concat f.input :name
- concat f.input :description
- end
- assert_select 'form span.error', "can't be blank"
- assert_select 'form span.error', "must be longer than 15 characters"
- end
-
- test 'input should generate error messages for attributes with several errors' do
- simple_form_for @super_user do |f|
- concat f.input :age
- concat f.input :credit_limit
- end
- assert_select 'form span.error', "is not a number and must be greater than 18"
- assert_select 'form span.error', "must be present and must be greater than 0"
- end
-
- test 'errors should not be generated for hidden fields' do
- simple_form_for @super_user do |f|
- concat f.input :name, :as => :hidden
- end
- assert_no_select 'form span.error'
- end
-end
diff --git a/test/form_builder_test.rb b/test/form_builder_test.rb
new file mode 100644
index 00000000..58777b8f
--- /dev/null
+++ b/test/form_builder_test.rb
@@ -0,0 +1,119 @@
+require 'test_helper'
+
+class FormBuilderTest < ActionView::TestCase
+
+ def with_form_for(attribute, options={})
+ simple_form_for @user do |f|
+ concat f.input attribute, options
+ end
+ end
+
+ test 'builder should generate text fields for string columns' do
+ with_form_for :name
+ assert_select 'form input#user_name.string'
+ end
+
+ test 'builder should generate text areas for text columns' do
+ with_form_for :description
+ assert_select 'form textarea#user_description.text'
+ end
+
+ test 'builder should generate a checkbox for boolean columns' do
+ with_form_for :active
+ assert_select 'form input[type=checkbox]#user_active.boolean'
+ end
+
+ test 'builder should use numeric text field for integer columns' do
+ with_form_for :age
+ assert_select 'form input#user_age.numeric'
+ end
+
+ test 'builder should generate numeric text field for numeric columns' do
+ with_form_for :credit_limit
+ assert_select 'form input#user_credit_limit.numeric'
+ end
+
+ test 'builder should generate password fields for columns that match password' do
+ with_form_for :password
+ assert_select 'form input#user_password.password'
+ end
+
+ test 'builder should generate date select for date columns' do
+ with_form_for :born_at
+ assert_select 'form select#user_born_at_1i.date'
+ end
+
+ test 'builder should generate time select for time columns' do
+ with_form_for :delivery_time
+ assert_select 'form select#user_delivery_time_4i.time'
+ end
+
+ test 'builder should generate datetime select for datetime columns' do
+ with_form_for :created_at
+ assert_select 'form select#user_created_at_1i.datetime'
+ end
+
+ test 'builder should generate datetime select for timestamp columns' do
+ with_form_for :updated_at
+ assert_select 'form select#user_updated_at_1i.datetime'
+ end
+
+ test 'builder should allow overriding default input type' do
+ with_form_for :name, :as => :text
+ assert_no_select 'form input#user_name'
+ assert_select 'form textarea#user_name.text'
+ with_form_for :active, :as => :radio
+ assert_no_select 'form input[type=checkbox]'
+ assert_select 'form input.radio[type=radio]', :count => 2
+ with_form_for :born_at, :as => :string
+ assert_no_select 'form select'
+ assert_select 'form input#user_born_at.string'
+ end
+
+ test 'builder should allow passing options to input' do
+ with_form_for :name, :html => { :class => 'my_input', :id => 'my_input' }
+ assert_select 'form input#my_input.my_input.string'
+ end
+
+ test 'builder should generate a input with label' do
+ with_form_for :name
+ assert_select 'form label.string[for=user_name]'
+ end
+
+ test 'builder should be able to disable the label for a input' do
+ with_form_for :name, :label => false
+ assert_no_select 'form label'
+ end
+
+ test 'builder should not generate hints for a input' do
+ with_form_for :name
+ assert_no_select 'span.hint'
+ end
+
+ test 'builder should be able to add a hint for a input' do
+ with_form_for :name, :hint => 'test'
+ assert_select 'span.hint', 'test'
+ end
+
+ test 'builder should be able to disable a hint even if it exists in i18n' do
+ store_translations(:en, :simple_form => { :hints => { :name => 'Hint test' } }) do
+ with_form_for :name, :hint => false
+ assert_no_select 'span.hint'
+ end
+ end
+
+ test 'builder should generate errors for attribute without errors' do
+ with_form_for :credit_limit
+ assert_no_select 'span.errors'
+ end
+
+ test 'builder should generate errors for attribute with errors' do
+ with_form_for :name
+ assert_select 'span.error', "can't be blank"
+ end
+
+ test 'builder should be able to disable showing errors for a input' do
+ with_form_for :name, :error => false
+ assert_no_select 'span.error'
+ end
+end
diff --git a/test/hint_test.rb b/test/hint_test.rb
deleted file mode 100644
index 834b58aa..00000000
--- a/test/hint_test.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-require 'test_helper'
-
-class ErrorTest < ActionView::TestCase
-
- test 'input should not generate a hint by default' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_no_select 'form span.hint'
- end
-
- test 'input should allow generating a hint' do
- simple_form_for @user do |f|
- concat f.input :name, :hint => 'Use with care...'
- end
- assert_select 'form span.hint', 'Use with care...'
- end
-
- test 'input should use i18n to find hints based on model and attribute' do
- store_translations(:en, :simple_form => { :hints => { :user => { :name =>
- 'Content of this input will be capitalized...'
- } } }) do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form span.hint', 'Content of this input will be capitalized...'
- end
- end
-
- test 'input should use i18n based only on attribute to pick up the label translation' do
- store_translations(:en, :simple_form => { :hints => { :name => 'Name hint!' } } ) do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form span.hint', 'Name hint!'
- end
- end
-
- test 'input should allow disabling a hint that exists in i18n' do
- store_translations(:en, :simple_form => { :hints => { :name => 'Name hint!' } } ) do
- simple_form_for @user do |f|
- concat f.input :name, :hint => false
- end
- assert_no_select 'form span.hint'
- end
- end
-
- test 'hint should not be generated for hidden fields' do
- simple_form_for @user do |f|
- concat f.input :name, :hint => 'Bla bla bla', :as => :hidden
- end
- assert_no_select 'form span.hint'
- end
-end
diff --git a/test/input_test.rb b/test/input_test.rb
deleted file mode 100644
index e40f0d89..00000000
--- a/test/input_test.rb
+++ /dev/null
@@ -1,249 +0,0 @@
-require 'test_helper'
-
-class InputTest < ActionView::TestCase
-
- setup do
- SimpleForm::Components::Input.reset_i18n_cache :boolean_collection
- end
-
- test 'input should generate a default text field' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form input[name=\'user[name]\'][id=user_name][value=New in Simple Form!]'
- end
-
- test 'input should generate a default class for each input' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form input#user_name.string'
- end
-
- test 'input should allow passing options to text field' do
- simple_form_for @user do |f|
- concat f.input :name, :html => { :class => 'my_input', :id => 'my_input' }
- end
- assert_select 'form input#my_input.my_input.string'
- end
-
- test 'input should generate a text area by default for text attributes' do
- simple_form_for @user do |f|
- concat f.input :description
- end
- assert_select 'form textarea.text#user_description'
- end
-
- test 'input should generate a text field by default for integer attributes' do
- simple_form_for @user do |f|
- concat f.input :age
- end
- assert_select 'form input.numeric#user_age'
- end
-
- test 'input should generate a text field by default for decimal attributes' do
- simple_form_for @user do |f|
- concat f.input :credit_limit
- end
- assert_select 'form input.numeric#user_credit_limit'
- end
-
- test 'input should generate a checkbox by default for boolean attributes' do
- simple_form_for @user do |f|
- concat f.input :active
- end
- assert_select 'form input[type=checkbox].boolean#user_active'
- end
-
- test 'input should generate a datetime select by default for datetime or timestamp attributes' do
- simple_form_for @user do |f|
- concat f.input :created_at
- concat f.input :updated_at
- end
- 1.upto(5) do |i|
- assert_select "form select.datetime#user_created_at_#{i}i"
- assert_select "form select.datetime#user_updated_at_#{i}i"
- end
- end
-
- test 'input should be able to pass options to datetime select' do
- simple_form_for @user do |f|
- concat f.input :created_at, :options => {
- :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
- }
- end
- assert_select 'form select.datetime[disabled=disabled]'
- assert_select 'form select.datetime option', 'ano'
- assert_select 'form select.datetime option', 'mês'
- assert_select 'form select.datetime option', 'dia'
- end
-
- test 'input should generate a date select by default for date attributes' do
- simple_form_for @user do |f|
- concat f.input :born_at
- end
- assert_select 'form select.date#user_born_at_1i'
- assert_select 'form select.date#user_born_at_2i'
- assert_select 'form select.date#user_born_at_3i'
- assert_no_select 'form select.date#user_born_at_4i'
- end
-
- test 'input should be able to pass options to date select' do
- simple_form_for @user do |f|
- concat f.input :born_at, :options => {
- :disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
- }
- end
- assert_select 'form select.date[disabled=disabled]'
- assert_select 'form select.date option', 'ano'
- assert_select 'form select.date option', 'mês'
- assert_select 'form select.date option', 'dia'
- end
-
- test 'input should generate a time select by default for time attributes' do
- simple_form_for @user do |f|
- concat f.input :delivery_time
- end
- assert_select 'form input[type=hidden]#user_delivery_time_1i'
- assert_select 'form input[type=hidden]#user_delivery_time_2i'
- assert_select 'form input[type=hidden]#user_delivery_time_3i'
- assert_select 'form select.time#user_delivery_time_4i'
- assert_select 'form select.time#user_delivery_time_5i'
- end
-
- test 'input should be able to pass options to time select' do
- simple_form_for @user do |f|
- concat f.input :delivery_time, :options => {
- :disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
- }
- end
- assert_select 'form select.time[disabled=disabled]'
- assert_select 'form select.time option', 'hora'
- assert_select 'form select.time option', 'minuto'
- end
-
- test 'input should allow overwriting default type' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :text
- concat f.input :born_at, :as => :string
- end
- assert_select 'form textarea.text#user_name'
- assert_select 'form input.string#user_born_at'
- end
-
- test 'input should allow boolean attributes as radio buttons' do
- simple_form_for @user do |f|
- concat f.input :active, :as => :radio
- end
- assert_select 'form input[type=radio][value=true].radio#user_active_true'
- assert_select 'form input[type=radio][value=false].radio#user_active_false'
- end
-
- test 'input as radio should generate labels by default' do
- simple_form_for @user do |f|
- concat f.input :active, :as => :radio
- end
- assert_select 'form label.radio[for=user_active_true]', 'Yes'
- assert_select 'form label.radio[for=user_active_false]', 'No'
- end
-
- test 'input as radio should use i18n to translate boolean labels' do
- store_translations(:en, :simple_form => { :true => 'Sim', :false => 'Não' }) do
- simple_form_for @user do |f|
- concat f.input :active, :as => :radio
- end
- assert_select 'form label.radio[for=user_active_true]', 'Sim'
- assert_select 'form label.radio[for=user_active_false]', 'Não'
- end
- end
-
- test 'input should allow boolean attributes as a select with options' do
- simple_form_for @user do |f|
- concat f.input :active, :as => :select
- end
- assert_select 'form select.select#user_active'
- assert_select 'form select option[value=true]', 'Yes'
- assert_select 'form select option[value=false]', 'No'
- end
-
- test 'input as select should use i18n to translate boolean options' do
- store_translations(:en, :simple_form => { :true => 'Sim', :false => 'Não' }) do
- simple_form_for @user do |f|
- concat f.input :active, :as => :select
- end
- assert_select 'form select option[value=true]', 'Sim'
- assert_select 'form select option[value=false]', 'Não'
- end
- end
-
- test 'input should generate a password field for password attributes' do
- simple_form_for @user do |f|
- concat f.input :password
- end
- assert_select 'form input[type=password].password#user_password'
- end
-
- test 'input should generate a hidden field' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :hidden
- end
- assert_no_select 'form input[type=text]'
- assert_select 'form input#user_name[type=hidden]'
- end
-
- test 'input should be required by default' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form input.required'
- end
-
- test 'input should allow disabling required' do
- simple_form_for @user do |f|
- concat f.input :name, :required => false
- end
- assert_no_select 'form input.required'
- end
-
- test 'input allow overriding collection for select types' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :select, :collection => ['Jose', 'Carlos']
- end
- assert_select 'form select.required'
- assert_select 'form select option', 'Jose'
- assert_select 'form select option', 'Carlos'
- end
-
- test 'inputs allow overriding collection for radio types' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :radio, :collection => ['Jose', 'Carlos']
- end
- assert_select 'form input[type=radio][value=Jose]'
- assert_select 'form input[type=radio][value=Carlos]'
- assert_select 'form label.radio', 'Jose'
- assert_select 'form label.radio', 'Carlos'
- end
-
- test 'input allow using a collection with text/value arrays for radio' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
- end
- assert_select 'form input[type=radio][value=jose]'
- assert_select 'form input[type=radio][value=carlos]'
- assert_select 'form label.radio', 'Jose'
- assert_select 'form label.radio', 'Carlos'
- end
-
- test 'input allow overriding label and value method for collections' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :radio,
- :collection => ['Jose' , 'Carlos'],
- :label_method => :upcase,
- :value_method => :downcase
- end
- assert_select 'form input[type=radio][value=jose]'
- assert_select 'form input[type=radio][value=carlos]'
- assert_select 'form label.radio', 'JOSE'
- assert_select 'form label.radio', 'CARLOS'
- end
-end
diff --git a/test/label_test.rb b/test/label_test.rb
deleted file mode 100644
index 6ee9dd7f..00000000
--- a/test/label_test.rb
+++ /dev/null
@@ -1,159 +0,0 @@
-require 'test_helper'
-
-class LabelTest < ActionView::TestCase
-
- setup do
- SimpleForm::Components::Label.reset_i18n_cache :translate_required_string
- end
-
- test 'input should generate a label with the text field' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form label[for=user_name]', /Name/
- end
-
- test 'input should allow not using a label' do
- simple_form_for @user do |f|
- concat f.input :name, :label => false
- end
- assert_no_select 'label'
- end
-
- test 'input should allow using a customized label' do
- simple_form_for @user do |f|
- concat f.input :name, :label => 'My label!'
- end
- assert_select 'form label[for=user_name]', /My label!/
- end
-
- test 'input should use label with human attribute name if it responds to it' do
- @super_user = SuperUser.new
- simple_form_for @super_user do |f|
- concat f.input :name
- end
- assert_select 'form label[for=super_user_name]', /Super User Name!/
- end
-
- test 'input should use i18n based on model name to pick up label translation' do
- store_translations(:en, :simple_form => { :labels => { :super_user => {
- :description => 'Descrição', :age => 'Idade'
- } } } ) do
- @super_user = SuperUser.new
- simple_form_for @super_user do |f|
- concat f.input :description
- concat f.input :age
- end
- assert_select 'form label[for=super_user_description]', /Descrição/
- assert_select 'form label[for=super_user_age]', /Idade/
- end
- end
-
- test 'input should use i18n based only on attribute to pick up the label translation' do
- store_translations(:en, :simple_form => { :labels => { :age => 'Idade' } } ) do
- simple_form_for @user do |f|
- concat f.input :age
- end
- assert_select 'form label[for=user_age]', /Idade/
- end
- end
-
- test 'label should use the same css class as input' do
- simple_form_for @user do |f|
- concat f.input :name
- concat f.input :description
- concat f.input :created_at
- concat f.input :born_at
- concat f.input :active
- concat f.input :age
- concat f.input :credit_limit
- end
- assert_select 'form label.string[for=user_name]'
- assert_select 'form label.text[for=user_description]'
- assert_select 'form label.datetime[for=user_created_at]'
- assert_select 'form label.date[for=user_born_at]'
- assert_select 'form label.boolean[for=user_active]'
- assert_select 'form label.numeric[for=user_age]'
- assert_select 'form label.numeric[for=user_credit_limit]'
- end
-
- test 'input required should generate label required as well' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form label.required'
- end
-
- test 'input not required should not generate label required' do
- simple_form_for @user do |f|
- concat f.input :name, :required => false
- end
- assert_no_select 'form label.required'
- end
-
- test 'label should add required text when input is required' do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form label.required', '* Name'
- assert_select 'form label abbr[title=required]', '*'
- end
-
- test 'label should not have required text in no required inputs' do
- simple_form_for @user do |f|
- concat f.input :name, :required => false
- end
- assert_no_select 'form label abbr'
- end
-
- test 'label should use i18n to find required text' do
- store_translations(:en, :simple_form => { :required => { :text => 'campo requerido' }}) do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form label abbr[title=campo requerido]', '*'
- end
- end
-
- test 'label should use i18n to find required mark' do
- store_translations(:en, :simple_form => { :required => { :mark => '*-*' }}) do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_select 'form label abbr', '*-*'
- end
- end
-
- test 'label should use i18n to find required string tag' do
- store_translations(:en, :simple_form => { :required => { :string => '*' }}) do
- simple_form_for @user do |f|
- concat f.input :name
- end
- assert_no_select 'form label abbr'
- assert_select 'form label span.required[title=requerido]', '*'
- end
- end
-
- test 'label should allow overwriting input id' do
- simple_form_for @user do |f|
- concat f.input :name, :html => { :id => 'my_new_id' }
- end
- assert_select 'form input[id=my_new_id]'
- assert_select 'form label[for=my_new_id]'
- end
-
- test 'label should use default input id when it was not overridden' do
- simple_form_for @user do |f|
- concat f.input :name, :html => { :class => 'test' }
- end
- assert_select 'form input[id=user_name]'
- assert_select 'form label[for=user_name]'
- end
-
- test 'label should not be generated for hidden fields' do
- simple_form_for @user do |f|
- concat f.input :name, :as => :hidden
- end
- assert_no_select 'label'
- end
-end
diff --git a/test/support/models.rb b/test/support/models.rb
index 1d1fd26f..05bcf516 100644
--- a/test/support/models.rb
+++ b/test/support/models.rb
@@ -34,21 +34,11 @@ class User < OpenStruct
Column.new(:name => attribute, :type => column_type)
end
- def human_attribute_name(attribute)
- nil
- end
-
- def errors
- {}
- end
-end
-
-class SuperUser < User
-
def human_attribute_name(attribute)
case attribute
when 'name' then 'Super User Name!'
- else super
+ when 'description' then 'User Description!'
+ else nil
end
end
@@ -57,7 +47,6 @@ class SuperUser < User
:name => "can't be blank",
:description => "must be longer than 15 characters",
:age => ["is not a number", "must be greater than 18"],
- :credit_limit => ["must be present", "must be greater than 0"]
}
end
end