Skip to content

Commit 9617a3b

Browse files
author
David Padilla
committed
The full parameter
1 parent d167cce commit 9617a3b

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

README.markdown

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ I'd recommend you understand what's happening with those 3 tools before attempti
1919

2020
Include the gem on your Gemfile
2121

22-
gem 'rails3-jquery-autocomplete', '>= 0.1.1', :require => 'autocomplete'
22+
gem 'rails3-jquery-autocomplete', '>= 0.1.3', :require => 'autocomplete'
2323

2424
Install it
2525

@@ -51,6 +51,18 @@ don't forget to add it on your routes file
5151
get :autocomplete_brand_name, :on => :collection
5252
end
5353

54+
By default, the search starts from the beginning of the string you're searching for. If you want to do a full search, set the _full_ parameter to true.
55+
56+
class ProductsController < Admin::BaseController
57+
autocomplete :brand, :name, :full => true
58+
end
59+
60+
The following list would match the term query 'un':
61+
62+
* Luna
63+
* Unacceptable
64+
* Rerun
65+
5466
### View
5567

5668
On your view, all you have to do is include the attribute autocomplete on the text field

Rakefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ require 'rubygems'
22
require 'rake'
33
require 'echoe'
44

5-
Echoe.new('rails3-jquery-autocomplete', '0.1.2') do |p|
5+
Echoe.new('rails3-jquery-autocomplete', '0.1.3') do |p|
66
p.description = "Use jQuery's autocomplete plugin with Rails 3."
77
p.url = "http://github.com/crowdint/rails3-jquery-autocomplete"
88
p.author = "David Padilla"
99
p.email = "[email protected]"
1010
p.ignore_pattern = ["tmp/*", "script/*"]
1111
p.development_dependencies = []
12-
end
12+
end

lib/autocomplete.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ module ClassMethods
2929
def autocomplete(object, method, options = {})
3030
limit = options[:limit] || 10
3131
order = options[:order] || "#{method} ASC"
32+
inner = options[:full] || false
3233

3334
define_method("autocomplete_#{object}_#{method}") do
3435
unless params[:term] && params[:term].empty?
35-
items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "%#{params[:term].downcase}%"]).limit(limit).order(order)
36+
items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]).limit(limit).order(order)
3637
else
3738
items = {}
3839
end

test/controller_module_test.rb

+25-3
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ def test_alphabetic_order
7070
assert_equal(json_response.first["label"], "Alpha")
7171
assert_equal(json_response.last["label"], "Alzpha")
7272
end
73-
73+
7474
def test_alternative_sort_order
7575
@movie = Movie.create(:name => 'Alzpha')
7676
@movie = Movie.create(:name => 'Alspha')
7777
@movie = Movie.create(:name => 'Alpha')
7878

7979
ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
80-
80+
8181
get :autocomplete_movie_name, :term => 'Al'
8282
json_response = JSON.parse(@response.body)
8383
assert_equal(json_response.first["label"], "Alzpha")
@@ -88,11 +88,33 @@ def test_response_limit
8888
@movie = Movie.create(:name => 'Alzpha')
8989
@movie = Movie.create(:name => 'Alspha')
9090
@movie = Movie.create(:name => 'Alpha')
91-
91+
9292
ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
9393

9494
get :autocomplete_movie_name, :term => 'Al'
9595
json_response = JSON.parse(@response.body)
9696
assert_equal(json_response.length, 1)
9797
end
98+
99+
def test_downcase
100+
@movie = Movie.create(:name => 'aLpHa')
101+
102+
ActorsController.send(:autocomplete, :movie, :name)
103+
104+
get :autocomplete_movie_name, :term => 'Al'
105+
json_response = JSON.parse(@response.body)
106+
assert_equal(json_response.length, 1)
107+
assert_equal(json_response.first["label"], 'aLpHa')
108+
end
109+
110+
def test_full_search
111+
@movie = Movie.create(:name => 'aLpHa')
112+
113+
ActorsController.send(:autocomplete, :movie, :name, {:full => true})
114+
115+
get :autocomplete_movie_name, :term => 'ph'
116+
json_response = JSON.parse(@response.body)
117+
assert_equal(json_response.length, 1)
118+
assert_equal(json_response.first["label"], 'aLpHa')
119+
end
98120
end

0 commit comments

Comments
 (0)