Skip to content

Commit

Permalink
FormBuilder methods should not modify the options hash
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Feb 21, 2012
1 parent b9a568d commit f4a1ac6
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 4 deletions.
15 changes: 13 additions & 2 deletions lib/simple_form/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def input(attribute_name, options={}, &block)
# name="user[name]" size="100" type="text" value="Carlos" />
#
def input_field(attribute_name, options={})
options = options.dup
options[:input_html] = options.except(:as, :collection, :label_method, :value_method)
SimpleForm::Wrappers::Root.new([:input], :wrapper => false).render find_input(attribute_name, options)
end
Expand Down Expand Up @@ -162,6 +163,8 @@ def input_field(attribute_name, options={})
# From the options above, only :collection can also be supplied.
#
def association(association, options={}, &block)
options = options.dup

return simple_fields_for(*[association,
options.delete(:collection), options].compact, &block) if block_given?

Expand Down Expand Up @@ -210,7 +213,7 @@ def association(association, options={}, &block)
# TODO: remove if condition when supporting only Rails 3.2 forward.
alias_method :button_button, :button if method_defined?(:button)
def button(type, *args, &block)
options = args.extract_options!
options = args.extract_options!.dup
options[:class] = [SimpleForm.button_class, options[:class]].compact
args << options
if respond_to?("#{type}_button")
Expand All @@ -229,6 +232,8 @@ def button(type, *args, &block)
# f.error :name, :id => "cool_error"
#
def error(attribute_name, options={})
options = options.dup

options[:error_html] = options.except(:error_tag, :error_prefix, :error_method)
column = find_attribute_column(attribute_name)
input_type = default_input_type(attribute_name, column, options)
Expand All @@ -244,6 +249,8 @@ def error(attribute_name, options={})
# f.full_error :token #=> <span class="error">Token is invalid</span>
#
def full_error(attribute_name, options={})
options = options.dup

options[:error_prefix] ||= if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(attribute_name.to_s)
else
Expand All @@ -264,6 +271,8 @@ def full_error(attribute_name, options={})
# f.hint "Don't forget to accept this"
#
def hint(attribute_name, options={})
options = options.dup

options[:hint_html] = options.except(:hint_tag, :hint)
if attribute_name.is_a?(String)
options[:hint] = attribute_name
Expand Down Expand Up @@ -292,7 +301,9 @@ def hint(attribute_name, options={})
#
def label(attribute_name, *args)
return super if args.first.is_a?(String) || block_given?
options = args.extract_options!

options = args.extract_options!.dup

options[:label_html] = options.dup
options[:label] = options.delete(:label)
options[:required] = options.delete(:required)
Expand Down
10 changes: 10 additions & 0 deletions test/form_builder/association_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,14 @@ def with_association_for(object, *args)
assert_select 'form ul', :count => 1
assert_select 'form ul li', :count => 3
end

test 'builder with collection support should not change the options hash' do
options = { :as => :check_boxes, :collection_wrapper_tag => :ul, :item_wrapper_tag => :li}
with_association_for @user, :tags, options

assert_select 'form ul', :count => 1
assert_select 'form ul li', :count => 3
assert_equal({ :as => :check_boxes, :collection_wrapper_tag => :ul, :item_wrapper_tag => :li},
options)
end
end
12 changes: 12 additions & 0 deletions test/form_builder/button_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ def with_button_for(object, *args)
assert_select 'form input.button[type=submit][value=Save Post]'
end

test 'builder should create buttons with options' do
with_button_for :post, :submit, :class => 'my_button'
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
end

test 'builder should not modify the options hash' do
options = { :class => 'my_button' }
with_button_for :post, :submit, options
assert_select 'form input.button.my_button[type=submit][value=Save Post]'
assert_equal({ :class => 'my_button' }, options)
end

test 'builder should create buttons for records' do
@user.new_record!
with_button_for @user, :submit
Expand Down
16 changes: 15 additions & 1 deletion test/form_builder/error_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def with_full_error_for(object, *args)
assert_select 'span#error.error.yay'
end

test 'error should not modify the options hash' do
options = { :id => 'error', :class => 'yay' }
with_error_for @user, :name, options
assert_select 'span#error.error.yay'
assert_equal({ :id => 'error', :class => 'yay' }, options)
end

test 'error should find errors on attribute and association' do
with_error_for @user, :company_id, :as => :select,
:error_method => :to_sentence, :reflection => Association.new(Company, :company, {})
Expand Down Expand Up @@ -86,7 +93,7 @@ def with_full_error_for(object, *args)
assert_select 'span.error', "Super User Name! can't be blank"
end

test 'full error should generate an full error tag with a clean HTML' do
test 'full error should generate an full error tag with a clean HTML' do
with_full_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
Expand All @@ -96,6 +103,13 @@ def with_full_error_for(object, *args)
assert_select 'span.error#name_error', "Your name can't be blank"
end

test 'full error should not modify the options hash' do
options = { :id => 'name_error' }
with_full_error_for @user, :name, options
assert_select 'span.error#name_error', "Super User Name! can't be blank"
assert_equal({ :id => 'name_error' }, options)
end

# CUSTOM WRAPPERS

test 'error with custom wrappers works' do
Expand Down
7 changes: 7 additions & 0 deletions test/form_builder/hint_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def with_hint_for(object, *args)
assert_select 'span.hint', 'Use with care...'
end

test 'hint should not modify the options hash' do
options = { :hint => 'Use with care...' }
with_hint_for @user, :name, options
assert_select 'span.hint', 'Use with care...'
assert_equal({ :hint => 'Use with care...' }, options)
end

test 'hint should be generated cleanly with optional text' do
with_hint_for @user, :name, :hint => 'Use with care...'
assert_no_select 'span.hint[hint]'
Expand Down
14 changes: 13 additions & 1 deletion test/form_builder/input_field_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ class InputFieldTest < ActionView::TestCase
assert_select 'input.string.name#name_input'
end

test 'builder input_field should not modify the options hash' do
options = { :id => 'name_input', :class => 'name' }

with_concat_form_for(@user) do |f|
f.input_field :name, options
end

assert_select 'input.string.name#name_input'
assert_equal({ :id => 'name_input', :class => 'name' }, options)
end


test 'builder input_field should generate an input tag with a clean HTML' do
with_concat_form_for(@user) do |f|
f.input_field :name, :as => :integer, :class => 'name'
Expand All @@ -48,4 +60,4 @@ class InputFieldTest < ActionView::TestCase
assert_no_select 'select.status[label_method]'
assert_no_select 'select.status[value_method]'
end
end
end
7 changes: 7 additions & 0 deletions test/form_builder/label_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ def with_label_for(object, *args, &block)
assert_select 'label.string#name_label', /My label/
end

test 'builder should not modify the options hash' do
options = { :label => 'My label', :id => 'name_label' }
with_label_for @user, :name, options
assert_select 'label.string#name_label', /My label/
assert_equal({ :label => 'My label', :id => 'name_label' }, options)
end

test 'builder should fallback to default label when string is given' do
with_label_for @user, :name, 'Nome do usuário'
assert_select 'label', 'Nome do usuário'
Expand Down

0 comments on commit f4a1ac6

Please sign in to comment.