rails3-jquery-autocomplete 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG.md +1 -0
- data/README.markdown +11 -2
- data/Rakefile +8 -0
- data/integration/app/views/id_elements/new.html.haml +1 -1
- data/integration/public/javascripts/autocomplete-rails.js +2 -99
- data/lib/generators/templates/autocomplete-rails-uncompressed.js +4 -4
- data/lib/generators/templates/autocomplete-rails.js +2 -2
- data/lib/rails3-jquery-autocomplete.rb +7 -6
- data/lib/rails3-jquery-autocomplete/autocomplete.rb +87 -41
- data/lib/rails3-jquery-autocomplete/orm.rb +8 -0
- data/lib/rails3-jquery-autocomplete/orm/active_record.rb +47 -0
- data/lib/rails3-jquery-autocomplete/orm/mongo_mapper.rb +30 -0
- data/lib/rails3-jquery-autocomplete/orm/mongoid.rb +30 -0
- data/lib/rails3-jquery-autocomplete/version.rb +1 -1
- data/rails3-jquery-autocomplete.gemspec +2 -1
- data/test/generators/autocomplete/install_generator_test.rb +1 -1
- data/test/generators/autocomplete/uncompressed_generator_test.rb +2 -2
- data/test/lib/rails3-jquery-autocomplete/autocomplete_test.rb +59 -0
- data/test/lib/rails3-jquery-autocomplete/orm/active_record_test.rb +130 -0
- data/test/lib/rails3-jquery-autocomplete/orm/mongo_mapper_test.rb +60 -0
- data/test/lib/rails3-jquery-autocomplete/orm/mongoid_test.rb +60 -0
- data/test/lib/rails3-jquery-autocomplete_test.rb +38 -0
- data/test/test_helper.rb +5 -2
- metadata +56 -41
- data/lib/rails3-jquery-autocomplete/helpers.rb +0 -137
- data/test/implementations_test.rb +0 -41
- data/test/lib/rails3-jquery-autocomplete/helpers_test.rb +0 -16
- data/test/support/active_record.rb +0 -44
- data/test/support/mongo_mapper.rb +0 -39
- data/test/support/mongoid.rb +0 -43
- data/test/test_controller.rb +0 -101
@@ -0,0 +1,30 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
module Orm
|
3
|
+
module Mongoid
|
4
|
+
def get_autocomplete_order(method, options, model=nil)
|
5
|
+
order = options[:order]
|
6
|
+
if order
|
7
|
+
order.split(',').collect do |fields|
|
8
|
+
sfields = fields.split
|
9
|
+
[sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
|
10
|
+
end
|
11
|
+
else
|
12
|
+
[[method.to_sym, :asc]]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_autocomplete_items(parameters)
|
17
|
+
model = parameters[:model]
|
18
|
+
method = parameters[:method]
|
19
|
+
options = parameters[:options]
|
20
|
+
is_full_search = options[:full]
|
21
|
+
term = parameters[:term]
|
22
|
+
limit = get_autocomplete_limit(options)
|
23
|
+
order = get_autocomplete_order(method, options)
|
24
|
+
|
25
|
+
search = (is_full_search ? '.*' : '^') + term + '.*'
|
26
|
+
items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.description = %q{Use jQuery's autocomplete plugin with Rails 3.}
|
14
14
|
|
15
15
|
s.add_dependency('rails', '~>3.0')
|
16
|
-
s.add_dependency('yajl-ruby')
|
17
16
|
|
18
17
|
s.add_development_dependency('sqlite3-ruby')
|
19
18
|
s.add_development_dependency('mongoid', '>= 2.0.0')
|
@@ -21,6 +20,8 @@ Gem::Specification.new do |s|
|
|
21
20
|
s.add_development_dependency('bson_ext', '~>1.3.0')
|
22
21
|
s.add_development_dependency('shoulda', '~>2.11.1')
|
23
22
|
s.add_development_dependency('uglifier')
|
23
|
+
s.add_development_dependency('rr')
|
24
|
+
s.add_development_dependency('rcov')
|
24
25
|
|
25
26
|
s.files = `git ls-files`.split("\n")
|
26
27
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -10,14 +10,14 @@ module Autocomplete
|
|
10
10
|
|
11
11
|
File.unlink(@filename) if File.exists?(@filename)
|
12
12
|
|
13
|
-
|
13
|
+
UncompressedGenerator.start([], :destination_root => @destination)
|
14
14
|
end
|
15
15
|
|
16
16
|
def test_install
|
17
17
|
assert File.exists?(@filename)
|
18
18
|
|
19
19
|
assert_equal(
|
20
|
-
File.read(File.join(@source, 'autocomplete-rails.js')),
|
20
|
+
File.read(File.join(@source, 'autocomplete-rails-uncompressed.js')),
|
21
21
|
File.read(@filename)
|
22
22
|
)
|
23
23
|
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
class AutocompleteTest < Test::Unit::TestCase
|
5
|
+
include Rails3JQueryAutocomplete::Autocomplete
|
6
|
+
|
7
|
+
context '#get_autocomplete_limit' do
|
8
|
+
context 'the limit option was specified' do
|
9
|
+
should "return the limit option" do
|
10
|
+
assert_equal 99, get_autocomplete_limit({:limit => 99})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'the limit option is not specified' do
|
15
|
+
should 'return 10' do
|
16
|
+
assert_equal 10, get_autocomplete_limit({})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context '#get_object' do
|
22
|
+
should 'return the specified sym as a class name' do
|
23
|
+
symbol = Object.new
|
24
|
+
class_object = Class.new
|
25
|
+
mock(symbol).to_s.mock!.camelize.mock!.constantize { class_object }
|
26
|
+
assert_equal class_object, get_object(symbol)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#json_for_autocomplete' do
|
31
|
+
should 'parse items to JSON' do
|
32
|
+
item = mock(Object)
|
33
|
+
mock(item).send(:name).times(2) { 'Object Name' }
|
34
|
+
mock(item).id { 1 }
|
35
|
+
items = [item]
|
36
|
+
response = self.json_for_autocomplete(items, :name).first
|
37
|
+
assert_equal response["id"], "1"
|
38
|
+
assert_equal response["value"], "Object Name"
|
39
|
+
assert_equal response["label"], "Object Name"
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with extra data' do
|
43
|
+
should 'add that extra data to result' do
|
44
|
+
item = mock(Object)
|
45
|
+
mock(item).send(:name).times(2) { 'Object Name' }
|
46
|
+
mock(item).id { 1 }
|
47
|
+
mock(item).send("extra") { 'Object Extra ' }
|
48
|
+
|
49
|
+
items = [item]
|
50
|
+
response = self.json_for_autocomplete(items, :name, ["extra"]).first
|
51
|
+
|
52
|
+
assert_equal "1" , response["id"]
|
53
|
+
assert_equal "Object Name" , response["value"]
|
54
|
+
assert_equal "Object Name" , response["label"]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
module Orm
|
5
|
+
class ActiveRecordTest < Test::Unit::TestCase
|
6
|
+
include Rails3JQueryAutocomplete::Orm::ActiveRecord
|
7
|
+
|
8
|
+
context "#get_autocomplete_order" do
|
9
|
+
context 'order is specified' do
|
10
|
+
should 'returns that order option' do
|
11
|
+
assert_equal "field ASC", get_autocomplete_order(:field, {:order => 'field ASC'})
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'no order is specified' do
|
16
|
+
should 'return the order clause by the field ASC' do
|
17
|
+
assert_equal "field ASC", get_autocomplete_order(:field, {})
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'a different model is specified' do
|
21
|
+
should 'return the order clause by the table_name.field ASC' do
|
22
|
+
model = Object.new
|
23
|
+
mock(model).table_name { 'table_name' }
|
24
|
+
assert_equal "table_name.field ASC", get_autocomplete_order(:field, {}, model)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context '#get_autocomplete_items' do
|
31
|
+
should 'retrieve the items from ActiveRecord' do
|
32
|
+
class Dog ; end
|
33
|
+
|
34
|
+
model = Dog
|
35
|
+
scoped = []
|
36
|
+
whered = []
|
37
|
+
term = 'query'
|
38
|
+
method = :field
|
39
|
+
|
40
|
+
options = {
|
41
|
+
:model => model,
|
42
|
+
:term => term,
|
43
|
+
:method => method,
|
44
|
+
:options => {}
|
45
|
+
}
|
46
|
+
|
47
|
+
mock(self).get_autocomplete_limit(anything) { 10 }
|
48
|
+
mock(self).get_autocomplete_order(anything, anything, anything) { "order ASC" }
|
49
|
+
mock(self).get_autocomplete_select_clause(model, method, {}) { ["field"] }
|
50
|
+
mock(self).get_autocomplete_where_clause(model, term, method, {}) { ["WHERE something"] }
|
51
|
+
mock(model).table_name.times(any_times) { 'model_table_name' }
|
52
|
+
|
53
|
+
mock(model).scoped { model }
|
54
|
+
mock(model).select(["field"]) { model }
|
55
|
+
mock(model).where(["WHERE something"]).mock!.limit(10).mock!.
|
56
|
+
order("order ASC") { 1 }
|
57
|
+
|
58
|
+
assert_equal 1, get_autocomplete_items(options)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context '#get_autocomplete_select_clause' do
|
63
|
+
setup do
|
64
|
+
@model = Object.new
|
65
|
+
mock(@model).table_name { 'table_name' }
|
66
|
+
mock(@model).primary_key { 'id' }
|
67
|
+
end
|
68
|
+
|
69
|
+
should 'create a select clause' do
|
70
|
+
assert_equal ["table_name.id", "table_name.method"],
|
71
|
+
get_autocomplete_select_clause(@model, :method, {})
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'with extra options' do
|
75
|
+
should 'return those extra fields on the clause' do
|
76
|
+
options = {:extra_data => ['table_name.created_at']}
|
77
|
+
|
78
|
+
assert_equal ["table_name.id", "table_name.method", "table_name.created_at"],
|
79
|
+
get_autocomplete_select_clause(@model, :method, options)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context '#get_autocomplete_where_clause' do
|
85
|
+
setup do
|
86
|
+
@model = Object.new
|
87
|
+
mock(@model).table_name { 'table_name' }
|
88
|
+
|
89
|
+
@term = 'query'
|
90
|
+
@options = {}
|
91
|
+
@method = :method
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'Not Postgres' do
|
95
|
+
should 'return options for where' do
|
96
|
+
mock(self).postgres? { false }
|
97
|
+
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context 'Postgres' do
|
102
|
+
should 'return options for where with ILIKE' do
|
103
|
+
mock(self).postgres? { true }
|
104
|
+
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'full search' do
|
109
|
+
should 'return options for where with the term sourrounded by %%' do
|
110
|
+
mock(self).postgres? { false }
|
111
|
+
@options[:full] = true
|
112
|
+
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context '#postgres?' do
|
118
|
+
should 'return nil if PGConn is not defined' do
|
119
|
+
assert_nil self.postgres?
|
120
|
+
end
|
121
|
+
|
122
|
+
should 'return true if PGConn is defined' do
|
123
|
+
class ::PGConn ; end
|
124
|
+
|
125
|
+
assert self.postgres?
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
module Orm
|
5
|
+
class MongoMapperTest < Test::Unit::TestCase
|
6
|
+
include Rails3JQueryAutocomplete::Orm::MongoMapper
|
7
|
+
|
8
|
+
context "#get_autocomplete_order" do
|
9
|
+
context "order is specified" do
|
10
|
+
should 'returns the parametrized order for Mongoid' do
|
11
|
+
assert_equal [[:field, :asc], [:state, :desc]],
|
12
|
+
get_autocomplete_order(:method, :order => 'field ASC, state DESC')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'order is not specified' do
|
17
|
+
should 'return the method ordered ASC by default' do
|
18
|
+
assert_equal [[:method, :asc]],
|
19
|
+
get_autocomplete_order(:method, {})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#get_autocomplete_items" do
|
25
|
+
setup do
|
26
|
+
@model = mock(Object)
|
27
|
+
|
28
|
+
@parameters = {
|
29
|
+
:model => @model,
|
30
|
+
:method => :field,
|
31
|
+
:term => 'query',
|
32
|
+
:options => {:full => false}
|
33
|
+
}
|
34
|
+
mock(self).get_autocomplete_limit(anything) { 10 }
|
35
|
+
mock(self).get_autocomplete_order(anything, anything) { [[:order, :asc]] }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'not a full search' do
|
39
|
+
should 'do stuff' do
|
40
|
+
mock(@model).where({:field=>/^query.*/i}).mock!.limit(10).
|
41
|
+
mock!.sort([[:order, :asc]])
|
42
|
+
|
43
|
+
get_autocomplete_items(@parameters)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'full search' do
|
48
|
+
should 'return a full search query' do
|
49
|
+
@parameters[:options] = {:full => true}
|
50
|
+
|
51
|
+
mock(@model).where({:field => /.*query.*/i}).mock!.limit(10).
|
52
|
+
mock!.sort([[:order, :asc]])
|
53
|
+
|
54
|
+
get_autocomplete_items(@parameters)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
module Orm
|
5
|
+
class MongoidTest < Test::Unit::TestCase
|
6
|
+
include Rails3JQueryAutocomplete::Orm::Mongoid
|
7
|
+
|
8
|
+
context "#get_autocomplete_order" do
|
9
|
+
context "order is specified" do
|
10
|
+
should 'returns the parametrized order for Mongoid' do
|
11
|
+
assert_equal [[:field, :asc], [:state, :desc]],
|
12
|
+
get_autocomplete_order(:method, :order => 'field ASC, state DESC')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'order is not specified' do
|
17
|
+
should 'return the method ordered ASC by default' do
|
18
|
+
assert_equal [[:method, :asc]],
|
19
|
+
get_autocomplete_order(:method, {})
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "#get_autocomplete_items" do
|
25
|
+
setup do
|
26
|
+
@model = mock(Object)
|
27
|
+
|
28
|
+
@parameters = {
|
29
|
+
:model => @model,
|
30
|
+
:method => :field,
|
31
|
+
:term => 'query',
|
32
|
+
:options => {:full => false}
|
33
|
+
}
|
34
|
+
mock(self).get_autocomplete_limit(anything) { 10 }
|
35
|
+
mock(self).get_autocomplete_order(anything, anything) { [[:order, :asc]] }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'not a full search' do
|
39
|
+
should 'do stuff' do
|
40
|
+
mock(@model).where({:field=>/^query.*/i}).mock!.limit(10).
|
41
|
+
mock!.order_by([[:order, :asc]])
|
42
|
+
|
43
|
+
get_autocomplete_items(@parameters)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'full search' do
|
48
|
+
should 'return a full search query' do
|
49
|
+
@parameters[:options] = {:full => true}
|
50
|
+
|
51
|
+
mock(@model).where({:field=>/.*query.*/i}).mock!.limit(10).
|
52
|
+
mock!.order_by([[:order, :asc]])
|
53
|
+
|
54
|
+
get_autocomplete_items(@parameters)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
class Rails3JQueryAutocompleteTest < ActionController::TestCase
|
5
|
+
ActorsController = Class.new(ActionController::Base)
|
6
|
+
ActorsController.autocomplete(:movie, :name)
|
7
|
+
|
8
|
+
class ::Movie ; end
|
9
|
+
|
10
|
+
context '#autocomplete_object_method' do
|
11
|
+
setup do
|
12
|
+
@controller = ActorsController.new
|
13
|
+
@items = {}
|
14
|
+
@options = { :display_value => :name }
|
15
|
+
end
|
16
|
+
|
17
|
+
should 'respond to the action' do
|
18
|
+
assert_respond_to @controller, :autocomplete_movie_name
|
19
|
+
end
|
20
|
+
|
21
|
+
should 'render the JSON items' do
|
22
|
+
mock(@controller).get_autocomplete_items({
|
23
|
+
:model => Movie, :method => :name, :options => @options, :term => "query"
|
24
|
+
}) { @items }
|
25
|
+
|
26
|
+
mock(@controller).json_for_autocomplete(@items, :name, nil)
|
27
|
+
get :autocomplete_movie_name, :term => 'query'
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'no term is specified' do
|
31
|
+
should "render an empty hash" do
|
32
|
+
mock(@controller).json_for_autocomplete({}, :name, nil)
|
33
|
+
get :autocomplete_movie_name
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -10,6 +10,7 @@ require 'rails/all'
|
|
10
10
|
require 'mongoid'
|
11
11
|
require 'mongo_mapper'
|
12
12
|
require 'shoulda'
|
13
|
+
require 'rr'
|
13
14
|
require 'rails/test_help'
|
14
15
|
require 'rails3-jquery-autocomplete'
|
15
16
|
|
@@ -24,5 +25,7 @@ end
|
|
24
25
|
|
25
26
|
ActionController::Base.send :include, Rails3JQueryAutocomplete::Application.routes.url_helpers
|
26
27
|
|
27
|
-
|
28
|
-
|
28
|
+
class Test::Unit::TestCase
|
29
|
+
include RR::Adapters::TestUnit
|
30
|
+
end
|
31
|
+
|