class SimpleFormExtension::Inputs::SelectizeInput

Public Instance Methods

collection() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 73
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?() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 46
def creatable?
  !!options[:creatable]
end
escape() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 50
def escape
  options[:escape]
end
input(wrapper_options = {}) click to toggle source

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,
# File lib/simple_form_extension/inputs/selectize_input.rb, line 16
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_items() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 59
def max_items
  options[:max_items]
end
multi?() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 54
def multi?
  (options.key?(:multi) && !!options[:multi]) ||
    enumerable?(value)
end
search_param() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 38
def search_param
  options[:search_param] ||= 'q'
end
search_url() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 42
def search_url
  options[:search_url]
end
serialize_value(value, text = nil) click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 119
def serialize_value(value, text = nil)
  { text: (text || value), value: value }
end
serialized_value() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 91
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_field() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 63
def sort_field
  if (sort_field = options[:sort_field]).present?
    sort_field
  elsif enum?
    'position'
  else
    'text'
  end
end
value() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 115
def value
  @value ||= options_fetch(:value) { object.send(attribute_name) }
end

Private Instance Methods

enum?() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 172
def enum?
  object_class.respond_to?(:defined_enums) && 
    object_class.defined_enums.key?(attribute_name.to_s)
end
enum_options() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 177
def enum_options
  object_class.defined_enums[attribute_name.to_s].map do |key, value|
    path = [object.model_name.i18n_key, attribute_name, key].join('.')
    translation = ::I18n.t("activerecord.enums.#{ path }", default: '')

    { text: translation.presence || key, value: key, position: value }
  end
end
enumerable?(target) click to toggle source

Check if the given target object is enumerable. Used internally to check wether :collection or :value arguments are collections.

# File lib/simple_form_extension/inputs/selectize_input.rb, line 150
def enumerable?(target)
  target.class.include?(Enumerable) || ActiveRecord::Relation === target
end
foreign_key() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 186
def foreign_key
  @foreign_key ||= case reflection.macro
  when :belongs_to then reflection.foreign_key
  when :has_one then :"#{ reflection.name }_id"
  when :has_many then :"#{ reflection.name.to_s.singularize }_ids"
  end
end
name_for(option) click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 154
def name_for(option)
  resource_name_for(option) || option.to_s
end
object_class() click to toggle source

Retrieve actual model class even when the form object is a proxy like for Ransack::Search or ActiveRecord::Relation classes

# File lib/simple_form_extension/inputs/selectize_input.rb, line 201
def object_class
  @object_class ||= if ((object_class = object.class) < ActiveRecord::Base)
    object_class
  elsif (klass = object.try(:klass))
    klass
  else
    object_class
  end
end
options_fetch(key, &block) click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 138
def options_fetch(key, &block)
  [options, input_html_options].each do |hash|
    return hash[key] if hash.key?(key)
  end

  # Return default block value or nil if no block was given
  block ? block.call : nil
end
reflection() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 166
def reflection
  @reflection ||= if object_class.respond_to?(:reflect_on_association)
    object_class.reflect_on_association(attribute_name)
  end
end
relation() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 158
def relation
  @relation ||= object.send(reflection.name) if relation?
end
relation?() click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 162
def relation?
  !!reflection
end
serialize_option(option) click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 125
def serialize_option(option)
  if option.kind_of?(Hash) && option.key?(:text) && option.key?(:value)
    option
  elsif option.kind_of?(ActiveRecord::Base)
    { text: name_for(option), value: option.id }
  elsif !option.kind_of?(Hash)
    { text: option.to_s, value: option }
  else
    raise ArgumentError.new "The individual collection items should " \
      "either be single items or a hash with :text and :value fields"
  end
end
text_from(resource) click to toggle source
# File lib/simple_form_extension/inputs/selectize_input.rb, line 194
def text_from(resource)
  resource_name_for(resource)
end