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

#displayObject



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/conjoin/inputs/select.rb', line 15

def display
  # We don't need the type
  options.delete :type

  append_button       = options.delete :append_button
  append_split_button = options.delete :append_split_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.downcase ? 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 append_button
    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
  elsif append_split_button
    first_button = append_split_button.shift

    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' => first_button[:href] do
            text first_button[:text]
          end
          if append_split_button.length
            button class: 'btn btn-primary dropdown-toggle', 'data-toggle' => "dropdown", type: 'button' do
              span class: 'caret'
            end
            ul class: 'dropdown-menu pull-right', role: 'menu' do
              append_split_button.each do |b|
                li do
                  a href: 'javascript:{};', 'on-click-get' => b[:href] do
                    text b[:text]
                  end
                end
              end
            end
          end
        end
      end
    end
  else
    content
  end
end

#render_opts(value, selected_value, opts) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/conjoin/inputs/select.rb', line 107

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_optionsObject



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