Class: Effective::FormInputs::Radios
Constant Summary
Effective::FormInput::BLANK, Effective::FormInput::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS
Instance Attribute Summary
#name, #options
Instance Method Summary
collapse
#assign_options_collection!, #assign_options_collection_methods!, #collection_options, #custom?, #group_label_method, #group_method, #grouped?, #html_options, #initialize, #inline?, #label_method, #option_key_method, #option_value_method, #options_collection, #polymorphic?, #polymorphic_id_method, #polymorphic_id_value, #polymorphic_type_method, #polymorphic_type_value, #polymorphic_value, #translate, #value_method
#hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html
Instance Method Details
#active_item?(builder) ⇒ Boolean
170
171
172
173
|
# File 'app/models/effective/form_inputs/radios.rb', line 170
def active_item?(builder)
value = self.value || collection_options[:checked]
value == builder.value || Array(value).map(&:to_s) == Array(builder.value).map(&:to_s)
end
|
35
36
37
38
39
40
41
42
43
|
# File 'app/models/effective/form_inputs/radios.rb', line 35
def build_button_group(&block)
if buttons?
content_tag(:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'buttons')
elsif cards?
content_tag(:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'cards')
else
yield
end
end
|
23
24
25
26
27
28
29
30
31
32
33
|
# File 'app/models/effective/form_inputs/radios.rb', line 23
def build_input(&block)
build_button_group do
html = @builder.collection_radio_buttons(name, options_collection, value_method, label_method, collection_options, item_input_options) { |builder| build_item(builder) }
if disabled? html = html.sub('<input type="hidden"', '<input type="hidden" disabled="disabled"').html_safe
end
html
end
end
|
#build_item(builder) ⇒ Object
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'app/models/effective/form_inputs/radios.rb', line 103
def build_item(builder)
item_id = unique_id(builder.object)
if buttons?
opts = item_label_options.merge(for: item_id)
opts[:class] = [opts[:class], ('active' if active_item?(builder)), ('first-button' if first_item?) ].compact.join(' ')
builder.label(opts) { builder.radio_button(id: item_id) + builder.text }
elsif cards?
opts = item_label_options.merge(for: item_id)
opts[:class] = [opts[:class], ('active border-secondary' if active_item?(builder)), ('first-card' if first_item?) ].compact.join(' ')
if active_item?(builder)
builder.label(opts) { builder.radio_button(id: item_id) + builder.text.sub('card-header', 'card-header bg-secondary text-white').html_safe }
else
builder.label(opts) { builder.radio_button(id: item_id) + builder.text }
end
else
build_item_wrap { builder.radio_button(id: item_id) + builder.label(item_label_options.merge(for: item_id)) }
end
end
|
#build_item_wrap(&block) ⇒ Object
125
126
127
128
129
130
131
132
133
|
# File 'app/models/effective/form_inputs/radios.rb', line 125
def build_item_wrap(&block)
if cards?
content_tag(:div, yield, class: 'card')
elsif custom?
content_tag(:div, yield, class: 'custom-control custom-radio ' + (inline? ? 'custom-control-inline' : 'form-group'))
else
content_tag(:div, yield, class: 'form-check' + (inline? ? ' form-check-inline' : ''))
end
end
|
#build_label ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'app/models/effective/form_inputs/radios.rb', line 85
def build_label
return BLANK if options[:label] == false
return BLANK if name.kind_of?(NilClass)
tag = (buttons? || inline?) ? :label : :legend
text = (options[:label].delete(:text) || (object.class.human_attribute_name(name) if object) || BLANK).html_safe
if buttons?
content_tag(:label, text, options[:label].merge(for: button_group_id))
elsif cards?
content_tag(:label, text, options[:label].merge(for: button_group_id))
elsif inline?
content_tag(:label, text, options[:label])
else
content_tag(:legend, text, options[:label])
end
end
|
#build_wrapper(&block) ⇒ Object
13
14
15
16
17
18
19
20
21
|
# File 'app/models/effective/form_inputs/radios.rb', line 13
def build_wrapper(&block)
tag = (buttons? || cards?) ? :div : :fieldset
if layout == :horizontal
content_tag(tag, content_tag(:div, yield, class: 'row'), options[:wrapper])
else
content_tag(tag, yield, options[:wrapper])
end
end
|
53
54
55
56
57
58
59
60
61
62
|
# File 'app/models/effective/form_inputs/radios.rb', line 53
def button_group_class
[
'effective-radios',
('btn-group btn-group-toggle' if buttons?),
('card-deck' if cards?),
('no-feedback' unless feedback_options),
('is-invalid' if feedback_options && has_error?(name)),
('is-valid' if feedback_options && has_error? && !has_error?(name))
].compact.join(' ')
end
|
161
162
163
|
# File 'app/models/effective/form_inputs/radios.rb', line 161
def button_group_id
"#{tag_id}_btn_group"
end
|
151
152
153
154
|
# File 'app/models/effective/form_inputs/radios.rb', line 151
def buttons? return @buttons unless @buttons.nil?
@buttons = (options.delete(:buttons) || false)
end
|
#cards? ⇒ Boolean
156
157
158
159
|
# File 'app/models/effective/form_inputs/radios.rb', line 156
def cards? return @cards unless @cards.nil?
@cards = (options.delete(:cards) || false)
end
|
#feedback_options ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'app/models/effective/form_inputs/radios.rb', line 64
def feedback_options
return false if layout == :inline
{
valid: { class: 'valid-feedback' }.compact,
invalid: { class: 'invalid-feedback' }.compact
}
end
|
#first_item? ⇒ Boolean
165
166
167
168
|
# File 'app/models/effective/form_inputs/radios.rb', line 165
def first_item?
return false unless @first_item.nil?
@first_item = true
end
|
73
74
75
76
77
78
79
80
81
82
83
|
# File 'app/models/effective/form_inputs/radios.rb', line 73
def input_html_options
if buttons?
{ autocomplete: 'off' }
elsif cards?
{ autocomplete: 'off' }
elsif custom?
{ class: 'custom-control-input' }
else
{ class: 'form-check-input' }
end
end
|
135
136
137
|
# File 'app/models/effective/form_inputs/radios.rb', line 135
def item_input_options
options[:input].except(:inline, :custom, :buttons)
end
|
#item_label_options ⇒ Object
139
140
141
142
143
144
145
146
147
148
149
|
# File 'app/models/effective/form_inputs/radios.rb', line 139
def item_label_options
if buttons?
{ class: 'btn btn-outline-secondary' }
elsif cards?
{ class: 'card' }
elsif custom?
{ class: 'custom-control-label' }
else
{ class: 'form-check-label' }
end
end
|
#wrapper_options ⇒ Object
45
46
47
48
49
50
51
|
# File 'app/models/effective/form_inputs/radios.rb', line 45
def wrapper_options
if buttons? || cards?
{ class: "form-group #{tag_id}" }
else
{ class: "form-group #{tag_id} #{button_group_class}" }
end
end
|