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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 72

def collection
  return if search_url

  if (collection = options[:collection])
    if enumerable?(collection)
      collection.map(&method(:serialize_option))
    else
      (object.send(collection) || []).map(&method(:serialize_option))
    end
  elsif relation?
    reflection.klass.all.map(&method(:serialize_option))
  elsif enum?
    enum_options
  else
    []
  end
end

#creatable?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 45

def creatable?
  !!options[:creatable]
end

#escapeObject



49
50
51
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 49

def escape
  options[:escape]
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
31
32
33
34
35
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 15

def input(wrapper_options = {})
  @attribute_name = 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,
    :'sort-field'      => sort_field,
    :'search-url'      => search_url,
    :'search-param'    => search_param,
    :'escape'          => escape
  )

  @builder.hidden_field attribute_name, input_html_options
end

#max_itemsObject



58
59
60
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 58

def max_items
  options[:max_items]
end

#multi?Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 53

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

#search_paramObject



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

def search_param
  options[:search_param] ||= 'q'
end

#search_urlObject



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

def search_url
  options[:search_url]
end

#serialize_value(value, text = nil) ⇒ Object



118
119
120
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 118

def serialize_value(value, text = nil)
  { text: (text || value), value: value }
end

#serialized_valueObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 90

def serialized_value
  return input_html_options[:data][:value] if input_html_options[:data][:value]

  if multi?
    if relation?
      value.map do |item|
        if (resource = relation.find { |resource| resource.id == item.to_i }) && (text = text_from(resource))
          serialize_value(item, text)
        else
          serialize_value(item)
        end
      end
    else
      value.map(&method(:serialize_value))
    end
  else
    if relation? && relation && (text = text_from(relation))
      serialize_value(value, text)
    else
      serialize_value(value)
    end
  end
end

#sort_fieldObject



62
63
64
65
66
67
68
69
70
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 62

def sort_field
  if (sort_field = options[:sort_field]).present?
    sort_field
  elsif enum?
    'position'
  else
    'text'
  end
end

#valueObject



114
115
116
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 114

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