Skip to content

Commit 9440708

Browse files
author
Matthew Bellantoni
committed
Clean up and refactor RadioInput and CheckBoxesInput.
1 parent a1f754e commit 9440708

File tree

3 files changed

+26
-78
lines changed

3 files changed

+26
-78
lines changed

lib/formtastic-bootstrap/inputs/base/choices.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ module Inputs
33
module Base
44
module Choices
55
include Formtastic::Inputs::Base::Choices
6+
7+
def choice_wrapping_html_options(choice)
8+
super(choice).tap do |options|
9+
options[:for] = choice_input_dom_id(choice)
10+
end
11+
end
12+
13+
def choice_label_html_options(choice)
14+
choice_wrapping_html_options(choice)
15+
end
16+
617
end
718
end
819
end

lib/formtastic-bootstrap/inputs/check_boxes_input.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module FormtasticBootstrap
22
module Inputs
33
class CheckBoxesInput < Formtastic::Inputs::CheckBoxesInput
44
include Base
5+
include Base::Choices
56

67
# TODO Make sure help blocks work correctly.
78
# TODO Support .inline
@@ -21,14 +22,9 @@ def to_html
2122
def choice_wrapping_html_options(choice)
2223
super(choice).tap do |options|
2324
options[:class] = ((options[:class].split) << "checkbox").join(" ")
24-
options[:for] = choice_input_dom_id(choice)
2525
end
2626
end
2727

28-
def choice_label_html_options(choice)
29-
choice_wrapping_html_options(choice)
30-
end
31-
3228
def choice_html(choice)
3329
template.content_tag(:label,
3430
hidden_fields? ?

lib/formtastic-bootstrap/inputs/radio_input.rb

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,102 +4,43 @@ class RadioInput < Formtastic::Inputs::RadioInput
44
include Base
55
include Base::Choices
66

7-
#
8-
97
# TODO Make sure help blocks work correctly.
108
# TODO Support .inline
119

12-
# def to_html
13-
# control_group_wrapping do
14-
# control_label_html <<
15-
# controls_wrapping do
16-
# choices_group_wrapping do
17-
# collection.map { |choice|
18-
# choice_wrapping(choice_wrapping_html_options(choice)) do
19-
# choice_html(choice)
20-
# end
21-
# }.join("\n").html_safe
22-
# end
23-
# end
24-
# end
25-
26-
# CHECKBOXES FROM THIS VERSION OF FB
27-
def to_html
28-
control_group_wrapping do
29-
control_label_html <<
30-
# hidden_field_for_all <<
31-
controls_wrapping do
32-
collection.map { |choice|
33-
choice_html(choice)
34-
}.join("\n").html_safe
35-
end
10+
def to_html
11+
control_group_wrapping do
12+
control_label_html <<
13+
controls_wrapping do
14+
collection.map { |choice|
15+
choice_html(choice)
16+
}.join("\n").html_safe
3617
end
3718
end
19+
end
3820

39-
def wrapper_html_options
40-
# Formtastic marks these as 'radio' but Bootstrap does something
41-
# with that, so change it to 'radio_buttons'.
42-
super.tap do |options|
43-
options[:class] = options[:class].gsub("radio", "radio_buttons")
44-
end
21+
def wrapper_html_options
22+
# Formtastic marks these as 'radio' but Bootstrap does something
23+
# with that, so change it to 'radio_buttons'.
24+
super.tap do |options|
25+
options[:class] = options[:class].gsub("radio", "radio_buttons")
4526
end
46-
47-
# FROM LAST VERSION OF FB
48-
# bootstrap_vertical_form_input_wrapping do
49-
# legend_html <<
50-
# input_div_wrapping do
51-
# choices_group_wrapping do
52-
# collection.map { |choice|
53-
# choice_wrapping(choice_wrapping_html_options(choice)) do
54-
# choice_html(choice)
55-
# end
56-
# }.join("\n").html_safe
57-
# end
58-
# end
59-
# end
60-
# end
27+
end
6128

6229
# This came from check_boxes. Do needed refactoring.
6330
def choice_wrapping_html_options(choice)
6431
super(choice).tap do |options|
6532
options[:class] = ((options[:class].split) << "radio").join(" ")
66-
options[:for] = choice_input_dom_id(choice)
6733
end
6834
end
6935

70-
# This came from check_boxes. Do needed refactoring.
71-
def choice_label_html_options(choice)
72-
choice_wrapping_html_options(choice)
73-
end
74-
75-
# From FB1.x
76-
# def choice_html(choice)
77-
# template.content_tag(:label, label_html_options.merge(:for => choice_input_dom_id(choice), :class => nil)) do
78-
# builder.radio_button(input_name, choice_value(choice), input_html_options.merge(choice_html_options(choice)).merge(:required => false)) <<
79-
#
80-
# choice_label(choice)
81-
# end
82-
# end
83-
84-
85-
# FROM F2.2
8636
def choice_html(choice)
8737
template.content_tag(:label,
8838
builder.radio_button(input_name, choice_value(choice), input_html_options.merge(choice_html_options(choice)).merge(:required => false)) <<
8939
choice_label(choice),
90-
# label_html_options.merge(:for => choice_input_dom_id(choice), :class => nil)
9140
label_html_options.merge(choice_label_html_options(choice))
9241
)
9342
end
9443

95-
# Override to remove the for attribute since this isn't associated with any element, as it's
96-
# nested inside the legend.
97-
def label_html_options
98-
super.merge(:for => nil)
99-
end
100-
101-
102-
10344
end
10445
end
10546
end

0 commit comments

Comments
 (0)