Skip to content

Commit

Permalink
Was failing to turn contents Array into a String. Added a missing CSS…
Browse files Browse the repository at this point in the history
… class name. Added tests. Fixes mjbellantoni#4.
  • Loading branch information
Matthew Bellantoni committed Dec 1, 2011
1 parent 0af6cbd commit 9f45cb8
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
15 changes: 14 additions & 1 deletion README.md
Expand Up @@ -86,7 +86,7 @@ made to generate the HTML expected by Bootstrap while still generating the rich
</div>
</fieldset>
<div class="actions">
<input class="btn create" name="commit" type="submit" value="Create Post" />
<input class="btn create commit" name="commit" type="submit" value="Create Post" />
</div>
</form>

Expand All @@ -98,9 +98,12 @@ made to generate the HTML expected by Bootstrap while still generating the rich
* <tt>:date</tt> et al are tagged with the <tt>stringish</tt> class.
* Hidden fields are not generated.
* Fieldsets are simply nested.
* <tt>f.buttons :name</tt> is not supported. This generates a <tt>fieldset</tt> and <tt>legend</tt> tag which will cause the wrong thing to happen in Bootstrap.

Bootstrap is somewhat incomplete, and in a few cases an inference needed to be drawn to determine a course of action. If you disagree with any of these choices, feel free to let me know.

(At some point, this gem may also provide some additional CSS to cover cases left unaddressed by Bootstrap, but its obvious what should be done. That's not going to happen any time soon though!)

### Other

A lot of the code (especially the test suite) was copied over from Formtastic and then beaten into submission. I'm sure you'll find some ugliness in there. In general, I mimicked the Formtastic code structure as much as possible.
Expand All @@ -121,6 +124,16 @@ In particular:

## Contributing to formtastic-bootstrap

### Submitting Issues

If you're filing a bug, thank you! Secondly, in the report please include:

* The version of Formtastic you're using.
* The version of Twitter Bootstrap you're using.
* The code for your form.
* Anything else you think will help!

### Source Contributions
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
* Fork the project
Expand Down
9 changes: 7 additions & 2 deletions lib/formtastic-bootstrap/helpers/buttons_helper.rb
Expand Up @@ -5,8 +5,13 @@ module ButtonsHelper
include Formtastic::Helpers::ButtonsHelper

def buttons(*args, &block)

html_options = args.extract_options!
html_options[:class] ||= "actions"

if html_options.has_key?(:name)
ActiveSupport::Deprecation.warn('The :name option is not supported')
end

if block_given?
template.content_tag(:div, html_options) do
Expand All @@ -16,7 +21,7 @@ def buttons(*args, &block)
args = [:commit] if args.empty?
contents = args.map { |button_name| send(:"#{button_name}_button") }
template.content_tag(:div, html_options.except(:builder, :parent, :name)) do
Formtastic::Util.html_safe(contents)
Formtastic::Util.html_safe(contents.join)
end
end

Expand All @@ -30,7 +35,7 @@ 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.merge!(:class => [button_html[:class], "btn", commit_button_i18n_key].compact.join(' '))
button_html.merge!(:class => [button_html[:class], "btn commit", commit_button_i18n_key].compact.join(' '))

# TODO We don't have a wrapper. Add deprecation message.
# wrapper_html = options.delete(:wrapper_html) || {}
Expand Down
149 changes: 149 additions & 0 deletions spec/helpers/buttons_helper_spec.rb
@@ -0,0 +1,149 @@
# encoding: utf-8
require 'spec_helper'

describe 'Formtastic::FormBuilder#buttons' do

include FormtasticSpecHelper

before do
@output_buffer = ''
mock_everything
Formtastic::Helpers::FormHelper.builder = FormtasticBootstrap::FormBuilder
end

describe 'with a block' do
describe 'when no options are provided' do
before do
concat(semantic_form_for(@new_post) do |builder|
buttons = builder.buttons do
concat('hello')
end
concat(buttons)
end)
end

it 'should render a div inside the form, with a class of "actions"' do
output_buffer.should have_tag("form div.actions")
end

it 'should not render an ol inside the div' do
output_buffer.should_not have_tag("form div.actions ol")
end

it 'should render the contents of the block inside the input' do
output_buffer.should have_tag("form div.actions", /hello/)
end

it 'should not render a legend inside the div' do
output_buffer.should_not have_tag("form div.actions legend")
end
end

describe 'when other options are provided' do
before do
@id_option = 'advanced'
@class_option = 'wide'

concat(semantic_form_for(@new_post) do |builder|
builder.buttons :id => @id_option, :class => @class_option do
end
end)
end
it 'should pass the options into the div tag as attributes' do
output_buffer.should have_tag("form div##{@id_option}")
output_buffer.should have_tag("form div.#{@class_option}")
end
end

end

describe 'without a block' do

describe 'with no args (default buttons)' do

before do
concat(semantic_form_for(@new_post) do |builder|
concat(builder.buttons)
end)
end

it 'should render a form' do
output_buffer.should have_tag('form')
end

it 'should render a "actions" div inside the form' do
output_buffer.should have_tag('form div.actions')
end

it 'should not render a legend in the div' do
output_buffer.should_not have_tag('form div.actions legend')
end

it 'should render an button item in the ol for each default button' do
output_buffer.should have_tag('form div.actions input.btn', :count => 1)
end

it 'should render a commit list item for the commit button' do
output_buffer.should have_tag('form div.actions input.commit')
end

end

describe 'with button names as args' do

before do
concat(semantic_form_for(@new_post) do |builder|
concat(builder.buttons(:commit))
end)
end

it 'should render a form with a div containing an input for each button arg' do
output_buffer.should have_tag('form > div.actions > input', :count => 1)
output_buffer.should have_tag('form > div.actions > input.commit')
end

end

describe 'with :names' do

before do
ActiveSupport::Deprecation.should_receive(:warn)
concat(
semantic_form_for(@new_post) do |builder|
concat(builder.buttons(:commit, :name => "Now click a button"))
end
)
end

it 'should warn that \':name\' is not supported' do
# Assertion is above in the before block.
end

end


describe 'with button names and an options hash' do

before do
concat(
semantic_form_for(@new_post) do |builder|
concat(builder.buttons(:commit, :id => "my-id"))
end
)
end

it 'should render a form with a div containing a input for each button arg' do
output_buffer.should have_tag('form > div.actions > input', :count => 1)
output_buffer.should have_tag('form > div.actions > input.commit', :count => 1)
end

it 'should pass the options down to the div' do
output_buffer.should have_tag('form > div#my-id.actions')
end

end

end

end

0 comments on commit 9f45cb8

Please sign in to comment.