Class: Effective::FormBuilderInputs::EffectiveRadioButtons

Inherits:
Effective::FormBuilderInput show all
Defined in:
app/models/effective/form_builder_inputs/effective_radio_buttons.rb

Constant Summary collapse

BOOLEAN_COLLECTION =
[['Yes', true], ['No', false]]

Instance Method Summary collapse

Methods inherited from Effective::FormBuilderInput

#field_name, #initialize, #js_options

Constructor Details

This class inherits a constructor from Effective::FormBuilderInput

Instance Method Details

#collectionObject



75
76
77
78
79
80
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 75

def collection
  @collection ||= begin
    collection = options.delete(:collection) || BOOLEAN_COLLECTION
    collection.respond_to?(:call) ? collection.call : collection.to_a
  end
end

#default_input_htmlObject



12
13
14
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 12

def default_input_html
  { class: 'effective_radio_buttons' }
end

#default_input_jsObject



16
17
18
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 16

def default_input_js
  {}
end

#default_optionsObject



8
9
10
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 8

def default_options
  { label_method: :first, value_method: :last }
end

#html_optionsObject



69
70
71
72
73
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 69

def html_options
  super.tap do |html_options|
    html_options[:class].delete('form-control')
  end
end

#item_html_optionsObject



65
66
67
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 65

def item_html_options
  @item_html_options ||= tag_options
end

#item_image_or_text(builder) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 55

def item_image_or_text(builder)
  @images_index += 1

  if options[:images]
    image_tag(options[:images][@images_index], alt: builder.text)
  else
    builder.text
  end
end

#optionsObject



86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 86

def options
  @effective_radio_buttons_options ||= super().tap do |options|
    options[:item_wrapper_class] = Array(options[:item_wrapper_class]).flatten.uniq

    if options[:inline] || options[:buttons]
      options[:item_wrapper_tag] = :label unless options[:buttons]
      options[:item_wrapper_class] = 'radio-inline'
    end

  end
end

#render_item(builder) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 25

def render_item(builder)
  if options[:inline] || options[:buttons]
    item = builder.radio_button(item_html_options) + item_image_or_text(builder)
  elsif options[:nested_boolean_style] == :nested
    item = builder.label { builder.radio_button(item_html_options) + item_image_or_text(builder) }
  else
    item = builder.radio_button(item_html_options) + builder.label { item_image_or_text(builder) }
  end

  if options[:buttons]
    @button_index ||= -1; @button_index += 1
  end

  if options[:item_wrapper_tag]
    active = (builder.object.send(options[:value_method]).to_s == value.to_s)
    #active ||= (builder.object.send(options[:value_method]).to_s == Array(value).first.to_s)

    (options[:item_wrapper_tag], item,
      class: [
        options[:item_wrapper_class],
        ('btn btn-default' if options[:buttons]),
        ('radio-first' if options[:buttons] && @button_index == 0),
        ('active' if active)
      ].flatten.compact.uniq.join(' ')
    )
  else
    item
  end
end

#to_htmlObject



20
21
22
23
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 20

def to_html
  initialize_and_validate_images!
  collection_radio_buttons(@object_name, @method, collection, options[:value_method], options[:label_method], options.slice(:checked), item_html_options, &proc { |builder| render_item(builder) })
end

#valueObject



82
83
84
# File 'app/models/effective/form_builder_inputs/effective_radio_buttons.rb', line 82

def value
  Array(options[:checked] || super).first
end