Adding required option for labels/inputs and ensure labels also have input type and required css classes

This commit is contained in:
Carlos Antonio da Silva 2009-12-08 14:49:14 -02:00
parent c58d217654
commit 3f2e21533f
6 changed files with 89 additions and 23 deletions

View File

@ -8,7 +8,9 @@ module SimpleForm
def input(attribute, options={})
@attribute, @options = attribute, options
@options.assert_valid_keys(:as, :label, :options, :html)
@options.assert_valid_keys(:as, :label, :required, :options, :html)
@input_type = (@options[:as] || default_input_type).to_sym
label = generate_label
input = generate_input
@ -16,5 +18,26 @@ module SimpleForm
label << input
end
private
def required_class
'required' if attribute_required?
end
def attribute_required?
true unless @options[:required] == false
end
def default_input_type
input_type = @object.try(:column_for_attribute, @attribute)
case input_type
when nil then :string
when :decimal then :numeric
when :timestamp then :datetime
when :string then
@attribute.to_s =~ /password/ ? :password : :string
else input_type
end
end
end
end

View File

@ -4,12 +4,11 @@ module SimpleForm
private
def generate_input
input_type = (@options[:as] || default_input_type).to_sym
html_options = @options[:html] || {}
html_options[:class] = "#{html_options[:class]} #{input_type}".strip
html_options[:class] = "#{html_options[:class]} #{@input_type} #{required_class}".strip
@options[:options] ||= {}
input_field = case input_type
input_field = case @input_type
when :boolean then
check_box(@attribute, html_options)
when :radio then
@ -31,15 +30,5 @@ module SimpleForm
end
end
def default_input_type
input_type = @object.try(:column_for_attribute, @attribute)
case input_type
when nil then :string
when :timestamp then :datetime
when :string then
@attribute.to_s =~ /password/ ? :password : :string
else input_type
end
end
end
end

View File

@ -5,7 +5,8 @@ module SimpleForm
def generate_label
return '' if @options[:label] == false
label(@attribute, @options[:label] || translate_label)
html_options = { :class => "#{@input_type} #{required_class}" }
label(@attribute, @options[:label] || translate_label, html_options)
end
def translate_label

View File

@ -49,7 +49,7 @@ class InputTest < ActionView::TestCase
simple_form_for @user do |f|
concat f.input :credit_limit
end
assert_select "form input.decimal#user_credit_limit"
assert_select "form input.numeric#user_credit_limit"
end
test 'input should generate a checkbox by default for boolean attributes' do
@ -76,7 +76,7 @@ class InputTest < ActionView::TestCase
:disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
}
end
assert_tag :tag => 'select', :attributes => { :disabled => 'disabled' }
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'
@ -89,7 +89,7 @@ class InputTest < ActionView::TestCase
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_tag :tag => 'select', :attributes => { :id => "user_born_at_4i" }
assert_no_select "form select.date#user_born_at_4i"
end
test 'input should be able to pass options to date select' do
@ -98,7 +98,7 @@ class InputTest < ActionView::TestCase
:disabled => true, :prompt => { :year => 'ano', :month => 'mês', :day => 'dia' }
}
end
assert_tag :tag => 'select', :attributes => { :disabled => 'disabled' }
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'
@ -121,7 +121,7 @@ class InputTest < ActionView::TestCase
:disabled => true, :prompt => { :hour => 'hora', :minute => 'minuto' }
}
end
assert_tag :tag => 'select', :attributes => { :disabled => 'disabled' }
assert_select 'form select.time[disabled=disabled]'
assert_select 'form select.time option', 'hora'
assert_select 'form select.time option', 'minuto'
end
@ -150,4 +150,17 @@ class InputTest < ActionView::TestCase
assert_select 'form input[type=password].password#user_password'
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
end

View File

@ -13,7 +13,7 @@ class LabelTest < ActionView::TestCase
simple_form_for @user do |f|
concat f.input :name, :label => false
end
assert_no_tag :tag => 'label'
assert_no_select 'label'
end
test 'input should allow using a customized label' do
@ -23,7 +23,7 @@ class LabelTest < ActionView::TestCase
assert_select 'form label[for=user_name]', 'My label!'
end
test 'input should use human attribute name if it responds to it' do
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
@ -31,7 +31,7 @@ class LabelTest < ActionView::TestCase
assert_select 'form label[for=super_user_name]', 'Super User Name!'
end
test 'input should use i18n to pick up translation' do
test 'input should use i18n to pick up label translation' do
store_translations(:en, :views => { :labels => { :super_user => {
:description => 'Descrição', :age => 'Idade'
} } } ) do
@ -44,4 +44,37 @@ class LabelTest < ActionView::TestCase
assert_select 'form label[for=super_user_age]', 'Idade'
end
end
test 'label should use the same input type 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.integer[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
end

View File

@ -43,4 +43,11 @@ class ActionView::TestCase
'/users'
end
alias :super_user_path :user_path
# Wrapper to assert no select exists
def assert_no_select(*args)
assert_raise Test::Unit::AssertionFailedError do
assert_select(*args)
end
end
end