Skip to content

Commit

Permalink
Merge pull request #566 from jim/preserve-field-error-proc
Browse files Browse the repository at this point in the history
Preserve ActionView::Base.field_error_proc
  • Loading branch information
carlosantoniodasilva committed May 9, 2012
2 parents 18306d8 + b9eb68e commit 0c22a25
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/simple_form/action_view_extensions/form_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ module ActionViewExtensions
# end
#
module FormHelper
# based on what is done in formtastic
# http://github.com/justinfrench/formtastic/blob/master/lib/formtastic.rb#L1706
@@default_field_error_proc = nil

# 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.
Expand Down Expand Up @@ -46,11 +42,13 @@ def simple_fields_for(record_name, record_object = nil, options = {}, &block)
private

def with_simple_form_field_error_proc
@@default_field_error_proc = ::ActionView::Base.field_error_proc
::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
result = yield
::ActionView::Base.field_error_proc = @@default_field_error_proc
result
default_field_error_proc = ::ActionView::Base.field_error_proc
begin
::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
yield
ensure
::ActionView::Base.field_error_proc = default_field_error_proc
end
end

def simple_form_css_class(record, options)
Expand Down
45 changes: 45 additions & 0 deletions test/action_view_extensions/form_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,49 @@ class FormHelperTest < ActionView::TestCase

assert_select "input[name='author[name]'][value='hash backed author']"
end

test 'custom error proc is not destructive' do
previous_error_proc = ActionView::Base.field_error_proc

begin
expected_error_proc = lambda {}
ActionView::Base.field_error_proc = expected_error_proc

result = nil
simple_form_for :user do |f|
result = simple_fields_for 'address' do
'hello'
end
end

assert_equal 'hello', result
assert_equal expected_error_proc, ActionView::Base.field_error_proc

ensure
ActionView::Base.field_error_proc = previous_error_proc
end
end

test 'custom error proc survives an exception' do
previous_error_proc = ActionView::Base.field_error_proc

begin
expected_error_proc = lambda {}
ActionView::Base.field_error_proc = expected_error_proc

begin
simple_form_for :user do |f|
simple_fields_for 'address' do
raise 'an exception'
end
end
rescue StandardError => e
end

assert_equal expected_error_proc, ActionView::Base.field_error_proc

ensure
ActionView::Base.field_error_proc = previous_error_proc
end
end
end

0 comments on commit 0c22a25

Please sign in to comment.