From 1c886ff65340e80f63947e255ed15be147122f34 Mon Sep 17 00:00:00 2001 From: dfens Date: Tue, 19 Mar 2013 15:27:11 +0100 Subject: [PATCH 1/5] Make FIELD_ERROR_PROC configurable Minor change on field_error_proc Make FIELD_ERROR_PROC configurable, activerecord helper to active model helper --- .../templates/config/initializers/simple_form.rb | 7 +++++++ lib/simple_form.rb | 8 ++++++++ lib/simple_form/action_view_extensions/form_helper.rb | 9 +-------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index cf9ab21d..f45417d6 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -136,4 +136,11 @@ SimpleForm.setup do |config| # Cache SimpleForm inputs discovery # config.cache_discovery = !Rails.env.development? + + # Override the default ActiveModelHelper behaviour of wrapping the input. + # This gets taken care of semantically by adding an error class to the wrapper tag + # containing the input. + # config.field_error_proc = proc do |html_tag, instance_tag| + # html_tag + # end end diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 0f89e983..20e9db25 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -139,6 +139,14 @@ module SimpleForm mattr_accessor :button_class @@button_class = 'button' + # Override the default ActiveRecordHelper behaviour of wrapping the input. + # This gets taken care of semantically by adding an error class to the wrapper tag + # containing the input. + mattr_accessor :field_error_proc + @@field_error_proc = proc do |html_tag, instance_tag| + html_tag + end + ## WRAPPER CONFIGURATION # The default wrapper to be used by the FormBuilder. mattr_accessor :default_wrapper diff --git a/lib/simple_form/action_view_extensions/form_helper.rb b/lib/simple_form/action_view_extensions/form_helper.rb index 309ab0b0..1a2d3017 100644 --- a/lib/simple_form/action_view_extensions/form_helper.rb +++ b/lib/simple_form/action_view_extensions/form_helper.rb @@ -9,13 +9,6 @@ module SimpleForm # end # module FormHelper - # Override the default ActiveRecordHelper behaviour of wrapping the input. - # This gets taken care of semantically by adding an error class to the wrapper tag - # containing the input. - # - FIELD_ERROR_PROC = proc do |html_tag, instance_tag| - html_tag - end def simple_form_for(record, options={}, &block) options[:builder] ||= SimpleForm::FormBuilder @@ -44,7 +37,7 @@ module SimpleForm def with_simple_form_field_error_proc default_field_error_proc = ::ActionView::Base.field_error_proc begin - ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC + ::ActionView::Base.field_error_proc = SimpleForm.field_error_proc yield ensure ::ActionView::Base.field_error_proc = default_field_error_proc From 7561a014da9edfa0685f0d577e028e9c335755d8 Mon Sep 17 00:00:00 2001 From: dfens Date: Mon, 29 Apr 2013 17:13:33 +0200 Subject: [PATCH 2/5] Setting custom field_error_proc test --- lib/simple_form.rb | 2 +- test/action_view_extensions/form_helper_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/simple_form.rb b/lib/simple_form.rb index 20e9db25..d559c174 100644 --- a/lib/simple_form.rb +++ b/lib/simple_form.rb @@ -139,7 +139,7 @@ module SimpleForm mattr_accessor :button_class @@button_class = 'button' - # Override the default ActiveRecordHelper behaviour of wrapping the input. + # Override the default ActiveModelHelper behaviour of wrapping the input. # This gets taken care of semantically by adding an error class to the wrapper tag # containing the input. mattr_accessor :field_error_proc diff --git a/test/action_view_extensions/form_helper_test.rb b/test/action_view_extensions/form_helper_test.rb index c9604d14..0acb2097 100644 --- a/test/action_view_extensions/form_helper_test.rb +++ b/test/action_view_extensions/form_helper_test.rb @@ -131,6 +131,13 @@ class FormHelperTest < ActionView::TestCase end end + test 'custom field error proc' do + expected_error_proc = lambda {} + swap SimpleForm, field_error_proc: expected_error_proc do + assert_equal expected_error_proc, SimpleForm.field_error_proc + end + end + private def swap_field_error_proc(expected_error_proc = lambda {}) From e24f8dfc2154935969895b0ec883f0f244f297d3 Mon Sep 17 00:00:00 2001 From: dfens Date: Mon, 6 May 2013 11:08:11 +0200 Subject: [PATCH 3/5] Ensure that field error proc is swapped by simple_form_for --- test/action_view_extensions/form_helper_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/action_view_extensions/form_helper_test.rb b/test/action_view_extensions/form_helper_test.rb index 0acb2097..cf52207d 100644 --- a/test/action_view_extensions/form_helper_test.rb +++ b/test/action_view_extensions/form_helper_test.rb @@ -138,6 +138,15 @@ class FormHelperTest < ActionView::TestCase end end + test 'SimpleForm for swaps default action view field_error_proc' do + expected_error_proc = lambda {} + swap SimpleForm, field_error_proc: expected_error_proc do + simple_form_for :user do |f| + assert_equal expected_error_proc, ::ActionView::Base.field_error_proc + end + end + end + private def swap_field_error_proc(expected_error_proc = lambda {}) From f9baf4db058e8deb461db4673b29f8d059644b5e Mon Sep 17 00:00:00 2001 From: dfens Date: Wed, 8 May 2013 16:43:10 +0200 Subject: [PATCH 4/5] Remove unnecessary test --- test/action_view_extensions/form_helper_test.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/test/action_view_extensions/form_helper_test.rb b/test/action_view_extensions/form_helper_test.rb index cf52207d..e9964aff 100644 --- a/test/action_view_extensions/form_helper_test.rb +++ b/test/action_view_extensions/form_helper_test.rb @@ -131,13 +131,6 @@ class FormHelperTest < ActionView::TestCase end end - test 'custom field error proc' do - expected_error_proc = lambda {} - swap SimpleForm, field_error_proc: expected_error_proc do - assert_equal expected_error_proc, SimpleForm.field_error_proc - end - end - test 'SimpleForm for swaps default action view field_error_proc' do expected_error_proc = lambda {} swap SimpleForm, field_error_proc: expected_error_proc do From 7800e3bee135799df12b6f320445b78b2b8ef937 Mon Sep 17 00:00:00 2001 From: dfens Date: Wed, 8 May 2013 16:48:09 +0200 Subject: [PATCH 5/5] Remove field_error_proc doc from initializer --- .../templates/config/initializers/simple_form.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/lib/generators/simple_form/templates/config/initializers/simple_form.rb b/lib/generators/simple_form/templates/config/initializers/simple_form.rb index f45417d6..cf9ab21d 100644 --- a/lib/generators/simple_form/templates/config/initializers/simple_form.rb +++ b/lib/generators/simple_form/templates/config/initializers/simple_form.rb @@ -136,11 +136,4 @@ SimpleForm.setup do |config| # Cache SimpleForm inputs discovery # config.cache_discovery = !Rails.env.development? - - # Override the default ActiveModelHelper behaviour of wrapping the input. - # This gets taken care of semantically by adding an error class to the wrapper tag - # containing the input. - # config.field_error_proc = proc do |html_tag, instance_tag| - # html_tag - # end end