From 3f93fc1c77f4d97dabd438bd7bab874da8651dc0 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Wed, 24 Feb 2010 09:06:40 -0200 Subject: [PATCH 1/5] Allow showing the result of a method instance instead of an attribute when showing the results --- lib/auto_complete_macros_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/auto_complete_macros_helper.rb b/lib/auto_complete_macros_helper.rb index 1d25ee4..d150e33 100644 --- a/lib/auto_complete_macros_helper.rb +++ b/lib/auto_complete_macros_helper.rb @@ -93,9 +93,9 @@ def auto_complete_field(field_id, options = {}) # # The auto_complete_result can of course also be called from a view belonging to the # auto_complete action if you need to decorate it further. - def auto_complete_result(entries, field, phrase = nil) + def auto_complete_result(entries, method, phrase = nil) return unless entries - items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) } + items = entries.map { |entry| content_tag("li", phrase ? highlight(entry.send(method.to_sym), phrase) : h(entry.send(method.to_sym))) } content_tag("ul", items.uniq) end From f70d593c39e6c2e7c9808d6931a6a41e192a5c0b Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Wed, 24 Feb 2010 09:52:03 -0200 Subject: [PATCH 2/5] Can set id for a autocomplete field --- lib/auto_complete_macros_helper.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/auto_complete_macros_helper.rb b/lib/auto_complete_macros_helper.rb index d150e33..7126506 100644 --- a/lib/auto_complete_macros_helper.rb +++ b/lib/auto_complete_macros_helper.rb @@ -105,10 +105,11 @@ def auto_complete_result(entries, method, phrase = nil) # auto_complete_for to respond the AJAX calls, # def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {}) + tag_id = tag_options[:id] ? tag_options[:id] : "#{object}_#{method}" (completion_options[:skip_style] ? "" : auto_complete_stylesheet) + text_field(object, method, tag_options) + - content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") + - auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options)) + content_tag("div", "", :id => "#{tag_id}_auto_complete", :class => "auto_complete") + + auto_complete_field("#{tag_id}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options)) end private From 23ec6350f544e59b57cb7b239d7d8dbb20261483 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Sun, 19 Sep 2010 22:28:07 -0300 Subject: [PATCH 3/5] Making the options html_safe. And then making it compatible with Rails 3 --- lib/auto_complete_macros_helper.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/auto_complete_macros_helper.rb b/lib/auto_complete_macros_helper.rb index 7126506..4f42b8e 100644 --- a/lib/auto_complete_macros_helper.rb +++ b/lib/auto_complete_macros_helper.rb @@ -95,8 +95,8 @@ def auto_complete_field(field_id, options = {}) # auto_complete action if you need to decorate it further. def auto_complete_result(entries, method, phrase = nil) return unless entries - items = entries.map { |entry| content_tag("li", phrase ? highlight(entry.send(method.to_sym), phrase) : h(entry.send(method.to_sym))) } - content_tag("ul", items.uniq) + items = entries.map { |entry| raw(content_tag("li", phrase ? highlight(entry.send(method.to_sym), phrase) : h(entry.send(method.to_sym)))) } + content_tag("ul", items.uniq.join.html_safe).html_safe end # Wrapper for text_field with added AJAX autocompletion functionality. From 9d2219b52c9e29f3225839c093a4289f10865d43 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Mon, 27 Sep 2010 17:52:14 -0300 Subject: [PATCH 4/5] Making text_field_with_auto_complete helper compatible with Rails 3 when used in conjunction with skip_style option --- lib/auto_complete_macros_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/auto_complete_macros_helper.rb b/lib/auto_complete_macros_helper.rb index 4f42b8e..2ffc8b0 100644 --- a/lib/auto_complete_macros_helper.rb +++ b/lib/auto_complete_macros_helper.rb @@ -106,7 +106,7 @@ def auto_complete_result(entries, method, phrase = nil) # def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {}) tag_id = tag_options[:id] ? tag_options[:id] : "#{object}_#{method}" - (completion_options[:skip_style] ? "" : auto_complete_stylesheet) + + (completion_options[:skip_style] ? "".html_safe : auto_complete_stylesheet) + text_field(object, method, tag_options) + content_tag("div", "", :id => "#{tag_id}_auto_complete", :class => "auto_complete") + auto_complete_field("#{tag_id}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options)) From 2a04deeb0decbbcca3d9a341d6819ea35e97b823 Mon Sep 17 00:00:00 2001 From: Marcelo Giorgi Date: Tue, 26 Apr 2011 18:53:48 -0300 Subject: [PATCH 5/5] README updated for Rails 3 --- README | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/README b/README index e08a815..01bf45d 100644 --- a/README +++ b/README @@ -1,4 +1,9 @@ -Example: +Example (updated for Rails 3): + + # config/routes.rb + .. + post "/blog/auto_complete_for_post_title" => "blog#auto_complete_for_post_title" + .. # Controller class BlogController < ApplicationController @@ -6,7 +11,11 @@ Example: end # View - <%= text_field_with_auto_complete :post, title %> + <%= text_field_with_auto_complete :post, :title %> + + # stylesheets + (this is not necesary or even the best fit, but a quick working solution if the boxes doesn't fit nicely below the auto complete textbox) + * { margin:0; padding:0; } By default, auto_complete_for limits the results to 10 entries, and sorts by the given field.