Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with Rails 3 #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions README
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
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
auto_complete_for :post, :title
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.
Expand Down
13 changes: 7 additions & 6 deletions lib/auto_complete_macros_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ 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])) }
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.
Expand All @@ -105,10 +105,11 @@ def auto_complete_result(entries, field, phrase = nil)
# auto_complete_for to respond the AJAX calls,
#
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
tag_id = tag_options[:id] ? tag_options[:id] : "#{object}_#{method}"
(completion_options[:skip_style] ? "".html_safe : 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
Expand Down