Added possibility to use label, error and hint helpers. TODO: verify label method name.

This commit is contained in:
Carlos Antonio da Silva 2009-12-10 13:49:17 -02:00
parent 233e1df4fa
commit 6c28ff5eed
2 changed files with 76 additions and 4 deletions

View File

@ -3,10 +3,8 @@ module SimpleForm
attr_reader :template, :object_name, :object, :attribute, :column,
:input_type, :options
def input(attribute, options={})
@attribute, @options = attribute, options
@column = find_attribute_column
@input_type = default_input_type
def input(*args)
define_simple_form_attributes(*args)
component = SimpleForm.terminator
SimpleForm.components.reverse.each do |klass|
@ -17,8 +15,38 @@ module SimpleForm
component.call
end
def error(*args)
define_simple_form_attributes(*args)
SimpleForm::Components::Error.new(self, SimpleForm.terminator).call
end
def hint(*args)
define_simple_form_attributes(*args)
SimpleForm::Components::Hint.new(self, SimpleForm.terminator).call
end
# TODO: as we are overriding default label method, we need a way to call the
# default label from rails, or use content tags inside our own helpers.
# Check whether we should remove label call, change method name or use content_tag
def label(*args)
# use default label if we pass the string as usually
return super if args.second.is_a?(String)
define_simple_form_attributes(*args)
SimpleForm::Components::Label.new(self, SimpleForm.terminator).call
end
private
def define_simple_form_attributes(*args)
options = args.extract_options!
attribute = args.shift
@attribute, @options = attribute, options
@column = find_attribute_column
@input_type = default_input_type
end
def default_input_type
return @options[:as].to_sym if @options[:as]
return :select if @options[:collection]

View File

@ -186,4 +186,48 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'form input.decimal#project_budget'
end
# ERRORS
test 'builder should generate an error component tag for the attribute' do
simple_form_for @user do |f|
concat f.error :name
end
assert_select 'span.error', "can't be blank"
end
test 'builder should allow passing options to error tag' do
simple_form_for @user do |f|
concat f.error :name, :error_html => { :id => 'name_error' }
end
assert_select 'span.error#name_error', "can't be blank"
end
# HINTS
test 'builder should generate a hint component tag for the attribute' do
simple_form_for @user do |f|
concat f.hint :name, :hint => 'Hello World!'
end
assert_select 'span.hint', 'Hello World!'
end
test 'builder should allow passing options to hint tag' do
simple_form_for @user do |f|
concat f.hint :name, :hint => 'Hello World!', :hint_html => { :id => 'name_hint' }
end
assert_select 'span.hint#name_hint', 'Hello World!'
end
# LABELS
test 'builder should generate a label component tag for the attribute' do
simple_form_for @user do |f|
concat f.label :name
end
assert_select 'label.string[for=user_name]', /Name/
end
test 'builder should allow passing options to label tag' do
simple_form_for @user do |f|
concat f.label :name, :label => 'My label', :label_html => { :id => 'name_label' }
end
assert_select 'label.string.required#name_label', /My label/
end
end