Class: TomSelectInput

Inherits:
SimpleForm::Inputs::CollectionSelectInput
  • Object
show all
Defined in:
app/inputs/tom_select_input.rb

Overview

SimpleForm input wrapper: tom select.

Instance Method Summary collapse

Instance Method Details

#collectionObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/inputs/tom_select_input.rb', line 65

def collection
  base_collection = options[:collection]

  # If no collection provided, fall back to selected values or empty array
  unless base_collection
    val = selected_value
    return val.present? ? Array(val) : []
  end

  # For multiple selects with array values, reorder collection to put selected items first
  # This preserves the database order when Tom Select renders the selected items
  model_values = selected_value
  if input_html_options[:multiple] && model_values.is_a?(Array) && model_values.any?
    reorder_collection_by_selected(base_collection, model_values)
  else
    base_collection
  end
end

#input(wrapper_options = nil) ⇒ Object



5
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/inputs/tom_select_input.rb', line 5

def input(wrapper_options = nil)
  input_html_options[:type] = :select
  input_html_options[:multiple] ||= options[:multiple] || multiple_by_association?
  input_html_options[:data] ||= {}

  placeholder = options[:placeholder] || input_html_options[:placeholder] || (options[:include_blank].is_a?(String) ? options[:include_blank] : nil) || 'Please Select'
  input_html_options[:data]['tom-select-placeholder-value'] = placeholder

  if input_html_options[:multiple]
    options[:include_blank] = input_html_options[:include_blank] = false
  elsif options[:include_blank].nil?
    options[:include_blank] = true
  end

  # Generic controller hookup; specialized behaviors react via events
  current_controller = input_html_options[:data][:controller] || input_html_options[:data]['controller']
  merged = [current_controller, 'tom-select'].compact.join(' ')
  input_html_options[:data][:controller] = merged
  input_html_options[:data]['controller'] = merged

  # Remote suggest URL if provided
  if (url = options[:suggest_url] || input_html_options[:data].delete(:suggest_url))
    input_html_options[:data]['tom-select-suggest-url-value'] = url
  end
  if (min_chars = options[:min_chars] || input_html_options[:data].delete(:min_chars))
    input_html_options[:data]['tom-select-min-chars-value'] = min_chars
  end
  input_html_options[:data]['tom-select-preload-value'] = true if options[:preload] || input_html_options[:data].delete(:preload)
  input_html_options[:data]['tom-select-preserve-order-value'] = true if options[:preserve_order] || input_html_options[:data].delete(:preserve_order)
  input_html_options[:data]['tom-select-create-value'] = true if options[:create] || input_html_options[:data].delete(:create)
  if (dropdown_parent = options[:dropdown_parent] || input_html_options[:data].delete(:dropdown_parent))
    input_html_options[:data]['tom-select-dropdown-parent-value'] = dropdown_parent
  end

  # Bootstrap form-select styling
  input_html_options[:class] = Array(input_html_options[:class]) << 'form-select'

  # Enable clear button when a blank/placeholder is present (lets users reset to Company)
  input_html_options[:data]['tom-select-plugins-value'] = ['clear_button'] if options[:include_blank] || input_html_options[:placeholder].present? || options[:placeholder].present?

  # Always prefer params for selected values so selections persist on reload
  selected_from_params = begin
    object.respond_to?(:params) ? object.params[attribute_name.to_s] : nil
  rescue StandardError
    nil
  end
  if input_html_options[:multiple]
    selected = Array(selected_from_params).compact
    options[:selected] = selected if selected.any?
  elsif selected_from_params.present?
    options[:selected] = selected_from_params
  end

  super
end

#input_html_classesObject



61
62
63
# File 'app/inputs/tom_select_input.rb', line 61

def input_html_classes
  (super || []).map(&:to_s).push('tom-select').uniq
end