Class: SimpleFormExtension::Inputs::SelectizeInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Includes:
Translations
Defined in:
lib/simple_form_extension/inputs/selectize_input.rb

Instance Method Summary collapse

Methods included from Translations

#_translate

Instance Method Details

#collectionObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 45

def collection
  if (collection = options[:collection])
    if enumerable?(collection)
      collection.map(&method(:serialize_option))
    else
      (object.send(collection) || []).map(&method(:serialize_option))
    end
  else
    []
  end
end

#creatable?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 32

def creatable?
  !!options[:creatable]
end

#input(wrapper_options = {}) ⇒ Object

This field only allows local select options (serialized into JSON) Searching for remote ones will be implemented later.

Data attributes that may be useful :

:'search-url' => search_url,
:'search-param' => search_param,
:'preload' => preload,


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 15

def input(wrapper_options = {})
  @attribute_name = reflection.foreign_key if relation?
  input_html_options[:data] ||= {}

  input_html_options[:data].merge!(
    :'selectize' => true,
    :'value' => serialized_value,
    :'creatable' => creatable?,
    :'multi' => multi?,
    :'add-translation' => _translate('selectize.add'),
    :'collection' => collection,
    :'max-items' => max_items
  )

  @builder.hidden_field attribute_name, input_html_options
end

#max_itemsObject



41
42
43
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 41

def max_items
  options[:max_items]
end

#multi?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 36

def multi?
  (options.key?(:multi) && !!options[:multi]) ||
    enumerable?(value)
end

#serialized_valueObject



57
58
59
60
61
62
63
64
65
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 57

def serialized_value
  if multi?
    value.map do |item|
      { text: item, value: item }
    end
  else
    value && { text: value, value: value }
  end
end

#valueObject



67
68
69
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 67

def value
  @value ||= options_fetch(:value) { object.send(attribute_name) }
end