From cba2b49410de596b9b1886c2adea1561e93c2b19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendonc=CC=A7a=20Franc=CC=A7a?= Date: Wed, 16 Nov 2011 13:29:36 -0200 Subject: [PATCH 1/3] Add failing test to #355 --- test/form_builder/general_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/form_builder/general_test.rb b/test/form_builder/general_test.rb index d6b652e0..eca888d9 100644 --- a/test/form_builder/general_test.rb +++ b/test/form_builder/general_test.rb @@ -281,6 +281,16 @@ class FormBuilderTest < ActionView::TestCase assert_select 'input.default_class#specific_id' end + test 'builder should receive a default argument and pass it to the inputs without changing the defaults' do + with_concat_form_for @user, :defaults => { :input_html => { :class => 'default_class', :id => 'default_id' } } do |f| + concat(f.input :name) + concat(f.input :credit_limit) + end + + assert_select "input.string.default_class[name='user[name]']" + assert_no_select "input.string[name='user[credit_limit]']" + end + # WITHOUT OBJECT test 'builder should generate properly when object is not present' do From 660b98655a66ab9570cf5ac43abe2b1316fc6d11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?George=20Guimara=CC=83es?= Date: Tue, 29 Nov 2011 18:37:58 -0200 Subject: [PATCH 2/3] Closes #355, but using deep_dup, which is only available in Rails 3.1 --- lib/simple_form/form_builder.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index 8c1a314d..e0b96214 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/deep_dup' + module SimpleForm class FormBuilder < ActionView::Helpers::FormBuilder attr_reader :template, :object_name, :object, :wrapper @@ -98,7 +100,7 @@ module SimpleForm # given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly. # def input(attribute_name, options={}, &block) - options = @defaults.deep_merge(options) if @defaults + options = @defaults.deep_dup.deep_merge(options) if @defaults chosen = if name = options[:wrapper] From f1686c5092713c5a121a0e0b13a24ba5709c7002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?George=20Guimara=CC=83es?= Date: Wed, 30 Nov 2011 20:03:14 -0200 Subject: [PATCH 3/3] copying Hash#deep_dup from active_support 3.1 for #355 --- lib/simple_form/core_ext/hash.rb | 16 ++++++++++++++++ lib/simple_form/form_builder.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 lib/simple_form/core_ext/hash.rb diff --git a/lib/simple_form/core_ext/hash.rb b/lib/simple_form/core_ext/hash.rb new file mode 100644 index 00000000..18ca8bef --- /dev/null +++ b/lib/simple_form/core_ext/hash.rb @@ -0,0 +1,16 @@ +# TODO: Delete this file when we drop support for Rails 3.0 +# This method is already implemented in active_support 3.1 + +unless Hash.new.respond_to?(:deep_dup) + class Hash + # Returns a deep copy of hash. + def deep_dup + duplicate = self.dup + duplicate.each_pair do |k,v| + tv = duplicate[k] + duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v + end + duplicate + end + end +end diff --git a/lib/simple_form/form_builder.rb b/lib/simple_form/form_builder.rb index e0b96214..5af1d92f 100644 --- a/lib/simple_form/form_builder.rb +++ b/lib/simple_form/form_builder.rb @@ -1,4 +1,4 @@ -require 'active_support/core_ext/hash/deep_dup' +require 'simple_form/core_ext/hash' module SimpleForm class FormBuilder < ActionView::Helpers::FormBuilder