Skip to content

Commit

Permalink
Merge pull request formtastic#760 from nashby/fix-issue-686
Browse files Browse the repository at this point in the history
use id for commit_button like rails2 did it, closes formtastic#686
  • Loading branch information
sobrinho committed Dec 17, 2011
2 parents ee5528e + 094dae2 commit 63528a7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
11 changes: 6 additions & 5 deletions lib/formtastic/helpers/buttons_helper.rb
Expand Up @@ -246,14 +246,15 @@ def commit_button(*args)
Formtastic::I18n.t(commit_button_i18n_key, :model => commit_button_object_name)) unless text.is_a?(::String)

button_html = options.delete(:button_html) || {}
button_html[:id] ||= "#{@object_name}_submit"
button_html.merge!(:class => [button_html[:class], commit_button_i18n_key].compact.join(' '))

wrapper_html = options.delete(:wrapper_html) || {}
wrapper_html[:class] = (commit_button_wrapper_html_class << wrapper_html[:class]).flatten.compact.join(' ')

accesskey = (options.delete(:accesskey) || default_commit_button_accesskey) unless button_html.has_key?(:accesskey)
button_html = button_html.merge(:accesskey => accesskey) if accesskey

template.content_tag(:li, Formtastic::Util.html_safe(submit(text, button_html)), wrapper_html)
end

Expand All @@ -274,10 +275,10 @@ def commit_button_object_name
else
object_name = @object_name.to_s.send(label_str_method)
end

object_name
end

def commit_button_i18n_key
if new_or_persisted_object?
key = @object.persisted? ? :update : :create
Expand All @@ -291,11 +292,11 @@ def commit_button_i18n_key
def commit_button_wrapper_html_class
['commit', 'button'] # TODO: Add class reflecting on form action.
end

def new_or_persisted_object?
@object && (@object.respond_to?(:persisted?) || @object.respond_to?(:new_record?))
end

end
end
end
29 changes: 18 additions & 11 deletions spec/helpers/commit_button_helper_spec.rb
Expand Up @@ -29,7 +29,7 @@
end

end

describe 'when not persisted' do

before do
Expand All @@ -44,12 +44,12 @@
concat(builder.commit_button)
end)
end

output_buffer.should have_tag('.commit input[@value="Submit Post"]')
end
end


describe 'when used on any record' do

before do
Expand All @@ -62,7 +62,7 @@
it 'should render a commit li' do
output_buffer.should have_tag('li.commit')
end

it 'should render a button li' do
output_buffer.should have_tag('li.button')
end
Expand Down Expand Up @@ -204,11 +204,11 @@
}
}
end

after do
::I18n.backend.reload!
end

it 'should render an input with localized label (I18n)' do
with_config :i18n_lookups_by_default, true do
::I18n.backend.store_translations :en,
Expand All @@ -225,7 +225,7 @@
output_buffer.should have_tag(%Q{li.commit input[@value="Custom Submit Post"][@class~="submit"]})
end
end

it 'should render an input with anoptional localized label (I18n) - if first is not set' do
with_config :i18n_lookups_by_default, true do
concat(semantic_form_for(:post, :url => 'http://example.com') do |builder|
Expand Down Expand Up @@ -427,7 +427,7 @@ def self.human_name
end

end

describe ':wrapper_html option' do

describe 'when provided' do
Expand Down Expand Up @@ -457,14 +457,21 @@ def self.human_name
end

describe 'when not provided' do
it 'should use default id and class' do
it 'should use default class' do
concat(semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text'))
end)
output_buffer.should have_tag("form li.commit.button")
end

it 'should use default id' do
concat(semantic_form_for(@new_post) do |builder|
concat(builder.commit_button('text'))
end)
output_buffer.should have_tag("form li.commit.button input#post_submit")
end
end

end

end

0 comments on commit 63528a7

Please sign in to comment.