Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #986 from groupbuddies/master

Allows to disable model infered class from form
  • Loading branch information...
commit f630fb0bee233c75301cc0a362cfbe58ae7b5001 2 parents b852706 + eb97ac8
@sobrinho sobrinho authored
View
11 lib/formtastic/helpers/form_helper.rb
@@ -58,6 +58,11 @@ module FormHelper
@@default_form_class = 'formtastic'
mattr_accessor :default_form_class
+ # Allows to set a custom proc to handle the class infered from the model's name. By default it
+ # will infer the name from the class name (eg. Post will be "post").
+ @@default_form_model_class_proc = proc { |model_class_name| model_class_name }
+ mattr_accessor :default_form_model_class_proc
+
# 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`.
@@ -156,11 +161,13 @@ def semantic_form_for(record_or_name_or_array, *args, &proc)
class_names = options[:html][:class] ? options[:html][:class].split(" ") : []
class_names << @@default_form_class
- class_names << case record_or_name_or_array
+ model_class_name = case record_or_name_or_array
when String, Symbol then record_or_name_or_array.to_s # :post => "post"
when Array then options[:as] || singularizer.call(record_or_name_or_array.last.class) # [@post, @comment] # => "comment"
else options[:as] || singularizer.call(record_or_name_or_array.class) # @post => "post"
end
+ class_names << @@default_form_model_class_proc.call(model_class_name)
+
options[:html][:class] = class_names.compact.join(" ")
with_custom_field_error_proc do
@@ -191,8 +198,6 @@ def with_custom_field_error_proc(&block)
ensure
::ActionView::Base.field_error_proc = default_field_error_proc
end
-
-
end
end
end
View
16 lib/generators/templates/formtastic.rb
@@ -66,7 +66,21 @@
# specifying that class here. Defaults to Formtastic::FormBuilder.
# Formtastic::Helpers::FormHelper.builder = MyCustomBuilder
-# You can opt-in to Formtastic's use of the HTML5 `required` attribute on `<input>`, `<select>`
+# All formtastic forms have a class that indicates that they are just that. You
+# can change it to any class you want.
+# Formtastic::Helpers::FormHelper.default_form_class = 'formtastic'
+
+# Formtastic will infer a class name from the model, array, string ot symbol you pass to the
+# form builder. You can customize the way that class is presented by overriding
+# this proc.
+# Formtastic::Helpers::FormHelper.default_form_model_class_proc = proc { |model_class_name| model_class_name }
+
+# 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.
+# Formtastic::Helpers::FormHelper.formtastic_field_error_proc = proc { |html_tag, instance_tag| html_tag }
+
+# You can opt-in to Formtastic's use of the HTML5 `required` attribute on `<input>`, `<select>`
# and `<textarea>` tags by setting this to true (defaults to false).
# Formtastic::FormBuilder.use_required_attribute = false
View
25 spec/helpers/form_helper_spec.rb
@@ -23,7 +23,7 @@
end)
output_buffer.should have_tag("form.formtastic")
end
-
+
it 'adds a "novalidate" attribute to the generated form when configured to do so' do
with_config :perform_browser_validations, true do
concat(semantic_form_for(@new_post, :url => '/hello') do |builder|
@@ -31,7 +31,7 @@
output_buffer.should_not have_tag("form[@novalidate]")
end
end
-
+
it 'omits a "novalidate" attribute to the generated form when configured to do so' do
with_config :perform_browser_validations, false do
concat(semantic_form_for(@new_post, :url => '/hello') do |builder|
@@ -62,7 +62,7 @@
end)
output_buffer.should have_tag("form.xyz")
end
-
+
it 'omits the leading spaces from the classes in the generated form when the default class is nil' do
Formtastic::Helpers::FormHelper.default_form_class = nil
concat(semantic_form_for(::Post.new, :as => :post, :url => '/hello') do |builder|
@@ -102,6 +102,17 @@
output_buffer.should have_tag("form.namespaced_post")
end
+ it 'adds a customized class to the generated form' do
+ Formtastic::Helpers::FormHelper.default_form_model_class_proc = lambda { |model_class_name| "#{model_class_name}_form" }
+ concat(semantic_form_for(@new_post, :url => '/hello') do |builder|
+ end)
+ output_buffer.should have_tag("form.post_form")
+
+ concat(semantic_form_for(:project, :url => '/hello') do |builder|
+ end)
+ output_buffer.should have_tag("form.project_form")
+ end
+
describe 'allows :html options' do
before(:each) do
concat(semantic_form_for(@new_post, :url => '/hello', :html => { :id => "something-special", :class => "something-extra", :multipart => true }) do |builder|
@@ -156,15 +167,15 @@
::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
+ lambda do
Formtastic::Helpers::FormHelper.formtastic_field_error_proc = proc {""}
semantic_form_for(@new_post, :url => '/hello') { |builder| }
end.should_not change(::ActionView::Base, :field_error_proc)
end
- end
-
+ end
+
describe "with :builder option" do
it "yields an instance of the given builder" do
class MyAwesomeCustomBuilder < Formtastic::FormBuilder
Please sign in to comment.
Something went wrong with that request. Please try again.