New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allows custom field_error_proc #899

Merged
merged 3 commits into from Dec 17, 2012
Jump to file or symbol
Failed to load files and symbols.
+35 −9
Diff settings

Always

Just for now

View
@@ -1,3 +1,7 @@
HEAD
* Allow to use a custom ActionView::Base.field_error_proc
2.2.1 2.2.1
* Rails 4 compatibilty updates * 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<ammeter>, ["0.2.5"])
s.add_development_dependency(%q<appraisal>) s.add_development_dependency(%q<appraisal>)
s.add_development_dependency(%q<rake>) s.add_development_dependency(%q<rake>)
s.add_development_dependency(%q<ruby-debug19>)
end end
@@ -58,6 +58,12 @@ module FormHelper
@@default_form_class = 'formtastic' @@default_form_class = 'formtastic'
mattr_accessor :default_form_class 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 # 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::FormBuilder` and to set some class names on the `<form>` tag such as
# `formtastic` and the downcased and underscored model name (eg `post`). # `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 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) def with_custom_field_error_proc(&block)
default_field_error_proc = ::ActionView::Base.field_error_proc 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 yield
ensure ensure
::ActionView::Base.field_error_proc = default_field_error_proc ::ActionView::Base.field_error_proc = default_field_error_proc
@@ -135,6 +135,29 @@
end end
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 describe "with :builder option" do
it "yields an instance of the given builder" do it "yields an instance of the given builder" do
class MyAwesomeCustomBuilder < Formtastic::FormBuilder class MyAwesomeCustomBuilder < Formtastic::FormBuilder
ProTip! Use n and p to navigate between commits in a pull request.