Updates to get forms working without objects and tests refactorings.

This commit is contained in:
Carlos Antonio da Silva 2009-12-10 09:31:40 -02:00
parent aabeacf40b
commit c255e0a504
7 changed files with 181 additions and 107 deletions

View File

@ -2,7 +2,7 @@ module SimpleForm
module Components
class Error < Base
def valid?
!hidden_input? && !errors.blank?
object && !hidden_input? && !errors.blank?
end
def errors

View File

@ -28,8 +28,12 @@ module SimpleForm
return options[:as].to_sym if options[:as]
return :select if options[:collection]
column = @object.column_for_attribute(attribute)
input_type = column.type
input_type = if @object.respond_to?(:column_for_attribute)
column = @object.column_for_attribute(attribute)
column.type if column
else
:string
end
case input_type
when :timestamp

View File

@ -2,8 +2,8 @@ require 'test_helper'
class ErrorTest < ActionView::TestCase
def with_error_for(attribute, type, &block)
simple_form_for @user do |f|
def with_error_for(object, attribute, type, &block)
simple_form_for object do |f|
error = SimpleForm::Components::Error.new(f, attribute, type, {})
concat(error.generate)
yield error if block_given?
@ -11,25 +11,30 @@ class ErrorTest < ActionView::TestCase
end
test 'error should not generate content for hidden fields' do
with_error_for(:name, :hidden) do |error|
with_error_for @user, :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|
with_error_for @user, :active, :boolean do |error|
assert error.generate.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.generate.blank?
end
end
test 'error should generate messages for attribute with single error' do
with_error_for(:name, :string)
with_error_for @user, :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)
with_error_for @user, :age, :numeric
assert_select 'span.error', 'is not a number and must be greater than 18'
end
end

View File

@ -2,8 +2,8 @@ require 'test_helper'
class ErrorTest < ActionView::TestCase
def with_hint_for(attribute, type, options={})
simple_form_for @user do |f|
def with_hint_for(object, attribute, type, options={})
simple_form_for object do |f|
hint = SimpleForm::Components::Hint.new(f, attribute, type, options)
concat(hint.generate)
yield hint if block_given?
@ -11,25 +11,25 @@ class ErrorTest < ActionView::TestCase
end
test 'hint should not be generated by default' do
with_hint_for(:name, :string) do |hint|
with_hint_for @user, :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|
with_hint_for @user, :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...')
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'span.hint', 'Use with care...'
end
test 'hint uses the current component tag set' do
swap SimpleForm, :component_tag => :p do
with_hint_for(:name, :string, :hint => 'Use with care...')
with_hint_for @user, :name, :string, :hint => 'Use with care...'
assert_select 'p.hint', 'Use with care...'
end
end
@ -38,7 +38,7 @@ class ErrorTest < ActionView::TestCase
store_translations(:en, :simple_form => { :hints => { :user => { :name =>
'Content of this input will be capitalized...'
} } }) do
with_hint_for(:name, :string)
with_hint_for @user, :name, :string
assert_select 'span.hint', 'Content of this input will be capitalized...'
end
end
@ -47,9 +47,13 @@ class ErrorTest < ActionView::TestCase
store_translations(:en, :simple_form => { :hints => { :name =>
'Content of this input will be downcased...'
} }) do
with_hint_for(:name, :string)
with_hint_for @user, :name, :string
assert_select 'span.hint', 'Content of this input will be downcased...'
end
end
test 'hint should generate properly when object is not present' do
with_hint_for :project, :name, :string, :hint => 'Test without object'
assert_select 'span.hint', 'Test without object'
end
end

View File

