Skip to content

Commit 0b66cf6

Browse files
committed
Allow f.button :submit to accept a block.
1 parent c14d25c commit 0b66cf6

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## master
22

33
### enhancements
4+
* It allows `submit` buttons to accept blocks. When a block is specified, the submit button will
5+
use a \<button> tag instead of an \<input> tag. [@lowjoel](https://github.com/lowjoel)
46

57
### bug fixes
68

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,10 @@ end
2727
1. You can still put configuration settings in, but it should no longer be necessary.
2828

2929
4. Restart your development server.
30+
31+
## Features
32+
33+
1. It adds the appropriate button styling to all buttons. By default, the `btn` and `btn-default`
34+
styles are added to all buttons. Submit buttons get `btn-primary` instead of `btn-default`.
35+
2. It allows `submit` buttons to accept blocks. When a block is specified, the submit button will
36+
use a \<button> tag instead of an \<input> tag.

lib/simple_form/bootstrap/form_builders/button.rb

+18
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ def button(type, *args, &proc)
1919

2020
super(type, *args, &proc)
2121
end
22+
23+
# Creates a submit button.
24+
#
25+
# This augments the original button implementation to generate a button element
26+
# with a submit action when a block is given. Otherwise, it falls back to the
27+
# original submit helper.
28+
def submit_button(*args, &block)
29+
if block_given?
30+
options = args.extract_options!.dup
31+
options[:type] = :submit
32+
options[:name] ||= 'commit'
33+
args << options
34+
button_button(options, &block)
35+
else
36+
submit(*args)
37+
end
38+
end
39+
2240
end
2341

2442
SimpleForm::FormBuilder.class_eval do

spec/simple_form/bootstrap/form_builders/button_spec.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ def test
1616
let(:button_type) { :button }
1717
let(:button_text) { 'Button!' }
1818
let(:button_options) { {} }
19+
let(:button_block) { nil }
1920
subject! do
2021
object = Button.new
2122
simple_form_for object, url: 'test' do |f|
22-
f.button button_type, button_text, button_options
23+
f.button button_type, button_text, button_options, &button_block
2324
end
2425
render text: output_buffer
2526
end
@@ -67,5 +68,18 @@ def test
6768
expect(rendered).to have_tag('input.btn.btn-primary', with: { value: button_text })
6869
end
6970
end
71+
72+
context 'when a block is given' do
73+
let(:proc_text) { 'I am in the proc!' }
74+
let(:button_block) do
75+
proc do
76+
proc_text
77+
end
78+
end
79+
80+
it 'renders the block' do
81+
expect(rendered).to have_tag('button', text: proc_text)
82+
end
83+
end
7084
end
7185
end

0 commit comments

Comments
 (0)