Module: BootstrapAutocompleteInput::Controllers::Autocomplete::ClassMethods

Defined in:
lib/bootstrap_autocomplete_input/controllers/autocomplete.rb

Instance Method Summary collapse

Instance Method Details

#autocomplete(object, method, options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bootstrap_autocomplete_input/controllers/autocomplete.rb', line 22

def autocomplete(object, method, options = {})
  define_method("autocomplete_#{object}_#{method}") do
    # model
    class_name = options[:class_name] || object
    model = get_object(class_name)

    term = '' # return ALL
    q = params[:q]
    q = '' if q.nil?

    #
    items = get_autocomplete_items(:model => model, :method=>method, :options => options, :q => q)

    #
    method_display_value = options[:display_value] if options.has_key?(:display_value)
    method_display_value ||= method

    method_display_id = options[:display_id] if options.has_key?(:display_id)
    method_display_id ||= model.primary_key

    data = items_to_json(items, method_display_id, method_display_value)
    render :json => data.to_json
  end
end