Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Commit

Permalink
Hardcode classes for radios / checkboxes
Browse files Browse the repository at this point in the history
These are dynamically inserted, but adding them statically makes for a better perf story – the custom inputs can render immediately instead of waiting for the JS to kick in.
  • Loading branch information
Robin Whittleton committed Nov 29, 2016
1 parent 00b49b7 commit 88291a0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/verify_form_builder.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class VerifyFormBuilder < ActionView::Helpers::FormBuilder
# Our custom radio / checkbox implementation
def custom_radio_button key, value, text, attributes = {}
label "#{key}_#{value.to_s.parameterize}", class: 'block-label' do
label "#{key}_#{value.to_s.parameterize}", class: 'block-label selection-button-radio' do
input = radio_button key, value, attributes
"#{input} #{text}".html_safe
end
end

def custom_check_box key, attributes, true_value, false_value, text
label key, class: 'block-label' do
label key, class: 'block-label selection-button-checkbox' do
input = check_box key, attributes, true_value, false_value
"#{input} #{text}".html_safe
end
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/verify_form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,47 @@
foo = double(:foo)
expect(foo).to receive(:a).and_return 'b'
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a_b"><input type="radio" value="b" checked="checked" name="foo[a]" id="foo_a_b" /> c</label>'
expected_html = '<label class="block-label selection-button-radio" for="foo_a_b"><input type="radio" value="b" checked="checked" name="foo[a]" id="foo_a_b" /> c</label>'
expect(form_builder.custom_radio_button(:a, 'b', 'c', {})).to eql expected_html
end

it 'creates the custom radio button with value set on object but is unchecked when does not match object' do
foo = double(:foo)
expect(foo).to receive(:a).and_return 'd'
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a_b"><input type="radio" value="b" name="foo[a]" id="foo_a_b" /> c</label>'
expected_html = '<label class="block-label selection-button-radio" for="foo_a_b"><input type="radio" value="b" name="foo[a]" id="foo_a_b" /> c</label>'
expect(form_builder.custom_radio_button(:a, 'b', 'c', {})).to eql expected_html
end

it 'allows us to set html attributes against a custom radio button' do
foo = double(:foo)
expect(foo).to receive(:a).and_return nil
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a_b"><input required="required" type="radio" value="b" name="foo[a]" id="foo_a_b" /> c</label>'
expected_html = '<label class="block-label selection-button-radio" for="foo_a_b"><input required="required" type="radio" value="b" name="foo[a]" id="foo_a_b" /> c</label>'
expect(form_builder.custom_radio_button(:a, 'b', 'c', required: true)).to eql expected_html
end

it 'creates the custom check box with value set on object and is checked when matches object' do
foo = double(:foo)
expect(foo).to receive(:a).and_return 'b'
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input type="checkbox" value="b" checked="checked" name="foo[a]" id="foo_a" /> d</label>'
expected_html = '<label class="block-label selection-button-checkbox" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input type="checkbox" value="b" checked="checked" name="foo[a]" id="foo_a" /> d</label>'
expect(form_builder.custom_check_box(:a, {}, 'b', 'c', 'd')).to eql expected_html
end

it 'creates the custom check box with value set on object but is unchecked when does not match object' do
foo = double(:foo)
expect(foo).to receive(:a).and_return 'd'
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input type="checkbox" value="b" name="foo[a]" id="foo_a" /> d</label>'
expected_html = '<label class="block-label selection-button-checkbox" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input type="checkbox" value="b" name="foo[a]" id="foo_a" /> d</label>'
expect(form_builder.custom_check_box(:a, {}, 'b', 'c', 'd')).to eql expected_html
end

it 'allows us to set html attributes against a custom check box' do
foo = double(:foo)
expect(foo).to receive(:a).and_return nil
form_builder = VerifyFormBuilder.new('foo', foo, template, {})
expected_html = '<label class="block-label" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input required="required" type="checkbox" value="b" name="foo[a]" id="foo_a" /> d</label>'
expected_html = '<label class="block-label selection-button-checkbox" for="foo_a"><input name="foo[a]" type="hidden" value="c" /><input required="required" type="checkbox" value="b" name="foo[a]" id="foo_a" /> d</label>'
expect(form_builder.custom_check_box(:a, { required: true }, 'b', 'c', 'd')).to eql expected_html
end
end

0 comments on commit 88291a0

Please sign in to comment.