Skip to content

Commit e8286d8

Browse files
committed
modified to work with mongoid
1 parent b689320 commit e8286d8

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*.tmproj
66
tmtags
77

8+
## REDCAR
9+
.redcar
10+
811
## EMACS
912
*~
1013
\#*

README.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
I modified this plugin to work with mongoid.
2+
13
# rails3-jquery-autocomplete
24

35
An easy way to use jQuery's autocomplete with Rails 3. You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)

lib/rails3-jquery-autocomplete.rb

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'form_helper'
2-
31
module Rails3JQueryAutocomplete
42
def self.included(base)
53
base.extend(ClassMethods)
@@ -27,27 +25,25 @@ def self.included(base)
2725
#
2826
module ClassMethods
2927
def autocomplete(object, method, options = {})
30-
limit = options[:limit] || 10
31-
order = options[:order] || "#{method} ASC"
3228

3329
define_method("autocomplete_#{object}_#{method}") do
3430
unless params[:term] && params[:term].empty?
35-
items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]).limit(limit).order(order)
31+
items = Category.where("this.name.match(/#{params[:term]}/i)").limit(10).asc(:name)
3632
else
3733
items = {}
3834
end
3935

40-
render :json => json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method))
36+
render :json => json_for_autocomplete(items, method)
4137
end
4238
end
4339
end
4440

4541
private
4642
def json_for_autocomplete(items, method)
47-
items.collect {|i| {"id" => i.id, "label" => i.send(method), "value" => i.send(method)}}
43+
items.collect {|i| {"id" => "#{i.id}", "label" => "#{i.name}", "value" => "#{i.name}"}}
4844
end
4945
end
5046

5147
class ActionController::Base
5248
include Rails3JQueryAutocomplete
53-
end
49+
end

0 commit comments

Comments
 (0)