Skip to content

Commit

Permalink
Merge pull request #1332 from sshaw/form_builder_namespace
Browse files Browse the repository at this point in the history
introduce :namespace option to abstract form builder, closes #1327
  • Loading branch information
ujifgc committed Jul 8, 2013
2 parents 209cae4 + 779f5e2 commit cbd7a35
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Expand Up @@ -215,7 +215,9 @@ def field_name(field=nil)
# field_name(:number) => "user_telephone_attributes_number"
# field_name(:street) => "user_addresses_attributes_0_street"
def field_id(field=nil, value=nil)
result = field_result
result = []
result << "#{@options[:namespace]}_" if @options[:namespace] && root_form?
result << field_result
result << field_id_fragment if nested_form?
result << "_#{field}" unless field.blank?
result << "_#{value}" unless value.blank?
Expand Down
9 changes: 6 additions & 3 deletions padrino-helpers/lib/padrino-helpers/form_helpers.rb
Expand Up @@ -14,7 +14,9 @@ module FormHelpers
# @param [String] url
# The url this form will submit to.
# @param [Hash] settings
# The settings associated with this form. Accepts html options.
# The settings associated with this form.
# Accepts a :namespace option that will be prepended to the id attributes of the form's elements.
# Also accepts html options.
# @option settings [String] :builder ("StandardFormBuilder")
# The FormBuilder class to use such as StandardFormBuilder.
# @param [Proc] block
Expand All @@ -32,6 +34,7 @@ module FormHelpers
def form_for(object, url, settings={}, &block)
instance = builder_instance(object, settings)
html = capture_html(instance, &block)
settings.delete(:namespace)
form_tag(url, settings) { html }
end

Expand Down Expand Up @@ -79,7 +82,7 @@ def fields_for(object, settings={}, &block)
def form_tag(url, options={}, &block)
desired_method = options[:method].to_s
options.delete(:method) unless desired_method =~ /get|post/i
options.reverse_merge!(:method => 'post',
options.reverse_merge!(:method => 'post',
:action => url,
:protect_from_csrf => is_protected_from_csrf? )
options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
Expand Down Expand Up @@ -255,7 +258,7 @@ def error_message_on(object, field, options={})
error = if defined?(Ohm::Model) && object.is_a?(Ohm::Model)
I18n.t("ohm.errors.messages.#{error[0]}", :default => error[0].to_s)
else
# Array(error).first is necessary because some ORMs
# Array(error).first is necessary because some ORMs
# give us an array others directly a value
Array(error)[0]
end
Expand Down
10 changes: 10 additions & 0 deletions padrino-helpers/test/test_form_builder.rb
Expand Up @@ -61,6 +61,16 @@ def standard_builder(object=@user)
assert_has_tag('form', :"accept-charset" => "UTF-8", :action => '/update', :method => 'post', "data-remote" => 'true') { actual_html }
end

should "display correct form html with namespace option" do
actual_html = form_for(@user, '/update', :namespace => 'foo') do |f|
f.text_field(:first_name) << f.fields_for(:role_types) { |role| role.text_field(:name) }
end

assert_has_no_tag(:form, :namespace => 'foo') { actual_html }
assert_has_tag(:input, :type => 'text', :name => 'user[first_name]', :id => 'foo_user_first_name') { actual_html }
assert_has_tag(:input, :type => 'text', :name => 'user[role_types_attributes][0][name]', :id => 'foo_user_role_types_attributes_0_name') { actual_html }
end

should "display correct form html with remote option and method put" do
actual_html = form_for(@user, '/update', :"accept-charset" => "UTF-8", :remote => true, :method => 'put') { "Demo" }
assert_has_tag('form', :"accept-charset" => "UTF-8", :method => 'post', "data-remote" => 'true') { actual_html }
Expand Down

0 comments on commit cbd7a35

Please sign in to comment.