Skip to content

Commit

Permalink
Fixed issue where field_error_proc gets overridden globally - not any…
Browse files Browse the repository at this point in the history
…more, only Formtastic. (But if wanted to set it globally, access Formtastic's field_error_proc with Formtastic::SemanticFormHelper::FIELD_ERROR_PROC and set it "the traditional way").
  • Loading branch information
grimen committed Oct 1, 2009
1 parent 80d0783 commit 2b81d9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/formtastic.rb
@@ -1,12 +1,5 @@
# coding: utf-8

# Override the default ActiveRecordHelper behaviour of wrapping the input.
# This gets taken care of semantically by adding an error class to the LI tag
# containing the input.
ActionView::Base.field_error_proc = proc do |html_tag, instance_tag|
html_tag
end

module Formtastic #:nodoc:

class SemanticFormBuilder < ActionView::Helpers::FormBuilder
Expand Down Expand Up @@ -1291,14 +1284,32 @@ def localized_attribute_string(attr_name, attr_value, i18n_key)
module SemanticFormHelper
@@builder = Formtastic::SemanticFormBuilder
mattr_accessor :builder


@@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 LI tag
# containing the input.
#
FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
html_tag
end

def use_custom_field_error_proc(&block)
@@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
end

[:form_for, :fields_for, :form_remote_for, :remote_form_for].each do |meth|
src = <<-END_SRC
def semantic_#{meth}(record_or_name_or_array, *args, &proc)
options = args.extract_options!
options[:builder] = @@builder
options[:html] ||= {}
class_names = options[:html][:class] ? options[:html][:class].split(" ") : []
class_names << "formtastic"
class_names << case record_or_name_or_array
Expand All @@ -1307,8 +1318,10 @@ def semantic_#{meth}(record_or_name_or_array, *args, &proc)
else record_or_name_or_array.class.to_s.underscore # @post => "post"
end
options[:html][:class] = class_names.join(" ")
#{meth}(record_or_name_or_array, *(args << options), &proc)
use_custom_field_error_proc do
#{meth}(record_or_name_or_array, *(args << options), &proc)
end
end
END_SRC
module_eval src, __FILE__, __LINE__
Expand Down
16 changes: 16 additions & 0 deletions spec/formtastic_spec.rb
Expand Up @@ -341,6 +341,22 @@ def custom(arg1, arg2, options = {})
@new_post.stub!(:errors).and_return(@errors)
end

describe "field error proc" do
it "should not be overridden globally for all form builders" do
current_field_error_proc = ::ActionView::Base.field_error_proc

semantic_form_for(@new_post) do |builder|
::ActionView::Base.field_error_proc.should_not == current_field_error_proc
end

::ActionView::Base.field_error_proc.should == current_field_error_proc

form_for(@new_post) do |builder|
::ActionView::Base.field_error_proc.should == current_field_error_proc
end
end
end

describe 'when there are errors' do
before do
@errors.stub!(:[]).with(:title).and_return(@title_errors)
Expand Down

0 comments on commit 2b81d9a

Please sign in to comment.