Class: SimpleForm::Inputs::AutocompleteCollectionInput

Inherits:
CollectionInput
  • Object
show all
Includes:
Autocomplete
Defined in:
lib/rails4-autocomplete/simple_form_plugin.rb

Instance Method Summary collapse

Methods included from Autocomplete

#rewrite_autocomplete_option

Instance Method Details

#inputObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rails4-autocomplete/simple_form_plugin.rb', line 40

def input
  # http://www.codeofficer.com/blog/entry/form_builders_in_rails_discovering_field_names_and_ids_for_javascript/
  hidden_id = "#{object_name}_#{attribute_name}_hidden".gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
  id_element = options[:id_element]
  if id_element
    id_element << ", #" << hidden_id
  else
    id_element = "#" + hidden_id
  end
  options[:id_element] = id_element
  autocomplete_options = rewrite_autocomplete_option
  #
  label_method, value_method = detect_collection_methods
  association = object.send(reflection.name)
  if association && association.respond_to?(label_method)
    autocomplete_options[:value] = association.send(label_method)
  end
  out = @builder.autocomplete_field(
    attribute_name,
    options[:url],
    autocomplete_options
  )
  hidden_options = if association && association.respond_to?(value_method)
    new_options = {}
    new_options[:value] = association.send(value_method)
    input_html_options.merge new_options
  else
    input_html_options
  end
  hidden_options[:id] = hidden_id
  out << @builder.hidden_field(
    attribute_name,
    hidden_options
  )
  out.html_safe
end