Class: SimpleForm::Inputs::AutocompleteCollectionInput

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

Instance Method Summary collapse

Methods included from Autocomplete

#rewrite_autocomplete_option

Instance Method Details

#input(opts) ⇒ Object



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails-jquery-autocomplete/simple_form_plugin.rb', line 50

def input(opts)
  # 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

  # This branching is to deal with a change beginning in simple_form 3.0.2 and above to ensure backwards compatibility
  if opts.nil?
    autocomplete_options = rewrite_autocomplete_option
  else
    merge_wrapper_options(rewrite_autocomplete_option, args)
  end

  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