Permalink
Browse files

Merge pull request #899 from tokland/custom_field_error_proc

you can now configure a custom feild error proc
Formtastic::Helpers::FormHelper.field_error_proc=
  • Loading branch information...
2 parents 521d894 + e798abe commit 9a41fecaa6588051355a5b44fada7e80cb8f815e @justinfrench committed Dec 17, 2012
Showing with 35 additions and 9 deletions.
  1. +4 −0 CHANGELOG
  2. +1 −0 formtastic.gemspec
  3. +7 −9 lib/formtastic/helpers/form_helper.rb
  4. +23 −0 spec/helpers/form_helper_spec.rb
View
@@ -1,3 +1,7 @@
+HEAD
+
+* Allow to use a custom ActionView::Base.field_error_proc
+
2.2.1
* Rails 4 compatibilty updates
View
@@ -36,4 +36,5 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<ammeter>, ["0.2.5"])
s.add_development_dependency(%q<appraisal>)
s.add_development_dependency(%q<rake>)
+ s.add_development_dependency(%q<ruby-debug19>)
end
@@ -58,6 +58,12 @@ module FormHelper
@@default_form_class = 'formtastic'
mattr_accessor :default_form_class
+ # Allows to set a custom field_error_proc wrapper. By default this wrapper
+ # is disabled since `formtastic` already adds an error class to the LI tag
+ # containing the input. Change this from `config/initializers/formtastic.rb`.
+ @@field_error_proc = proc { |html_tag, instance_tag| html_tag }
+ mattr_accessor :field_error_proc
+
# Wrapper around Rails' own `form_for` helper to set the `:builder` option to
# `Formtastic::FormBuilder` and to set some class names on the `<form>` tag such as
# `formtastic` and the downcased and underscored model name (eg `post`).
@@ -178,17 +184,9 @@ def semantic_fields_for(record_name, record_object = nil, options = {}, &block)
protected
- # 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.
- # @private
- FIELD_ERROR_PROC = proc do |html_tag, instance_tag|
- html_tag
- end
-
def with_custom_field_error_proc(&block)
default_field_error_proc = ::ActionView::Base.field_error_proc
- ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
+ ::ActionView::Base.field_error_proc = @@field_error_proc
yield
ensure
::ActionView::Base.field_error_proc = default_field_error_proc
@@ -135,6 +135,29 @@
end
end
+ describe ActionView::Base.field_error_proc do
+ it 'is set to no-op wrapper by default' do
+ semantic_form_for(@new_post, :url => '/hello') do |builder|
+ ::ActionView::Base.field_error_proc.call("html", nil).should == "html"
+ end
+ end
+
+ it 'is set to the configured custom field_error_proc' do
+ field_error_proc = mock()
+ Formtastic::Helpers::FormHelper.field_error_proc = field_error_proc
+ semantic_form_for(@new_post, :url => '/hello') do |builder|
+ ::ActionView::Base.field_error_proc.should == field_error_proc
+ end
+ end
+
+ it 'is restored to its original value after the form is rendered' do
+ lambda do
+ Formtastic::Helpers::FormHelper.field_error_proc = proc {}
+ semantic_form_for(@new_post, :url => '/hello') { |builder| }
+ end.should_not change(::ActionView::Base, :field_error_proc)
+ end
+ end
+
describe "with :builder option" do
it "yields an instance of the given builder" do
class MyAwesomeCustomBuilder < Formtastic::FormBuilder

0 comments on commit 9a41fec

Please sign in to comment.