Class: Effective::FormInputs::Select

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

Direct Known Subclasses

TimeZoneSelect

Constant Summary collapse

INCLUDE_NULL =
'Blank (null)'

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_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, #translate, #value_method

Methods inherited from Effective::FormInput

#feedback_options, #hint_options, #initialize, #input_group_options, #label_options, #to_html, #wrapper_options

Constructor Details

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

Instance Method Details

#ajax?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/models/effective/form_inputs/select.rb', line 86

def ajax?
  ajax_url.present?
end

#assign_options_collection!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/effective/form_inputs/select.rb', line 64

def assign_options_collection!
  super

  return unless include_null

  # Check for singles - transform the array
  if options_collection.kind_of?(Array) && !options_collection.first.respond_to?(:to_a) # [:admin, :member]
    @options_collection = options_collection.map { |obj| [obj, obj] }
  end

  if options_collection.kind_of?(Array) && options_collection.first.respond_to?(:to_a)
    options_collection.push(['---------------', '-', disabled: 'disabled'])
    options_collection.push([include_null, 'nil'])
  end

  if options_collection.kind_of?(Hash)
    options_collection[include_null] = [[include_null, 'nil']]
  end

  options_collection
end

#build_input(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/effective/form_inputs/select.rb', line 10

def build_input(&block)
  html = if polymorphic?
    @builder.grouped_collection_select(polymorphic_id_method, options_collection, group_method, group_label_method, option_key_method, option_value_method, collection_options, html_options)
  elsif grouped?
    @builder.grouped_collection_select(name, options_collection, group_method, group_label_method, option_key_method, option_value_method, collection_options, html_options)
  else
    @builder.collection_select(name, options_collection, value_method, label_method, collection_options, html_options)
  end

  if polymorphic?
    html += @builder.hidden_field(polymorphic_type_method, value: polymorphic_type_value)
    html += @builder.hidden_field(polymorphic_id_method, value: polymorphic_id_value)
  end

  if single_selected?
    html.gsub!('selected="selected"', '') if html.sub!('selected="selected"', "selected='selected'")
  end

  html.html_safe
end

#input_html_optionsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/effective/form_inputs/select.rb', line 50

def input_html_options
  classes = [
    'effective_select',
    'form-control',
    ('polymorphic' if polymorphic?),
    ('grouped' if (grouped? || polymorphic?)),
    ('hide-disabled' if hide_disabled?),
    ('tags-input' if tags?),
    ('disable-open-on-focus' if disable_open_on_focus?),
  ].compact.join(' ')

  { class: classes, multiple: (true if multiple?), include_blank: (true if include_blank?), include_null: include_null }.compact
end

#input_js_optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/effective/form_inputs/select.rb', line 31

def input_js_options
  placeholder = options.delete(:placeholder)
  placeholder = '' if placeholder == false

  opts = {
    theme: 'bootstrap',
    minimumResultsForSearch: 6,
    width: 'style',
    placeholder: (placeholder || 'Please choose'),
    allowClear: (true if include_blank?),
    tokenSeparators: ([',', ';', '\n', '\t'] if tags?),
    tags: (true if tags?),
    template: js_template,
    containerClass: ('hide-disabled' if hide_disabled?),
    dropdownClass: ('hide-disabled' if hide_disabled?),
    ajax: ({ url: ajax_url, dataType: 'json', delay: 250 } if ajax?)
  }.compact
end