Class: Effective::FormInputs::Radios

Inherits:
CollectionInput show all
Defined in:
app/models/effective/form_inputs/radios.rb

Constant Summary

Constants inherited from Effective::FormInput

Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES

Instance Attribute Summary

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from CollectionInput

#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

Methods inherited from Effective::FormInput

#hint_options, #initialize, #input_group_options, #input_js_options, #label_options, #to_html

Constructor Details

This class inherits a constructor from Effective::FormInputs::CollectionInput

Instance Method Details

#active_item?(builder) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
165
# File 'app/models/effective/form_inputs/radios.rb', line 162

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

#build_button_group(&block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'app/models/effective/form_inputs/radios.rb', line 27

def build_button_group(&block)
  if buttons?
    (:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'buttons')
  elsif cards?
    (:div, yield, id: button_group_id, class: button_group_class, 'data-toggle': 'cards')
  else
    yield
  end
end

#build_input(&block) ⇒ Object



21
22
23
24
25
# File 'app/models/effective/form_inputs/radios.rb', line 21

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/effective/form_inputs/radios.rb', line 95

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



117
118
119
120
121
122
123
124
125
# File 'app/models/effective/form_inputs/radios.rb', line 117

def build_item_wrap(&block)
  if cards?
    (:div, yield, class: 'card')
  elsif custom?
    (:div, yield, class: 'custom-control custom-radio ' + (inline? ? 'custom-control-inline' : 'form-group'))
  else
    (:div, yield, class: 'form-check' + (inline? ? ' form-check-inline' : ''))
  end
end

#build_labelObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/effective/form_inputs/radios.rb', line 77

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?
    (:label, text, options[:label].merge(for: button_group_id))
  elsif cards?
    (:label, text, options[:label].merge(for: button_group_id))
  elsif inline?
    (:label, text, options[:label])
  else
    (:legend, text, options[:label])
  end
end

#build_wrapper(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'app/models/effective/form_inputs/radios.rb', line 11

def build_wrapper(&block)
  tag = (buttons? || cards?) ? :div : :fieldset

  if layout == :horizontal
    (tag, (:div, yield, class: 'row'), options[:wrapper])
  else
    (tag, yield, options[:wrapper])
  end
end

#button_group_classObject



45
46
47
48
49
50
51
52
53
54
# File 'app/models/effective/form_inputs/radios.rb', line 45

def button_group_class
  [
    'effective-radios',
    ('no-feedback' if options[:feedback] == false),
    ('btn-group btn-group-toggle' if buttons?),
    ('card-deck' if cards?),
    ('is-invalid' if options[:feedback] && has_error?(name)),
    ('is-valid' if options[:feedback] && has_error? && !has_error?(name))
  ].compact.join(' ')
end

#button_group_idObject



153
154
155
# File 'app/models/effective/form_inputs/radios.rb', line 153

def button_group_id
  "#{tag_id}_btn_group"
end

#buttons?Boolean

default false

Returns:

  • (Boolean)


143
144
145
146
# File 'app/models/effective/form_inputs/radios.rb', line 143

def buttons? # default false
  return @buttons unless @buttons.nil?
  @buttons = (options.delete(:buttons) || false)
end

#cards?Boolean

default false

Returns:

  • (Boolean)


148
149
150
151
# File 'app/models/effective/form_inputs/radios.rb', line 148

def cards? # default false
  return @cards unless @cards.nil?
  @cards= (options.delete(:cards) || false)
end

#feedback_optionsObject



56
57
58
59
60
61
62
63
# File 'app/models/effective/form_inputs/radios.rb', line 56

def feedback_options
  return false if layout == :inline

  {
    valid: { class: 'valid-feedback' }.compact,
    invalid: { class: 'invalid-feedback' }.compact
  }
end

#first_item?Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'app/models/effective/form_inputs/radios.rb', line 157

def first_item?
  return false unless @first_item.nil?
  @first_item = true
end

#input_html_optionsObject



65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/effective/form_inputs/radios.rb', line 65

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

#item_input_optionsObject



127
128
129
# File 'app/models/effective/form_inputs/radios.rb', line 127

def item_input_options
  options[:input].except(:inline, :custom, :buttons)
end

#item_label_optionsObject



131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/effective/form_inputs/radios.rb', line 131

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_optionsObject



37
38
39
40
41
42
43
# File 'app/models/effective/form_inputs/radios.rb', line 37

def wrapper_options
  if buttons? || cards?
    { class: "form-group #{tag_id}" }
  else
    { class: "form-group effective-radios #{tag_id}" }
  end
end