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::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

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)


95
96
97
# File 'app/models/effective/form_inputs/select.rb', line 95

def ajax?
  ajax_url.present?
end

#assign_options_collection!Object



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
# File 'app/models/effective/form_inputs/select.rb', line 65

def assign_options_collection!
  super

  if ajax? && !include_null
    if options_collection.kind_of?(Array) && options_collection.first.respond_to?(:to_select2)
      @options_collection = options_collection.map { |obj| [obj.to_select2, obj.to_param] }
    end
  end

  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] = [
      Struct.new(:to_s, :id, :first, :second).new(include_null, 'nil', include_null, 'nil')
    ]
  end

  options_collection
end

#build_input(&block) ⇒ Object



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

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
63
# File 'app/models/effective/form_inputs/select.rb', line 50

def input_html_options
  classes = [
    'effective_select',
    'form-control',
    ('freeform' if freeform?),
    ('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?), id: tag_id }.compact
end

#input_js_optionsObject



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 33

def input_js_options
  opts = {
    theme: 'bootstrap',
    minimumResultsForSearch: (freeform? ? 0 : 6),
    width: 'style',
    placeholder: placeholder,
    allowClear: (true if include_blank?),
    tokenSeparators: ([',', ';', '\n', '\t'] if tags?),
    tags: (true if tags?),
    noResults: no_results,
    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