Class: Conjoin::FormBuilder::SelectInput

Inherits:
Input
  • Object
show all
Defined in:
lib/conjoin/inputs/select.rb

Direct Known Subclasses

StateInput

Instance Attribute Summary

Attributes inherited from Input

#app, #data, #options, #record

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Input

#errors?, #id, #initialize, #nested_name, #render

Constructor Details

This class inherits a constructor from Conjoin::FormBuilder::Input

Class Method Details

.select_optionsObject



58
59
60
# File 'lib/conjoin/inputs/select.rb', line 58

def self.select_options
  @select_options
end

Instance Method Details

#displayObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/conjoin/inputs/select.rb', line 6

def display
  mab do
    # automatically add a prompt by default
    options[:prompt] = true unless options.key? :prompt
    options[:class] += ' select2'
    selected_value = options.delete :value

    select options do
      if prompt = options.delete(:prompt)
        opts = {
          value: ''
        }
        opts['selected'] = 'selected' unless selected_value
        option opts do
          text prompt.to_s == 'true' ? 'Please Choose One.' : prompt
        end
      end

      if not options[:group]
        select_options.each do |name, value|
          option render_opts(value, selected_value, opts) do
            text name.titleize
          end
        end
      else
        select_options.each do |group_select, group|
          optgroup label: group.to_s.titleize do
            group_select.each do |value, name|
              option render_opts(value, selected_value, opts) do
                text name.titleize
              end
            end
          end
        end
      end
    end
  end
end

#render_opts(value, selected_value, opts) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/conjoin/inputs/select.rb', line 45

def render_opts value, selected_value, opts
  opts = {
    value: value
  }
  if selected_value.is_a? ActiveRecord::Associations::CollectionProxy
    opts['selected'] = 'selected' if selected_value.map(&:id).include? value
  else
    opts['selected'] = 'selected' if selected_value == value.to_s
  end

  opts
end

#select_optionsObject



62
63
64
# File 'lib/conjoin/inputs/select.rb', line 62

def select_options
  self.class.select_options.invert
end