From 608b1944e6e8634b43495750c93fc7a3f6906592 Mon Sep 17 00:00:00 2001 From: Andreas Boehrnsen Date: Fri, 7 Oct 2011 16:57:27 +0800 Subject: [PATCH 1/2] DEV: added "distinct" functionality --- .../autocomplete.rb | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/rails3-jquery-autocomplete/autocomplete.rb b/lib/rails3-jquery-autocomplete/autocomplete.rb index fefc9146..ed52013f 100644 --- a/lib/rails3-jquery-autocomplete/autocomplete.rb +++ b/lib/rails3-jquery-autocomplete/autocomplete.rb @@ -57,7 +57,7 @@ def autocomplete(object, method, options = {}) items = {} end - render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data]) + render :json => json_for_autocomplete(items, options[:display_value] ||= method, options[:extra_data], options[:distinct]) end end end @@ -73,7 +73,7 @@ def get_autocomplete_limit(options) # # returns a Actor constant supposing it is already defined # def get_object(model_sym) - object = model_sym.to_s.camelize.constantize + model_sym.to_s.camelize.constantize end # @@ -81,16 +81,25 @@ def get_object(model_sym) # Can be overriden to show whatever you like # Hash also includes a key/value pair for each method in extra_data # - def json_for_autocomplete(items, method, extra_data=[]) - items.collect do |item| - hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)} + def json_for_autocomplete(items, method, extra_data=[], distinct = false) + arr = [] + items.each do |item| + hash = {} + hash['id'] = item.id.to_s unless distinct + hash['value'] = item.send(method) + hash['label'] = hash['value'] + extra_data.each do |datum| hash[datum] = item.send(datum) end if extra_data # TODO: Come back to remove this if clause when test suite is better - hash + + next if distinct and arr.include?(hash) + arr << hash end + arr end + end end From 4a4097fc9af2f49dabea5e62a9966599e9a38ee2 Mon Sep 17 00:00:00 2001 From: Hubert Hesse Date: Fri, 13 Jan 2012 15:00:11 +0100 Subject: [PATCH 2/2] This changes the input field value on navigation through the result list via key down/up. This is what most other applications google, firefox, etc do. --- integration/public/javascripts/autocomplete-rails.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/integration/public/javascripts/autocomplete-rails.js b/integration/public/javascripts/autocomplete-rails.js index 5b23fbf5..07198806 100644 --- a/integration/public/javascripts/autocomplete-rails.js +++ b/integration/public/javascripts/autocomplete-rails.js @@ -65,10 +65,6 @@ return false; } }, - focus: function() { - // prevent value inserted on focus - return false; - }, select: function( event, ui ) { var terms = split( this.value ); // remove the current input