@ -6,8 +6,8 @@ class InputTest < ActionView::TestCase
SimpleForm::Components::Input.reset_i18n_cache :boolean_collection
end
def with_input_for(attribute, type, options={})
simple_form_for @user do |f|
def with_input_for(object, attribute, type, options={})
simple_form_for object do |f|
input = SimpleForm::Components::Input.new(f, attribute, type, options)
concat(input.generate)
yield input if block_given?
@ -15,73 +15,73 @@ class InputTest < ActionView::TestCase
end
test 'input should map text field to string attribute' do
with_input_for(:name, :string)
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 css class based on default input type' do
with_input_for(:name, :string)
with_input_for @user, :name, :string
assert_select 'input.string'
with_input_for(:description, :text)
with_input_for @user, :description, :text
assert_select 'textarea.text'
with_input_for(:age, :integer)
with_input_for @user, :age, :integer
assert_select 'input.integer'
with_input_for(:born_at, :date)
with_input_for @user, :born_at, :date
assert_select 'select.date'
with_input_for(:created_at, :datetime)
with_input_for @user, :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' }
with_input_for @user, :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
with_input_for @user, :description, :text
assert_select 'textarea.text#user_description'
end
test 'input should generate an integer text field for integer attributes ' do
with_input_for :age, :integer
with_input_for @user, :age, :integer
assert_select 'input.integer#user_age'
end
test 'input should generate a float text field for float attributes ' do
with_input_for :age, :float
with_input_for @user, :age, :float
assert_select 'input.float#user_age'
end
test 'input should generate a decimal text field for decimal attributes ' do
with_input_for :age, :decimal
with_input_for @user, :age, :decimal
assert_select 'input.decimal#user_age'
end
test 'input should generate a checkbox by default for boolean attributes' do
with_input_for :active, :boolean
with_input_for @user, :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
with_input_for @user, :password, :password
assert_select 'input[type=password].password#user_password'
end
test 'input should generate a hidden field' do
with_input_for :name, :hidden
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 datetime select by default for datetime attributes' do
with_input_for :created_at, :datetime
with_input_for @user, :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 => {
with_input_for @user, :created_at, :datetime, :options => {
:disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
}
assert_select 'select.datetime[disabled=disabled]'
@ -91,7 +91,7 @@ class InputTest < ActionView::TestCase
end
test 'input should generate a date select for date attributes' do
with_input_for :born_at, :date
with_input_for @user, :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'
@ -99,7 +99,7 @@ class InputTest < ActionView::TestCase
end
test 'input should be able to pass options to date select' do
with_input_for :born_at, :date, :options => {
with_input_for @user, :born_at, :date, :options => {
:disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
}
assert_select 'select.date[disabled=disabled]'
@ -109,7 +109,7 @@ class InputTest < ActionView::TestCase
end
test 'input should generate a time select for time attributes' do
with_input_for :delivery_time, :time
with_input_for @user, :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'
@ -118,7 +118,7 @@ class InputTest < ActionView::TestCase
end
test 'input should be able to pass options to time select' do
with_input_for :delivery_time, :time, :options => {
with_input_for @user, :delivery_time, :time, :options => {
:disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
}
assert_select 'select.time[disabled=disabled]'
@ -127,27 +127,27 @@ class InputTest < ActionView::TestCase
end
test 'input should generate boolean radio buttons by default for radio types' do
with_input_for :active, :radio
with_input_for @user, :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
with_input_for @user, :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
with_input_for @user, :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
with_input_for @user, :active, :select
assert_select 'select.select#user_active'
assert_select 'select option[value=true]', 'Yes'
assert_select 'select option[value=false]', 'No'
@ -155,14 +155,14 @@ class InputTest < ActionView::TestCase
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
with_input_for @user, :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']
with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
assert_select 'select.select#user_name'
assert_select 'select option', 'Jose'
assert_select 'select option', 'Carlos'
@ -170,35 +170,35 @@ class InputTest < ActionView::TestCase
test 'input should mark the selected value by default' do
@user.name = "Carlos"
with_input_for :name, :select, :collection => ['Jose', 'Carlos']
with_input_for @user, :name, :select, :collection => ['Jose', 'Carlos']
assert_select 'select option[selected=selected]', 'Carlos'
end
test 'input should mark the selected value also when using integers' do
@user.age = 18
with_input_for :age, :select, :collection => 18..60
with_input_for @user, :age, :select, :collection => 18..60
assert_select 'select option[selected=selected]', '18'
end
test 'input should automatically set include blank' do
with_input_for :age, :select, :collection => 18..30
with_input_for @user, :age, :select, :collection => 18..30
assert_select 'select option[value=]', ""
end
test 'input should not set include blank if otherwise is told' do
with_input_for :age, :select, :collection => 18..30, :options => { :include_blank => false }
with_input_for @user, :age, :select, :collection => 18..30, :options => { :include_blank => false }
assert_no_select 'select option[value=]', ""
end
test 'input should detect label and value on collections' do
users = [ setup_new_user(:id => 1, :name => "Jose"), setup_new_user(:id => 2, :name => "Carlos") ]
with_input_for :description, :select, :collection => users
with_input_for @user, :description, :select, :collection => users
assert_select 'select option[value=1]', 'Jose'
assert_select 'select option[value=2]', 'Carlos'
end
test 'input should allow overriding collection for radio types' do
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Jose]'
assert_select 'input[type=radio][value=Carlos]'
assert_select 'label.radio', 'Jose'
@ -207,12 +207,12 @@ class InputTest < ActionView::TestCase
test 'input should mark the current radio value by default' do
@user.name = "Carlos"
with_input_for :name, :radio, :collection => ['Jose', 'Carlos']
with_input_for @user, :name, :radio, :collection => ['Jose', 'Carlos']
assert_select 'input[type=radio][value=Carlos][checked=checked]'
end
test 'input should allow using a collection with text/value arrays' do
with_input_for :name, :radio, :collection => [['Jose', 'jose'], ['Carlos', 'carlos']]
with_input_for @user, :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'
@ -220,7 +220,7 @@ class InputTest < ActionView::TestCase
end
test 'input should allow overriding label and value method for collections' do
with_input_for :name, :radio,
with_input_for @user, :name, :radio,
:collection => ['Jose' , 'Carlos'],
:label_method => :upcase,
:value_method => :downcase
@ -231,13 +231,29 @@ class InputTest < ActionView::TestCase
end
test 'input should be required by default' do
with_input_for :name, :string
with_input_for @user, :name, :string
assert_select 'input.required#user_name'
end
test 'input should allow disabling required' do
with_input_for :name, :string, :required => false
with_input_for @user, :name, :string, :required => false
assert_no_select 'input.required'
assert_select 'input.optional#user_name'
end
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'
end
test 'input as radio should be generated properly when object is not present ' do
with_input_for :project, :name, :radio
assert_select 'input.radio#project_name_true'
assert_select 'input.radio#project_name_false'
end
test 'input as select with collection should be generated properly when object is not present' do
with_input_for :project, :name, :select, :collection => ['Jose', 'Carlos']
assert_select 'select.select#project_name'
end
end

View File

@ -8,8 +8,8 @@ class LabelTest < ActionView::TestCase
end
end
def with_label_for(attribute, type, options={})
simple_form_for @user do |f|
def with_label_for(object, attribute, type, options={})
simple_form_for object do |f|
label = SimpleForm::Components::Label.new(f, attribute, type, options)
concat(label.generate)
yield label if block_given?
@ -17,23 +17,23 @@ class LabelTest < ActionView::TestCase
end
test 'label should not be generated for hidden inputs' do
with_label_for(:name, :hidden) do |label|
with_label_for @user, :name, :hidden do |label|
assert label.generate.blank?
end
end
test 'label should generate a default humanized description' do
with_label_for(:name, :string)
with_label_for @user, :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!')
with_label_for @user, :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)
with_label_for @user, :description, :text
assert_select 'label[for=user_description]', /User Description!/
end
@ -41,80 +41,101 @@ class LabelTest < ActionView::TestCase
store_translations(:en, :simple_form => { :labels => { :user => {
:description => 'Descrição'
} } } ) do
with_label_for(:description, :text)
with_label_for @user, :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)
with_label_for @user, :age, :integer
assert_select 'label[for=user_age]', /Idade/
end
end
test 'label should have css class from type' do
with_label_for(:name, :string)
with_label_for @user, :name, :string
assert_select 'label.string'
with_label_for(:description, :text)
with_label_for @user, :description, :text
assert_select 'label.text'
with_label_for(:age, :numeric)
assert_select 'label.numeric'
with_label_for(:born_at, :date)
with_label_for @user, :age, :integer
assert_select 'label.integer'
with_label_for @user, :born_at, :date
assert_select 'label.date'
with_label_for(:created_at, :datetime)
with_label_for @user, :created_at, :datetime
assert_select 'label.datetime'
end
test 'label should be required by default' do
with_label_for(:name, :string)
with_label_for @user, :name, :string
assert_select 'label.required'
end
test 'label should be able to disable required' do
with_label_for(:name, :string, :required => false)
with_label_for @user, :name, :string, :required => false
assert_no_select 'label.required'
end
test 'label should add required text when required' do
with_label_for(:name, :string)
with_label_for @user, :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)
with_label_for @user, :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)
with_label_for @user, :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)
with_label_for @user, :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 => '<span class="required" title="requerido">*</span>' }}) do
with_label_for(:name, :string)
with_label_for @user, :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' })
with_label_for @user, :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' })
with_label_for @user, :name, :string, :html => { :class => 'my_new_id' }
assert_select 'label[for=user_name]'
end
test 'label should be generated properly when object is not present' do
with_label_for :project, :name, :string
assert_select 'label[for=project_name]', /Name/
end
test 'label should use i18n properly when object is not present' do
store_translations(:en, :simple_form => { :labels => {
:project => { :name => 'Nome' }
} } ) do
with_label_for :project, :name, :string
assert_select 'label[for=project_name]', /Nome/
end
end
test 'label should add required by default when object is not present' do
with_label_for :project, :name, :string
assert_select 'label.required[for=project_name]'
with_label_for :project, :description, :string, :required => false
assert_no_select 'label.required[for=project_description]'
end
end

