Skip to content

Commit

Permalink
Refactored button_to to reuse the form_tag helper. Moved it to `f…
Browse files Browse the repository at this point in the history
…orm_helpers` to have it on the same namespace (it kind of makes sense for it to be in there) and added some tests to it as well.
  • Loading branch information
Darío Javier Cravero committed Mar 14, 2013
1 parent b6ddd16 commit 3f52b59
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 43 deletions.
43 changes: 0 additions & 43 deletions padrino-helpers/lib/padrino-helpers/asset_tag_helpers.rb
Expand Up @@ -106,49 +106,6 @@ def link_to(*args, &block)
end
end

##
# Creates a form containing a single button that submits to the url.
#
# @overload button_to(name, url, options={})
# @param [String] caption The text caption.
# @param [String] url The url href.
# @param [Hash] options The html options.
# @overload button_to(name, options={}, &block)
# @param [String] url The url href.
# @param [Hash] options The html options.
# @param [Proc] block The button content.
#
# @option options [Boolean] :multipart
# If true, this form will support multipart encoding.
# @option options [String] :remote
# Instructs ujs handler to handle the submit as ajax.
# @option options [Symbol] :method
# Instructs ujs handler to use different http method (i.e :post, :delete).
#
# @return [String] Form and button html with specified +options+.
#
# @example
# button_to 'Delete', url(:accounts_destroy, :id => account), :method => :delete, :class => :form
# # Generates:
# # <form class="form" action="/admin/accounts/destroy/2" method="post">
# # <input type="hidden" value="delete" name="_method" />
# # <input type="submit" value="Delete" />
# # </form>
#
# @api public
def button_to(*args, &block)
name, url = args[0], args[1]
options = args.extract_options!
desired_method = options[:method]
options.delete(:method) if options[:method].to_s !~ /get|post/i
options.reverse_merge!(:method => 'post', :action => url)
options[:enctype] = 'multipart/form-data' if options.delete(:multipart)
options['data-remote'] = 'true' if options.delete(:remote)
inner_form_html = hidden_form_method_field(desired_method)
inner_form_html += block_given? ? capture_html(&block) : submit_tag(name)
content_tag('form', inner_form_html, options)
end

##
# Creates a link tag that browsers and news readers can use to auto-detect an RSS or ATOM feed.
#
Expand Down
43 changes: 43 additions & 0 deletions padrino-helpers/lib/padrino-helpers/form_helpers.rb
Expand Up @@ -713,6 +713,49 @@ def csrf_token_field(token = nil)
hidden_field_tag :authenticity_token, :value => token
end

##
# Creates a form containing a single button that submits to the url.
#
# @overload button_to(name, url, options={})
# @param [String] caption The text caption.
# @param [String] url The url href.
# @param [Hash] options The html options.
# @overload button_to(name, options={}, &block)
# @param [String] url The url href.
# @param [Hash] options The html options.
# @param [Proc] block The button content.
#
# @option options [Boolean] :multipart
# If true, this form will support multipart encoding.
# @option options [String] :remote
# Instructs ujs handler to handle the submit as ajax.
# @option options [Symbol] :method
# Instructs ujs handler to use different http method (i.e :post, :delete).
#
# @return [String] Form and button html with specified +options+.
#
# @example
# button_to 'Delete', url(:accounts_destroy, :id => account), :method => :delete, :class => :form
# # Generates:
# # <form class="form" action="/admin/accounts/destroy/2" method="post">
# # <input type="hidden" value="delete" name="_method" />
# # <input type="submit" value="Delete" />
# # </form>
#
# @api public
def button_to(*args, &block)
name, url = args[0], args[1]
options = args.extract_options!
options['data-remote'] = 'true' if options.delete(:remote)
if block_given?
form_tag(url, options, &block)
else
form_tag(url, options) do
submit_tag(name)
end
end
end

protected

##
Expand Down
21 changes: 21 additions & 0 deletions padrino-helpers/test/test_form_helpers.rb
Expand Up @@ -781,4 +781,25 @@ def app
assert_have_selector 'form.advanced-form input[type=image]', :count => 1, :src => "/images/buttons/submit.png?#{@stamp}"
end
end

context 'for #button_to method' do
should "have a form and set the method properly" do
actual_html = button_to('Delete', '/users/1', :method => :delete)
assert_has_tag('form', :action => '/users/1') { actual_html }
assert_has_tag('form input', :type => 'hidden', :name => "_method", :value => 'delete') { actual_html }
assert_has_tag('form input', :type => 'hidden', :name => "authenticity_token") { actual_html }
end

should "add a submit button by default if no content is specified" do
actual_html = button_to('My Delete Button', '/users/1', :method => :delete)
assert_has_tag('form input', :type => 'submit', :value => 'My Delete Button') { actual_html }
end

should "set specific content inside the form if a block was sent" do
actual_html = button_to('My Delete Button', '/users/1', :method => :delete) do
content_tag :button, "My button's content", :type => :submit, :title => "My button"
end
assert_has_tag('form button', :type => 'submit', :content => "My button's content", :title => "My button") { actual_html }
end
end
end

0 comments on commit 3f52b59

Please sign in to comment.