Class: SimpleFormExtension::Inputs::SelectizeInput
- Inherits:
-
SimpleForm::Inputs::Base
- Object
- SimpleForm::Inputs::Base
- SimpleFormExtension::Inputs::SelectizeInput
- Includes:
- ResourceNameHelper, Translations
- Defined in:
- lib/simple_form_extension/inputs/selectize_input.rb
Instance Method Summary collapse
- #collection ⇒ Object
- #creatable? ⇒ Boolean
- #escape ⇒ Object
-
#input(_wrapper_options = {}) ⇒ Object
This field only allows local select options (serialized into JSON) Searching for remote ones will be implemented later.
- #max_items ⇒ Object
- #multi? ⇒ Boolean
- #search_param ⇒ Object
- #search_url ⇒ Object
- #serialize_value(value, text = nil) ⇒ Object
- #serialized_value ⇒ Object
- #sort_field ⇒ Object
- #value ⇒ Object
Methods included from ResourceNameHelper
Methods included from Translations
Instance Method Details
#collection ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 73 def collection return if search_url if (collection = [: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? else [] end end |
#creatable? ⇒ Boolean
46 47 48 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 46 def creatable? !![:creatable] end |
#escape ⇒ Object
50 51 52 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 50 def escape [: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,
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 16 def input( = {}) @attribute_name = foreign_key if relation? [:data] ||= {} [:data].merge!( controller: 'selectize-field', 'selectize-field-input-value': Array.wrap(serialized_value), 'selectize-field-creatable-value': creatable?, 'selectize-field-multi-value': multi?, 'selectize-field-add-translation-value': _translate('selectize.add'), 'selectize-field-collection-value': collection, 'selectize-field-max-items-value': max_items, 'selectize-field-sort-field-value': sort_field, 'selectize-field-search-url-value': search_url, 'selectize-field-search-param-value': search_param, 'selectize-field-escape-value': escape.nil? ? true : escape ) @builder.text_field attribute_name, end |
#max_items ⇒ Object
59 60 61 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 59 def max_items [:max_items] end |
#multi? ⇒ Boolean
54 55 56 57 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 54 def multi? (.key?(:multi) && !![:multi]) || enumerable?(value) end |
#search_param ⇒ Object
38 39 40 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 38 def search_param [:search_param] ||= 'q' end |
#search_url ⇒ Object
42 43 44 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 42 def search_url [:search_url] end |
#serialize_value(value, text = nil) ⇒ Object
117 118 119 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 117 def serialize_value(value, text = nil) { text: (text || value), value: value } end |
#serialized_value ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 91 def serialized_value return [:data][:value] if [:data][:value] if multi? if relation? value.map do |item| if (resource = relation.find { |r| r.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 elsif relation? && relation && (text = text_from(relation)) serialize_value(value, text) else serialize_value(value) end end |
#sort_field ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 63 def sort_field if (sort_field = [:sort_field]).present? sort_field elsif enum? 'position' else 'text' end end |
#value ⇒ Object
113 114 115 |
# File 'lib/simple_form_extension/inputs/selectize_input.rb', line 113 def value @value ||= (:value) { object.send(attribute_name) } end |