rails3-jquery-autocomplete 0.3.6 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +13 -2
- data/lib/rails3-jquery-autocomplete.rb +5 -50
- data/lib/rails3-jquery-autocomplete/autocomplete.rb +42 -0
- data/lib/{form_helper.rb → rails3-jquery-autocomplete/form_helper.rb} +1 -1
- data/lib/rails3-jquery-autocomplete/helpers.rb +81 -0
- data/test/active_record_controller_test.rb +100 -0
- data/test/{autocomplete_generator_test.rb → generators/generator_test.rb} +0 -0
- data/test/helpers.rb +122 -0
- data/test/mongoid_controller_test.rb +100 -0
- data/test/support/active_record.rb +43 -0
- data/test/support/mongoid.rb +41 -0
- data/test/test_helper.rb +13 -21
- metadata +20 -10
- data/test/controller_module_test.rb +0 -138
data/README.markdown
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
# rails3-jquery-autocomplete
|
2
2
|
|
3
|
-
An easy way to use jQuery's autocomplete with Rails 3.
|
4
|
-
|
3
|
+
An easy way to use jQuery's autocomplete with Rails 3.
|
4
|
+
|
5
|
+
In now supports both ActiveRecord and [mongoid](http://github.com/mongoid/mongoid).
|
6
|
+
|
7
|
+
## ActiveRecord
|
8
|
+
|
9
|
+
You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)
|
10
|
+
on how to use this gem with ActiveRecord [here](http://github.com/crowdint/rails3-jquery-autocomplete-app).
|
11
|
+
|
12
|
+
## MongoID
|
13
|
+
|
14
|
+
You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid)
|
15
|
+
on how to use this gem with MongoID [here](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid).
|
5
16
|
|
6
17
|
## Before you start
|
7
18
|
|
@@ -1,53 +1,8 @@
|
|
1
|
-
require 'form_helper'
|
2
|
-
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
end
|
7
|
-
|
8
|
-
# Inspired on DHH's autocomplete plugin
|
9
|
-
#
|
10
|
-
# Usage:
|
11
|
-
#
|
12
|
-
# class ProductsController < Admin::BaseController
|
13
|
-
# autocomplete :brand, :name
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# This will magically generate an action autocomplete_brand_name, so,
|
17
|
-
# don't forget to add it on your routes file
|
18
|
-
#
|
19
|
-
# resources :products do
|
20
|
-
# get :autocomplete_brand_name, :on => :collection
|
21
|
-
# end
|
22
|
-
#
|
23
|
-
# Now, on your view, all you have to do is have a text field like:
|
24
|
-
#
|
25
|
-
# f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
|
26
|
-
#
|
27
|
-
#
|
28
|
-
module ClassMethods
|
29
|
-
def autocomplete(object, method, options = {})
|
30
|
-
limit = options[:limit] || 10
|
31
|
-
order = options[:order] || "#{method} ASC"
|
32
|
-
|
33
|
-
define_method("autocomplete_#{object}_#{method}") do
|
34
|
-
if 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)
|
36
|
-
else
|
37
|
-
items = {}
|
38
|
-
end
|
39
|
-
|
40
|
-
render :json => json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method))
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
def json_for_autocomplete(items, method)
|
47
|
-
items.collect {|i| {"id" => i.id, "label" => i.send(method), "value" => i.send(method)}}
|
48
|
-
end
|
49
|
-
end
|
1
|
+
require 'rails3-jquery-autocomplete/form_helper'
|
2
|
+
require 'rails3-jquery-autocomplete/helpers'
|
3
|
+
require 'rails3-jquery-autocomplete/autocomplete'
|
50
4
|
|
51
5
|
class ActionController::Base
|
52
|
-
|
6
|
+
extend Rails3JQueryAutocomplete::ClassMethods
|
7
|
+
include Rails3JQueryAutocomplete::Helpers
|
53
8
|
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
|
3
|
+
# Inspired on DHH's autocomplete plugin
|
4
|
+
#
|
5
|
+
# Usage:
|
6
|
+
#
|
7
|
+
# class ProductsController < Admin::BaseController
|
8
|
+
# autocomplete :brand, :name
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
# This will magically generate an action autocomplete_brand_name, so,
|
12
|
+
# don't forget to add it on your routes file
|
13
|
+
#
|
14
|
+
# resources :products do
|
15
|
+
# get :autocomplete_brand_name, :on => :collection
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# Now, on your view, all you have to do is have a text field like:
|
19
|
+
#
|
20
|
+
# f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
|
21
|
+
#
|
22
|
+
#
|
23
|
+
module ClassMethods
|
24
|
+
def autocomplete(object, method, options = {})
|
25
|
+
|
26
|
+
define_method("autocomplete_#{object}_#{method}") do
|
27
|
+
|
28
|
+
term = params[:term]
|
29
|
+
|
30
|
+
if term && !term.empty?
|
31
|
+
items = get_items(:model => get_object(object), \
|
32
|
+
:options => options, :term => term, :method => method)
|
33
|
+
else
|
34
|
+
items = {}
|
35
|
+
end
|
36
|
+
|
37
|
+
render :json => json_for_autocomplete(items, options[:display_value] ||= method)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
|
3
|
+
# Contains utility methods used by autocomplete
|
4
|
+
module Helpers
|
5
|
+
|
6
|
+
# Returns a three keys hash
|
7
|
+
def json_for_autocomplete(items, method)
|
8
|
+
items.collect {|item| {"id" => item.id, "label" => item.send(method), "value" => item.send(method)}}
|
9
|
+
end
|
10
|
+
|
11
|
+
# Returns parameter model_sym as a constant
|
12
|
+
#
|
13
|
+
# get_object(:actor)
|
14
|
+
# # returns a Actor constant supposing it is already defined
|
15
|
+
def get_object(model_sym)
|
16
|
+
object = model_sym.to_s.camelize.constantize
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns a symbol representing what implementation should be used to query
|
20
|
+
# the database and raises *NotImplementedError* if ORM implementor can not be found
|
21
|
+
def get_implementation(object)
|
22
|
+
if object.superclass.to_s == 'ActiveRecord::Base'
|
23
|
+
:activerecord
|
24
|
+
elsif object.included_modules.collect(&:to_s).include?('Mongoid::Document')
|
25
|
+
:mongoid
|
26
|
+
else
|
27
|
+
raise NotImplementedError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the order parameter to be used in the query created by get_items
|
32
|
+
def get_order(implementation, method, options)
|
33
|
+
order = options[:order]
|
34
|
+
|
35
|
+
case implementation
|
36
|
+
when :mongoid then
|
37
|
+
if order
|
38
|
+
order.split(',').collect do |fields|
|
39
|
+
sfields = fields.split
|
40
|
+
[sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
|
41
|
+
end
|
42
|
+
else
|
43
|
+
[[method.to_sym, :asc]]
|
44
|
+
end
|
45
|
+
when :activerecord then
|
46
|
+
order || "#{method} ASC"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Returns a limit that will be used on the query
|
51
|
+
def get_limit(options)
|
52
|
+
options[:limit] ||= 10
|
53
|
+
end
|
54
|
+
|
55
|
+
# Queries selected database
|
56
|
+
#
|
57
|
+
# items = get_items(:model => get_object(object), :options => options, :term => term, :method => method)
|
58
|
+
def get_items(parameters)
|
59
|
+
|
60
|
+
model = parameters[:model]
|
61
|
+
method = parameters[:method]
|
62
|
+
options = parameters[:options]
|
63
|
+
term = parameters[:term]
|
64
|
+
is_full_search = options[:full]
|
65
|
+
|
66
|
+
limit = get_limit(options)
|
67
|
+
implementation = get_implementation(model)
|
68
|
+
order = get_order(implementation, method, options)
|
69
|
+
|
70
|
+
case implementation
|
71
|
+
when :mongoid
|
72
|
+
search = (is_full_search ? '.*' : '^') + term + '.*'
|
73
|
+
items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
|
74
|
+
when :activerecord
|
75
|
+
items = model.where(["LOWER(#{method}) LIKE ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]) \
|
76
|
+
.limit(limit).order(order)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require 'support/active_record'
|
3
|
+
|
4
|
+
module Rails3JQueryAutocomplete
|
5
|
+
module Test
|
6
|
+
module ActiveRecord
|
7
|
+
|
8
|
+
class ActorsControllerTest < ActionController::TestCase
|
9
|
+
|
10
|
+
include Rails3JQueryAutocomplete::Test::ActiveRecord
|
11
|
+
|
12
|
+
setup do
|
13
|
+
@controller = ActorsController.new
|
14
|
+
end
|
15
|
+
|
16
|
+
context "the autocomplete gem" do
|
17
|
+
|
18
|
+
should "be able to access the autocomplete action regardless of the quality of param[:term]" do
|
19
|
+
get :autocomplete_movie_name
|
20
|
+
assert_response :success
|
21
|
+
|
22
|
+
get :autocomplete_movie_name, :term => ''
|
23
|
+
assert_response :success
|
24
|
+
|
25
|
+
get :autocomplete_movie_name, :term => nil
|
26
|
+
assert_response :success
|
27
|
+
|
28
|
+
get :autocomplete_movie_name, :term => 'Al'
|
29
|
+
assert_response :success
|
30
|
+
end
|
31
|
+
|
32
|
+
should "respond with expected json" do
|
33
|
+
get :autocomplete_movie_name, :term => 'Al'
|
34
|
+
json_response = JSON.parse(@response.body)
|
35
|
+
assert_equal(json_response.first["label"], @movie.name)
|
36
|
+
assert_equal(json_response.first["value"], @movie.name)
|
37
|
+
assert_equal(json_response.first["id"], @movie.id)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "return results in alphabetical order by default" do
|
41
|
+
get :autocomplete_movie_name, :term => 'Al'
|
42
|
+
json_response = JSON.parse(@response.body)
|
43
|
+
assert_equal(json_response.first["label"], "Alpha")
|
44
|
+
assert_equal(json_response.last["label"], "Alzpha")
|
45
|
+
end
|
46
|
+
|
47
|
+
should "be able to sort in other ways if desired" do
|
48
|
+
ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
|
49
|
+
|
50
|
+
get :autocomplete_movie_name, :term => 'Al'
|
51
|
+
json_response = JSON.parse(@response.body)
|
52
|
+
assert_equal(json_response.first["label"], "Alzpha")
|
53
|
+
assert_equal(json_response.last["label"], "Alpha")
|
54
|
+
end
|
55
|
+
|
56
|
+
should "be able to limit the results" do
|
57
|
+
ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
|
58
|
+
|
59
|
+
get :autocomplete_movie_name, :term => 'Al'
|
60
|
+
json_response = JSON.parse(@response.body)
|
61
|
+
assert_equal(json_response.length, 1)
|
62
|
+
end
|
63
|
+
|
64
|
+
should "ignore case of search term and results" do
|
65
|
+
@movie = Movie.create(:name => 'aLpHa')
|
66
|
+
|
67
|
+
ActorsController.send(:autocomplete, :movie, :name)
|
68
|
+
|
69
|
+
get :autocomplete_movie_name, :term => 'Al'
|
70
|
+
json_response = JSON.parse(@response.body)
|
71
|
+
assert_equal(json_response.length, Movie.count)
|
72
|
+
assert_equal(json_response.last["label"], 'aLpHa')
|
73
|
+
end
|
74
|
+
|
75
|
+
should "match term to letters in middle of words when full-text search is on" do
|
76
|
+
ActorsController.send(:autocomplete, :movie, :name, {:full => true})
|
77
|
+
|
78
|
+
get :autocomplete_movie_name, :term => 'ph'
|
79
|
+
json_response = JSON.parse(@response.body)
|
80
|
+
assert_equal(json_response.length, Movie.count)
|
81
|
+
assert_equal(json_response.first["label"], @movie.name)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "be able to customize what is displayed" do
|
85
|
+
ActorsController.send(:autocomplete, :movie, :name, {:display_value => :display_name})
|
86
|
+
|
87
|
+
get :autocomplete_movie_name, :term => 'Al'
|
88
|
+
|
89
|
+
json_response = JSON.parse(@response.body)
|
90
|
+
|
91
|
+
assert_equal(@movie.display_name, json_response.first["label"])
|
92
|
+
assert_equal(@movie.display_name, json_response.first["value"])
|
93
|
+
assert_equal(@movie.id, json_response.first["id"])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
File without changes
|
data/test/helpers.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
module Test
|
5
|
+
|
6
|
+
class HelpersTest < ::Test::Unit::TestCase
|
7
|
+
|
8
|
+
assert_structure = lambda do |hash|
|
9
|
+
assert_instance_of(String, hash['label'])
|
10
|
+
assert_instance_of(String, hash['value'])
|
11
|
+
end
|
12
|
+
|
13
|
+
items_exist_assert = lambda do |model|
|
14
|
+
items = Helpers.get_items(:model => model), :term => 'A', :method => 'name')
|
15
|
+
assert(items)
|
16
|
+
assert_equal(3, items.size)
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with Mongoid support' do
|
20
|
+
|
21
|
+
require 'support/mongoid'
|
22
|
+
|
23
|
+
context 'passing a Mongoid query result' do
|
24
|
+
|
25
|
+
include Rails3JQueryAutocomplete::Mongoid::Test
|
26
|
+
|
27
|
+
should 'parse items to JSON' do
|
28
|
+
response = Helpers.json_for_autocomplete(Game.all, :name)
|
29
|
+
assert_not_nil(response)
|
30
|
+
response.each(&assert_structure)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'looking for items' do
|
35
|
+
|
36
|
+
include Rails3JQueryAutocomplete::Mongoid::Test
|
37
|
+
|
38
|
+
should 'return items' do
|
39
|
+
items_exist_assert.call(Game)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'with ActiveRecord support' do
|
45
|
+
|
46
|
+
require 'support/active_record'
|
47
|
+
|
48
|
+
context 'passing an ActiveRecord query result' do
|
49
|
+
|
50
|
+
include Rails3JQueryAutocomplete::ActiveRecord::Test
|
51
|
+
|
52
|
+
should 'parse items to JSON' do
|
53
|
+
response = Helpers.json_for_autocomplete(Game.all, :name)
|
54
|
+
assert_not_nil(response)
|
55
|
+
response.each(&assert_structure)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'looking for items' do
|
60
|
+
|
61
|
+
include Rails3JQueryAutocomplete::ActiveRecord::Test
|
62
|
+
|
63
|
+
should 'return items' do
|
64
|
+
items_exist_assert.call(Movie)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'converting a symbol to connstant' do
|
70
|
+
should 'return a defined constant' do
|
71
|
+
::MyJustDefinedConstant = Class.new
|
72
|
+
assert_equal(::MyJustDefinedConstant, Helpers.get_object(:my_just_defined_constant))
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'returning one symbol for an implemented adapter' do
|
77
|
+
|
78
|
+
require 'support/active_record'
|
79
|
+
require 'support/mongoid'
|
80
|
+
|
81
|
+
should 'return a symbol for defined interface' do
|
82
|
+
assert_equal(:activerecord, Helpers.get_implementation(::Movie))
|
83
|
+
assert_equal(:mongoid, Helpers.get_implementation(::Game))
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'returning a limit integer number' do
|
88
|
+
|
89
|
+
should 'return default value of 10' do
|
90
|
+
assert_equal(10, Helpers.get_limit({}))
|
91
|
+
end
|
92
|
+
|
93
|
+
should 'return params value' do
|
94
|
+
assert_equal(1, Helpers.get_limit(:limit => 1))
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'returning elements ordering' do
|
99
|
+
|
100
|
+
should 'returning default ascending order for activerecord' do
|
101
|
+
assert_equal("name ASC", Helpers.get_order(:activerecord, 'name', {}))
|
102
|
+
end
|
103
|
+
|
104
|
+
should 'returning descending order for activerecord' do
|
105
|
+
assert_equal("thisfield DESC", Helpers.get_order(:activerecord, 'otherfield', :order => 'thisfield DESC'))
|
106
|
+
end
|
107
|
+
|
108
|
+
should 'returning default ascending order for mongoid' do
|
109
|
+
expected = [[:name, :asc]]
|
110
|
+
returned = Helpers.get_order(:mongoid, 'name', {})
|
111
|
+
assert expected.eql?(returned)
|
112
|
+
end
|
113
|
+
|
114
|
+
should 'returning order according paramters for mongoid' do
|
115
|
+
expected = [[:first, :asc], [:second, :desc]]
|
116
|
+
returned = Helpers.get_order(:mongoid, 'name', {:order => 'first asc, second desc'})
|
117
|
+
assert expected.eql?(returned)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require 'support/mongoid'
|
3
|
+
|
4
|
+
module Rails3JQueryAutocomplete
|
5
|
+
module Test
|
6
|
+
module Mongoid
|
7
|
+
|
8
|
+
class GamesControllerTest < ActionController::TestCase
|
9
|
+
|
10
|
+
include Rails3JQueryAutocomplete::Test::Mongoid
|
11
|
+
|
12
|
+
setup do
|
13
|
+
@controller = GamesController.new
|
14
|
+
end
|
15
|
+
|
16
|
+
context "the autocomplete gem" do
|
17
|
+
|
18
|
+
should "be able to access the autocomplete action regardless of the quality of param[:term]" do
|
19
|
+
get :autocomplete_game_name
|
20
|
+
assert_response :success
|
21
|
+
|
22
|
+
get :autocomplete_game_name, :term => ''
|
23
|
+
assert_response :success
|
24
|
+
|
25
|
+
get :autocomplete_game_name, :term => nil
|
26
|
+
assert_response :success
|
27
|
+
|
28
|
+
get :autocomplete_game_name, :term => 'Al'
|
29
|
+
assert_response :success
|
30
|
+
end
|
31
|
+
|
32
|
+
should "respond with expected json" do
|
33
|
+
get :autocomplete_game_name, :term => 'Al'
|
34
|
+
json_response = JSON.parse(@response.body)
|
35
|
+
assert_equal(json_response.first["label"], @game.name)
|
36
|
+
assert_equal(json_response.first["value"], @game.name)
|
37
|
+
assert_equal(json_response.first["id"], @game.id.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
should "return results in alphabetical order by default" do
|
41
|
+
get :autocomplete_game_name, :term => 'Al'
|
42
|
+
json_response = JSON.parse(@response.body)
|
43
|
+
assert_equal(json_response.first["label"], "Alpha")
|
44
|
+
assert_equal(json_response.last["label"], "Alzpha")
|
45
|
+
end
|
46
|
+
|
47
|
+
should "be able to sort in other ways if desired" do
|
48
|
+
GamesController.send(:autocomplete, :game, :name, {:order => "name DESC"})
|
49
|
+
|
50
|
+
get :autocomplete_game_name, :term => 'Al'
|
51
|
+
json_response = JSON.parse(@response.body)
|
52
|
+
assert_equal(json_response.first["label"], "Alzpha")
|
53
|
+
assert_equal(json_response.last["label"], "Alpha")
|
54
|
+
end
|
55
|
+
|
56
|
+
should "be able to limit the results" do
|
57
|
+
GamesController.send(:autocomplete, :game, :name, {:limit => 1})
|
58
|
+
|
59
|
+
get :autocomplete_game_name, :term => 'Al'
|
60
|
+
json_response = JSON.parse(@response.body)
|
61
|
+
assert_equal(json_response.length, 1)
|
62
|
+
end
|
63
|
+
|
64
|
+
should "ignore case of search term and results" do
|
65
|
+
@game = Game.create(:name => 'aLpHa')
|
66
|
+
|
67
|
+
GamesController.send(:autocomplete, :game, :name)
|
68
|
+
|
69
|
+
get :autocomplete_game_name, :term => 'Al'
|
70
|
+
json_response = JSON.parse(@response.body)
|
71
|
+
assert_equal(json_response.length, Game.count)
|
72
|
+
assert_equal(json_response.last["label"], 'aLpHa')
|
73
|
+
end
|
74
|
+
|
75
|
+
should "match term to letters in middle of words when full-text search is on" do
|
76
|
+
GamesController.send(:autocomplete, :game, :name, {:full => true})
|
77
|
+
|
78
|
+
get :autocomplete_game_name, :term => 'ph'
|
79
|
+
json_response = JSON.parse(@response.body)
|
80
|
+
assert_equal(json_response.length, Game.count)
|
81
|
+
assert_equal(json_response.first["label"], @game.name)
|
82
|
+
end
|
83
|
+
|
84
|
+
should "be able to customize what is displayed" do
|
85
|
+
GamesController.send(:autocomplete, :game, :name, {:display_value => :display_name})
|
86
|
+
|
87
|
+
get :autocomplete_game_name, :term => 'Al'
|
88
|
+
|
89
|
+
json_response = JSON.parse(@response.body)
|
90
|
+
|
91
|
+
assert_equal(@game.display_name, json_response.first["label"])
|
92
|
+
assert_equal(@game.display_name, json_response.first["value"])
|
93
|
+
assert_equal(@game.id.to_s, json_response.first["id"])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class Actor < ActiveRecord::Base
|
2
|
+
belongs_to :movie
|
3
|
+
end
|
4
|
+
|
5
|
+
class Movie < ActiveRecord::Base
|
6
|
+
def display_name
|
7
|
+
"Movie: #{name}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ActorsController < ActionController::Base
|
12
|
+
autocomplete :movie, :name
|
13
|
+
end
|
14
|
+
|
15
|
+
module Rails3JQueryAutocomplete
|
16
|
+
module Test
|
17
|
+
module ActiveRecord
|
18
|
+
def setup
|
19
|
+
::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
20
|
+
::ActiveRecord::Schema.define(:version => 1) do
|
21
|
+
create_table :movies do |t|
|
22
|
+
t.column :name, :string
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :actors do |t|
|
26
|
+
t.column :movie_id, :integer
|
27
|
+
t.column :name, :string
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@movie = Movie.create(:name => 'Alpha')
|
32
|
+
@movie2 = Movie.create(:name => 'Alspha')
|
33
|
+
@movie3 = Movie.create(:name => 'Alzpha')
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
::ActiveRecord::Base.connection.tables.each do |table|
|
38
|
+
::ActiveRecord::Base.connection.drop_table(table)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Character
|
2
|
+
include Mongoid::Document
|
3
|
+
field :name, :class => String
|
4
|
+
referenced_in :game
|
5
|
+
end
|
6
|
+
|
7
|
+
class Game
|
8
|
+
include Mongoid::Document
|
9
|
+
field :name, :class => String
|
10
|
+
references_one :character
|
11
|
+
def display_name
|
12
|
+
"Game: #{name}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class GamesController < ActionController::Base
|
17
|
+
autocomplete :game, :name
|
18
|
+
end
|
19
|
+
|
20
|
+
module Rails3JQueryAutocomplete
|
21
|
+
module Test
|
22
|
+
module Mongoid
|
23
|
+
def setup
|
24
|
+
::Mongoid.configure do |config|
|
25
|
+
name = "rails3_jquery_autocomplete_test"
|
26
|
+
host = "localhost"
|
27
|
+
config.master = Mongo::Connection.new.db(name)
|
28
|
+
config.logger = nil
|
29
|
+
end
|
30
|
+
|
31
|
+
@game = Game.create(:name => 'Alpha')
|
32
|
+
@game2 = Game.create(:name => 'Alspha')
|
33
|
+
@game3 = Game.create(:name => 'Alzpha')
|
34
|
+
end
|
35
|
+
|
36
|
+
def teardown
|
37
|
+
::Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,34 +1,26 @@
|
|
1
1
|
require 'test/unit'
|
2
|
-
|
3
2
|
require 'rubygems'
|
4
3
|
gem 'rails', '>=3.0.0.rc'
|
5
|
-
gem 'sqlite3-ruby'
|
6
4
|
|
7
5
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
6
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
7
|
|
10
|
-
|
11
|
-
require "active_record"
|
12
|
-
require "active_model"
|
13
|
-
require "action_controller"
|
14
|
-
require "rails/railtie"
|
8
|
+
ENV["RAILS_ENV"] = "test"
|
15
9
|
|
10
|
+
require 'rails/all'
|
11
|
+
require 'mongoid'
|
12
|
+
require 'shoulda'
|
13
|
+
require 'redgreen'
|
14
|
+
require 'rails/test_help'
|
16
15
|
require 'rails3-jquery-autocomplete'
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
Rails3JQueryAutocomplete::Routes = ActionDispatch::Routing::RouteSet.new
|
23
|
-
Rails3JQueryAutocomplete::Routes.draw do |map|
|
24
|
-
map.connect ':controller/:action/:id'
|
25
|
-
map.connect ':controller/:action'
|
17
|
+
module Rails3JQueryAutocomplete
|
18
|
+
class Application < Rails::Application
|
19
|
+
end
|
26
20
|
end
|
27
21
|
|
28
|
-
|
29
|
-
|
30
|
-
class ActiveSupport::TestCase
|
31
|
-
setup do
|
32
|
-
@routes = Rails3JQueryAutocomplete::Routes
|
33
|
-
end
|
22
|
+
Rails3JQueryAutocomplete::Application.routes.draw do
|
23
|
+
match '/:controller(/:action(/:id))'
|
34
24
|
end
|
25
|
+
|
26
|
+
ActionController::Base.send :include, Rails3JQueryAutocomplete::Application.routes.url_helpers
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails3-jquery-autocomplete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- David Padilla
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-10-
|
18
|
+
date: 2010-10-22 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -47,13 +47,19 @@ files:
|
|
47
47
|
- README.markdown
|
48
48
|
- Rakefile
|
49
49
|
- lib/cucumber/autocomplete.rb
|
50
|
-
- lib/form_helper.rb
|
51
50
|
- lib/generators/autocomplete_generator.rb
|
52
51
|
- lib/generators/templates/autocomplete-rails.js
|
53
52
|
- lib/rails3-jquery-autocomplete.rb
|
54
|
-
-
|
55
|
-
-
|
53
|
+
- lib/rails3-jquery-autocomplete/autocomplete.rb
|
54
|
+
- lib/rails3-jquery-autocomplete/form_helper.rb
|
55
|
+
- lib/rails3-jquery-autocomplete/helpers.rb
|
56
|
+
- test/active_record_controller_test.rb
|
56
57
|
- test/form_helper_test.rb
|
58
|
+
- test/generators/generator_test.rb
|
59
|
+
- test/helpers.rb
|
60
|
+
- test/mongoid_controller_test.rb
|
61
|
+
- test/support/active_record.rb
|
62
|
+
- test/support/mongoid.rb
|
57
63
|
- test/test_helper.rb
|
58
64
|
- LICENSE
|
59
65
|
has_rdoc: true
|
@@ -91,7 +97,11 @@ signing_key:
|
|
91
97
|
specification_version: 3
|
92
98
|
summary: Use jQuery's autocomplete plugin with Rails 3.
|
93
99
|
test_files:
|
94
|
-
- test/
|
95
|
-
- test/controller_module_test.rb
|
100
|
+
- test/active_record_controller_test.rb
|
96
101
|
- test/form_helper_test.rb
|
102
|
+
- test/generators/generator_test.rb
|
103
|
+
- test/helpers.rb
|
104
|
+
- test/mongoid_controller_test.rb
|
105
|
+
- test/support/active_record.rb
|
106
|
+
- test/support/mongoid.rb
|
97
107
|
- test/test_helper.rb
|
@@ -1,138 +0,0 @@
|
|
1
|
-
require "test_helper"
|
2
|
-
|
3
|
-
class ActorsController < ApplicationController
|
4
|
-
autocomplete :movie, :name
|
5
|
-
end
|
6
|
-
|
7
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
-
|
9
|
-
class Actor < ActiveRecord::Base
|
10
|
-
belongs_to :movie
|
11
|
-
end
|
12
|
-
|
13
|
-
class Movie < ActiveRecord::Base
|
14
|
-
def display_name
|
15
|
-
"Movie: #{name}"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup_db
|
20
|
-
ActiveRecord::Schema.define(:version => 1) do
|
21
|
-
create_table :movies do |t|
|
22
|
-
t.column :name, :string
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table :actors do |t|
|
26
|
-
t.column :movie_id, :integer
|
27
|
-
t.column :name, :string
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def teardown_db
|
33
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
34
|
-
ActiveRecord::Base.connection.drop_table(table)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class ActorsControllerTest < ActionController::TestCase
|
39
|
-
require 'shoulda'
|
40
|
-
require 'redgreen'
|
41
|
-
def setup
|
42
|
-
setup_db
|
43
|
-
|
44
|
-
@controller = ActorsController.new
|
45
|
-
@controller.request = @request = ActionController::TestRequest.new
|
46
|
-
@controller.response = @response = ActionController::TestResponse.new
|
47
|
-
end
|
48
|
-
|
49
|
-
def teardown
|
50
|
-
teardown_db
|
51
|
-
end
|
52
|
-
|
53
|
-
context "the autocomplete gem" do
|
54
|
-
setup do
|
55
|
-
@movie = Movie.create(:name => 'Alpha')
|
56
|
-
@movie2 = Movie.create(:name => 'Alspha')
|
57
|
-
@movie3 = Movie.create(:name => 'Alzpha')
|
58
|
-
end
|
59
|
-
|
60
|
-
should "be able to access the autocomplete action regardless of the quality of param[:term]" do
|
61
|
-
get :autocomplete_movie_name
|
62
|
-
assert_response :success
|
63
|
-
|
64
|
-
get :autocomplete_movie_name, :term => ''
|
65
|
-
assert_response :success
|
66
|
-
|
67
|
-
get :autocomplete_movie_name, :term => nil
|
68
|
-
assert_response :success
|
69
|
-
|
70
|
-
get :autocomplete_movie_name, :term => 'Al'
|
71
|
-
assert_response :success
|
72
|
-
end
|
73
|
-
|
74
|
-
should "respond with expected json" do
|
75
|
-
get :autocomplete_movie_name, :term => 'Al'
|
76
|
-
json_response = JSON.parse(@response.body)
|
77
|
-
assert_equal(json_response.first["label"], @movie.name)
|
78
|
-
assert_equal(json_response.first["value"], @movie.name)
|
79
|
-
assert_equal(json_response.first["id"], @movie.id)
|
80
|
-
end
|
81
|
-
|
82
|
-
should "return results in alphabetical order by default" do
|
83
|
-
get :autocomplete_movie_name, :term => 'Al'
|
84
|
-
json_response = JSON.parse(@response.body)
|
85
|
-
assert_equal(json_response.first["label"], "Alpha")
|
86
|
-
assert_equal(json_response.last["label"], "Alzpha")
|
87
|
-
end
|
88
|
-
|
89
|
-
should "be able to sort in other ways if desired" do
|
90
|
-
ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
|
91
|
-
|
92
|
-
get :autocomplete_movie_name, :term => 'Al'
|
93
|
-
json_response = JSON.parse(@response.body)
|
94
|
-
assert_equal(json_response.first["label"], "Alzpha")
|
95
|
-
assert_equal(json_response.last["label"], "Alpha")
|
96
|
-
end
|
97
|
-
|
98
|
-
should "be able to limit the results" do
|
99
|
-
ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
|
100
|
-
|
101
|
-
get :autocomplete_movie_name, :term => 'Al'
|
102
|
-
json_response = JSON.parse(@response.body)
|
103
|
-
assert_equal(json_response.length, 1)
|
104
|
-
end
|
105
|
-
|
106
|
-
should "ignore case of search term and results" do
|
107
|
-
@movie = Movie.create(:name => 'aLpHa')
|
108
|
-
|
109
|
-
ActorsController.send(:autocomplete, :movie, :name)
|
110
|
-
|
111
|
-
get :autocomplete_movie_name, :term => 'Al'
|
112
|
-
json_response = JSON.parse(@response.body)
|
113
|
-
assert_equal(json_response.length, Movie.count)
|
114
|
-
assert_equal(json_response.last["label"], 'aLpHa')
|
115
|
-
end
|
116
|
-
|
117
|
-
should "match term to letters in middle of words when full-text search is on" do
|
118
|
-
ActorsController.send(:autocomplete, :movie, :name, {:full => true})
|
119
|
-
|
120
|
-
get :autocomplete_movie_name, :term => 'ph'
|
121
|
-
json_response = JSON.parse(@response.body)
|
122
|
-
assert_equal(json_response.length, Movie.count)
|
123
|
-
assert_equal(json_response.first["label"], @movie.name)
|
124
|
-
end
|
125
|
-
|
126
|
-
should "be able to customize what is displayed" do
|
127
|
-
ActorsController.send(:autocomplete, :movie, :name, {:display_value => :display_name})
|
128
|
-
|
129
|
-
get :autocomplete_movie_name, :term => 'Al'
|
130
|
-
|
131
|
-
json_response = JSON.parse(@response.body)
|
132
|
-
|
133
|
-
assert_equal(@movie.display_name, json_response.first["label"])
|
134
|
-
assert_equal(@movie.display_name, json_response.first["value"])
|
135
|
-
assert_equal(@movie.id, json_response.first["id"])
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|