Class: Effective::FormInputs::Radios
Constant Summary
Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES
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, #value_method
#hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html
Instance Method Details
#active_item?(builder) ⇒ Boolean
118
119
120
121
|
# File 'app/models/effective/form_inputs/radios.rb', line 118
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
|
22
23
24
25
26
27
28
|
# File 'app/models/effective/form_inputs/radios.rb', line 22
def build_button_group(&block)
if buttons?
content_tag(:div, yield, id: button_group_id, class: 'btn-group btn-group-toggle effective-radios', 'data-toggle': 'buttons')
else
yield
end
end
|
16
17
18
19
20
|
# File 'app/models/effective/form_inputs/radios.rb', line 16
def build_input(&block)
build_button_group do
@builder.collection_radio_buttons(name, options_collection, value_method, label_method, collection_options, item_input_options) { |builder| build_item(builder) }
end
end
|
#build_item(builder) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'app/models/effective/form_inputs/radios.rb', line 69
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 }
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
82
83
84
85
86
87
88
|
# File 'app/models/effective/form_inputs/radios.rb', line 82
def build_item_wrap(&block)
if 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'app/models/effective/form_inputs/radios.rb', line 53
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 inline?
content_tag(:label, text, options[:label])
else
content_tag(:legend, text, options[:label])
end
end
|
#build_wrapper(&block) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'app/models/effective/form_inputs/radios.rb', line 6
def build_wrapper(&block)
tag = buttons? ? :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
|
109
110
111
|
# File 'app/models/effective/form_inputs/radios.rb', line 109
def button_group_id
"#{tag_id}_btn_group"
end
|
104
105
106
107
|
# File 'app/models/effective/form_inputs/radios.rb', line 104
def buttons?
return @buttons unless @buttons.nil?
@buttons = (options.delete(:buttons) || false)
end
|
#feedback_options ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'app/models/effective/form_inputs/radios.rb', line 34
def feedback_options
return false if layout == :inline
{
valid: { class: 'valid-feedback', style: ('display: block;' if has_error? && !has_error?(name)) }.compact,
invalid: { class: 'invalid-feedback', style: ('display: block;' if has_error?(name)) }.compact
}
end
|
#first_item? ⇒ Boolean
113
114
115
116
|
# File 'app/models/effective/form_inputs/radios.rb', line 113
def first_item?
return false unless @first_item.nil?
@first_item = true
end
|
43
44
45
46
47
48
49
50
51
|
# File 'app/models/effective/form_inputs/radios.rb', line 43
def input_html_options
if buttons?
{ autocomplete: 'off' }
elsif custom?
{ class: 'custom-control-input' }
else
{ class: 'form-check-input' }
end
end
|
90
91
92
|
# File 'app/models/effective/form_inputs/radios.rb', line 90
def item_input_options
options[:input].except(:inline, :custom, :buttons)
end
|
#item_label_options ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'app/models/effective/form_inputs/radios.rb', line 94
def item_label_options
if buttons?
{ class: 'btn btn-outline-secondary' }
elsif custom?
{ class: 'custom-control-label' }
else
{ class: 'form-check-label' }
end
end
|
#wrapper_options ⇒ Object
30
31
32
|
# File 'app/models/effective/form_inputs/radios.rb', line 30
def wrapper_options
{ class: "form-group #{tag_id}" }
end
|