View File

@ -2,136 +2,136 @@ require 'test_helper'
class FormBuilderTest < ActionView::TestCase
def with_form_for(attribute, options={})
simple_form_for @user do |f|
def with_form_for(object, attribute, options={})
simple_form_for object do |f|
concat f.input attribute, options
end
end
test 'builder should generate text fields for string columns' do
with_form_for :name
with_form_for @user, :name
assert_select 'form input#user_name.string'
end
test 'builder should generate text areas for text columns' do
with_form_for :description
with_form_for @user, :description
assert_select 'form textarea#user_description.text'
end
test 'builder should generate a checkbox for boolean columns' do
with_form_for :active
with_form_for @user, :active
assert_select 'form input[type=checkbox]#user_active.boolean'
end
test 'builder should use integer text field for integer columns' do
with_form_for :age
with_form_for @user, :age
assert_select 'form input#user_age.integer'
end
test 'builder should generate decimal text field for decimal columns' do
with_form_for :credit_limit
with_form_for @user, :credit_limit
assert_select 'form input#user_credit_limit.decimal'
end
test 'builder should generate password fields for columns that match password' do
with_form_for :password
with_form_for @user, :password
assert_select 'form input#user_password.password'
end
test 'builder should generate date select for date columns' do
with_form_for :born_at
with_form_for @user, :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
with_form_for @user, :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
with_form_for @user, :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
with_form_for @user, :updated_at
assert_select 'form select#user_updated_at_1i.datetime'
end
test 'build should generate select if a collection is given' do
with_form_for :age, :collection => 1..60
with_form_for @user, :age, :collection => 1..60
assert_select 'form select#user_age.select'
end
test 'builder should allow overriding default input type for text' do
with_form_for :name, :as => :text
with_form_for @user, :name, :as => :text
assert_no_select 'form input#user_name'
assert_select 'form textarea#user_name.text'
with_form_for :active, :as => :radio
with_form_for @user, :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
with_form_for @user, :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' }
with_form_for @user, :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
with_form_for @user, :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
with_form_for @user, :name, :label => false
assert_no_select 'form label'
end
test 'builder should use custom label' do
with_form_for :name, :label => 'Yay!'
with_form_for @user, :name, :label => 'Yay!'
assert_no_select 'form label', 'Yay!'
end
test 'builder should not generate hints for a input' do
with_form_for :name
with_form_for @user, :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'
with_form_for @user, :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
with_form_for @user, :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
with_form_for @user, :credit_limit
assert_no_select 'span.errors'
end
test 'builder should generate errors for attribute with errors' do
with_form_for :name
with_form_for @user, :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
with_form_for @user, :name, :error => false
assert_no_select 'span.error'
end
test 'builder support wrapping around an specific tag' do
swap SimpleForm, :wrapper_tag => :p do
with_form_for :name
with_form_for @user, :name
assert_select 'form p label[for=user_name]'
assert_select 'form p input#user_name.string'
end
@ -145,4 +145,28 @@ class FormBuilderTest < ActionView::TestCase
end
end
test 'builder should generate properly when object is not present' do
with_form_for :project, :name
assert_select 'form input.string#project_name'
end
test 'builder should generate password fields based on attribute name when object is not present' do
with_form_for :project, :password_confirmation
assert_select 'form input[type=password].password#project_password_confirmation'
end
test 'builder should generate text fields by default for all attributes when object is not present' do
with_form_for :project, :created_at
assert_select 'form input.string#project_created_at'
with_form_for :project, :budget
assert_select 'form input.string#project_budget'
end
test 'builder should allow overriding input type when object is not present' do
with_form_for :project, :created_at, :as => :datetime
assert_select 'form select.datetime#project_created_at_1i'
with_form_for :project, :budget, :as => :decimal
assert_select 'form input.decimal#project_budget'
end
end