From 2b81d9af385dadf8b37dc14f387afe3d43e4958a Mon Sep 17 00:00:00 2001 From: Jonas Grimfelt Date: Wed, 30 Sep 2009 07:16:05 +0200 Subject: [PATCH] Fixed issue where field_error_proc gets overridden globally - not anymore, 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"). --- lib/formtastic.rb | 35 ++++++++++++++++++++++++----------- spec/formtastic_spec.rb | 16 ++++++++++++++++ 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/lib/formtastic.rb b/lib/formtastic.rb index 4dcc5c745..aab1c78f3 100644 --- a/lib/formtastic.rb +++ b/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 @@ -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 @@ -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__ diff --git a/spec/formtastic_spec.rb b/spec/formtastic_spec.rb index 2647055ac..cc012915b 100644 --- a/spec/formtastic_spec.rb +++ b/spec/formtastic_spec.rb @@ -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)