Permalink
Browse files

The full parameter

  • Loading branch information...
1 parent d167cce commit 9617a3b34c96075dd7d5988634aea0e92088b68c David Padilla committed Jul 25, 2010
Showing with 42 additions and 7 deletions.
  1. +13 −1 README.markdown
  2. +2 −2 Rakefile
  3. +2 −1 lib/autocomplete.rb
  4. +25 −3 test/controller_module_test.rb
View
@@ -19,7 +19,7 @@ I'd recommend you understand what's happening with those 3 tools before attempti
Include the gem on your Gemfile
- gem 'rails3-jquery-autocomplete', '>= 0.1.1', :require => 'autocomplete'
+ gem 'rails3-jquery-autocomplete', '>= 0.1.3', :require => 'autocomplete'
Install it
@@ -51,6 +51,18 @@ don't forget to add it on your routes file
get :autocomplete_brand_name, :on => :collection
end
+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.
+
+ class ProductsController < Admin::BaseController
+ autocomplete :brand, :name, :full => true
+ end
+
+The following list would match the term query 'un':
+
+* Luna
+* Unacceptable
+* Rerun
+
### View
On your view, all you have to do is include the attribute autocomplete on the text field
View
@@ -2,11 +2,11 @@ require 'rubygems'
require 'rake'
require 'echoe'
-Echoe.new('rails3-jquery-autocomplete', '0.1.2') do |p|
+Echoe.new('rails3-jquery-autocomplete', '0.1.3') do |p|
p.description = "Use jQuery's autocomplete plugin with Rails 3."
p.url = "http://github.com/crowdint/rails3-jquery-autocomplete"
p.author = "David Padilla"
p.email = "david@crowdint.com"
p.ignore_pattern = ["tmp/*", "script/*"]
p.development_dependencies = []
-end
+end
View
@@ -29,10 +29,11 @@ module ClassMethods
def autocomplete(object, method, options = {})
limit = options[:limit] || 10
order = options[:order] || "#{method} ASC"
+ inner = options[:full] || false
define_method("autocomplete_#{object}_#{method}") do
unless params[:term] && params[:term].empty?
- items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "%#{params[:term].downcase}%"]).limit(limit).order(order)
+ items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]).limit(limit).order(order)
else
items = {}
end
@@ -70,14 +70,14 @@ def test_alphabetic_order
assert_equal(json_response.first["label"], "Alpha")
assert_equal(json_response.last["label"], "Alzpha")
end
-
+
def test_alternative_sort_order
@movie = Movie.create(:name => 'Alzpha')
@movie = Movie.create(:name => 'Alspha')
@movie = Movie.create(:name => 'Alpha')
ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
-
+
get :autocomplete_movie_name, :term => 'Al'
json_response = JSON.parse(@response.body)
assert_equal(json_response.first["label"], "Alzpha")
@@ -88,11 +88,33 @@ def test_response_limit
@movie = Movie.create(:name => 'Alzpha')
@movie = Movie.create(:name => 'Alspha')
@movie = Movie.create(:name => 'Alpha')
-
+
ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
get :autocomplete_movie_name, :term => 'Al'
json_response = JSON.parse(@response.body)
assert_equal(json_response.length, 1)
end
+
+ def test_downcase
+ @movie = Movie.create(:name => 'aLpHa')
+
+ ActorsController.send(:autocomplete, :movie, :name)
+
+ get :autocomplete_movie_name, :term => 'Al'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.length, 1)
+ assert_equal(json_response.first["label"], 'aLpHa')
+ end
+
+ def test_full_search
+ @movie = Movie.create(:name => 'aLpHa')
+
+ ActorsController.send(:autocomplete, :movie, :name, {:full => true})
+
+ get :autocomplete_movie_name, :term => 'ph'
+ json_response = JSON.parse(@response.body)
+ assert_equal(json_response.length, 1)
+ assert_equal(json_response.first["label"], 'aLpHa')
+ end
end

0 comments on commit 9617a3b

Please sign in to comment.