Class: Conjoin::FormBuilder::SelectInput

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

Direct Known Subclasses

StateInput, YearInput

Instance Attribute Summary

Attributes inherited from Input

#app, #data, #options, #record

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

Instance Method Details

#display ⇒ Object



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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/conjoin/inputs/select.rb', line 15

def display
  append_button = options.delete :append_button
  if options[:multiple]
    options[:name] += '[]'
  end

  content = mab do
    # automatically add a prompt by default
    options[:prompt] = 'true' unless options.key? :prompt
    options[:class] += ' selectize' unless options.delete(:selectize) == false
    selected_value = options.delete :value

    select options do
      opts = {
        value: ''
      }

      if prompt = options.delete(:prompt)
        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.invert.each do |name, value|
          option render_opts(value, selected_value, opts) do
            text (name != name.upcase ? name.titleize : name)
          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

  if not append_button
    content
  else
    mab do
      div class: 'input-group' do
        text! content
        div class: 'input-group-btn' do
          button class: 'btn btn-primary', type: 'button', 'on-click-get' => append_button[:href] do
            text append_button[:text]
          end
        end
      end
    end
  end
end

#render_opts(value, selected_value, opts) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/conjoin/inputs/select.rb', line 75

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
  elsif selected_value.is_a? Array
    opts['selected'] = 'selected' if selected_value.include? value.to_s
  else
    opts['selected'] = 'selected' if selected_value.to_s == value.to_s
  end

  opts
end

#select_options ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/conjoin/inputs/select.rb', line 4

def select_options
  values_select = {}
  values        = data.record_class.send(data.field_name).values

  values.each do |value|
    values_select[value] = value
  end

  values_select
end