rails3-jquery-autocomplete 0.7.2 → 0.7.3
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/.gitignore +1 -0
- data/README.markdown +7 -2
- data/lib/rails3-jquery-autocomplete/helpers.rb +21 -6
- data/lib/rails3-jquery-autocomplete/version.rb +1 -1
- data/rails3-jquery-autocomplete.gemspec +1 -0
- data/test/implementations_test.rb +6 -0
- data/test/lib/rails3-jquery-autocomplete/helpers_test.rb +16 -0
- data/test/support/mongo_mapper.rb +39 -0
- data/test/test_helper.rb +1 -0
- metadata +27 -8
data/.gitignore
CHANGED
data/README.markdown
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
An easy way to use jQuery's autocomplete with Rails 3.
|
4
4
|
|
5
|
-
Supports both ActiveRecord
|
5
|
+
Supports both ActiveRecord, [mongoid](http://github.com/mongoid/mongoid), and [MongoMapper](https://github.com/jnunemaker/mongomapper).
|
6
6
|
|
7
7
|
Works with [Formtastic](http://github.com/justinfrench/formtastic)
|
8
8
|
and [SimpleForm](https://github.com/plataformatec/simple_form)
|
@@ -17,6 +17,10 @@ on how to use this gem with ActiveRecord [here](http://github.com/crowdint/rails
|
|
17
17
|
You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid)
|
18
18
|
on how to use this gem with MongoID [here](http://github.com/crowdint/rails3-jquery-autocomplete-app/tree/mongoid). (Same thing, different branch)
|
19
19
|
|
20
|
+
## MongoMapper
|
21
|
+
|
22
|
+
TODO
|
23
|
+
|
20
24
|
## Before you start
|
21
25
|
|
22
26
|
Make sure your project is using jQuery-ui with the autocomplete widget
|
@@ -203,7 +207,7 @@ A javascript event named *railsAutocomplete.select* is fired on the input field
|
|
203
207
|
If you are using [Formtastic](http://github.com/justinfrench/formtastic), you automatically get the *autocompleted_input* helper on *semantic_form_for*:
|
204
208
|
|
205
209
|
semantic_form_for @product do |f|
|
206
|
-
f.autocompleted_input :brand_name, :url =>
|
210
|
+
f.autocompleted_input :brand_name, :url => autocomplete_brand_name_products_path
|
207
211
|
end
|
208
212
|
|
209
213
|
The only difference with the original helper is that you must specify the autocomplete url using the *:url* option.
|
@@ -306,6 +310,7 @@ integration folder:
|
|
306
310
|
|
307
311
|
# Changelog
|
308
312
|
|
313
|
+
* 0.7.3 MongoMapper
|
309
314
|
* 0.7.2 Steak helper
|
310
315
|
* 0.7.1 Fixed joined scopes (Issue #43)
|
311
316
|
* 0.7.0 Scopes
|
@@ -8,7 +8,7 @@ module Rails3JQueryAutocomplete
|
|
8
8
|
# Can be overriden to show whatever you like
|
9
9
|
# Hash also includes a key/value pair for each method in extra_data
|
10
10
|
#
|
11
|
-
def json_for_autocomplete(items, method, extra_data)
|
11
|
+
def json_for_autocomplete(items, method, extra_data=nil)
|
12
12
|
items.collect do |item|
|
13
13
|
hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
|
14
14
|
extra_data.each do |datum|
|
@@ -35,6 +35,8 @@ module Rails3JQueryAutocomplete
|
|
35
35
|
:activerecord
|
36
36
|
elsif ancestors_ary.include?('Mongoid::Document')
|
37
37
|
:mongoid
|
38
|
+
elsif ancestors_ary.include?('MongoMapper::Document')
|
39
|
+
:mongo_mapper
|
38
40
|
else
|
39
41
|
raise NotImplementedError
|
40
42
|
end
|
@@ -47,7 +49,7 @@ module Rails3JQueryAutocomplete
|
|
47
49
|
end
|
48
50
|
|
49
51
|
# Returns the order parameter to be used in the query created by get_items
|
50
|
-
def get_autocomplete_order(implementation, method, options)
|
52
|
+
def get_autocomplete_order(implementation, method, options, model=nil)
|
51
53
|
order = options[:order]
|
52
54
|
|
53
55
|
case implementation
|
@@ -60,8 +62,18 @@ module Rails3JQueryAutocomplete
|
|
60
62
|
else
|
61
63
|
[[method.to_sym, :asc]]
|
62
64
|
end
|
65
|
+
when :mongo_mapper then
|
66
|
+
if order
|
67
|
+
order.split(',').collect do |fields|
|
68
|
+
sfields = fields.split
|
69
|
+
[sfields[0].downcase.to_sym, sfields[1].downcase.to_sym]
|
70
|
+
end
|
71
|
+
else
|
72
|
+
[[method.to_sym, :asc]]
|
73
|
+
end
|
63
74
|
when :activerecord then
|
64
|
-
|
75
|
+
table_prefix = model ? "#{model.table_name}." : ""
|
76
|
+
order || "#{table_prefix}#{method} ASC"
|
65
77
|
end
|
66
78
|
end
|
67
79
|
|
@@ -98,11 +110,11 @@ module Rails3JQueryAutocomplete
|
|
98
110
|
scopes = Array(options[:scopes])
|
99
111
|
limit = get_autocomplete_limit(options)
|
100
112
|
implementation = get_implementation(model)
|
101
|
-
order = get_autocomplete_order(implementation, method, options)
|
113
|
+
order = get_autocomplete_order(implementation, method, options, model)
|
102
114
|
|
103
115
|
like_clause = (defined?(PGconn) ? 'ILIKE' : 'LIKE')
|
104
116
|
|
105
|
-
items = model.scoped
|
117
|
+
implementation == :mongo_mapper ? (items = model.query) : items = model.scoped
|
106
118
|
|
107
119
|
scopes.each { |scope| items = items.send(scope) } unless scopes.empty?
|
108
120
|
|
@@ -110,9 +122,12 @@ module Rails3JQueryAutocomplete
|
|
110
122
|
when :mongoid
|
111
123
|
search = (is_full_search ? '.*' : '^') + term + '.*'
|
112
124
|
items = model.where(method.to_sym => /#{search}/i).limit(limit).order_by(order)
|
125
|
+
when :mongo_mapper
|
126
|
+
search = (is_full_search ? '.*' : '^') + term + '.*'
|
127
|
+
items = model.where(method.to_sym => /#{search}/i).limit(limit).sort(order)
|
113
128
|
when :activerecord
|
114
129
|
table_name = model.table_name
|
115
|
-
items = items.select(["#{table_name}.
|
130
|
+
items = items.select(["#{table_name}.#{model.primary_key}", "#{table_name}.#{method}"] + (options[:extra_data].blank? ? [] : options[:extra_data])) unless options[:full_model]
|
116
131
|
items = items.where(["LOWER(#{table_name}.#{method}) #{like_clause} ?", "#{(is_full_search ? '%' : '')}#{term.downcase}%"]) \
|
117
132
|
.limit(limit).order(order)
|
118
133
|
end
|
@@ -17,6 +17,7 @@ Gem::Specification.new do |s|
|
|
17
17
|
|
18
18
|
s.add_development_dependency('sqlite3-ruby')
|
19
19
|
s.add_development_dependency('mongoid', '>= 2.0.0')
|
20
|
+
s.add_development_dependency('mongo_mapper', '>= 0.9')
|
20
21
|
s.add_development_dependency('bson_ext', '~>1.3.0')
|
21
22
|
s.add_development_dependency('shoulda', '~>2.11.1')
|
22
23
|
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
require 'test_controller'
|
3
3
|
require 'support/mongoid'
|
4
|
+
require 'support/mongo_mapper'
|
4
5
|
require 'support/active_record'
|
5
6
|
|
6
7
|
class ActiveRecordControllerTest < ActionController::TestCase
|
@@ -33,3 +34,8 @@ class MonogidControllerTest < ActionController::TestCase
|
|
33
34
|
include Rails3JQueryAutocomplete::TestCase::Mongoid
|
34
35
|
include Rails3JQueryAutocomplete::TestCase
|
35
36
|
end
|
37
|
+
|
38
|
+
class MongoMapperControllerTest < ActionController::TestCase
|
39
|
+
include Rails3JQueryAutocomplete::TestCase::MongoMapper
|
40
|
+
include Rails3JQueryAutocomplete::TestCase
|
41
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Rails3JQueryAutocomplete
|
4
|
+
module Test
|
5
|
+
class HelpersTest < ::Test::Unit::TestCase
|
6
|
+
include Rails3JQueryAutocomplete::Helpers
|
7
|
+
|
8
|
+
context 'passing a query result' do
|
9
|
+
should 'parse items to JSON' do
|
10
|
+
response = self.json_for_autocomplete([], :name)
|
11
|
+
assert_not_nil(response)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
module TestCase
|
3
|
+
module MongoMapper
|
4
|
+
def setup
|
5
|
+
::MongoMapper.connection = Mongo::Connection.new('localhost', 27017)
|
6
|
+
::MongoMapper.database = "rails3_jquery_autocomplete_test"
|
7
|
+
|
8
|
+
create_models
|
9
|
+
|
10
|
+
@controller = ActorsController.new
|
11
|
+
@movie1 = @movie_class.create(:name => 'Alpha')
|
12
|
+
@movie2 = @movie_class.create(:name => 'Alspha')
|
13
|
+
@movie3 = @movie_class.create(:name => 'Alzpha')
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
destroy_models
|
18
|
+
::MongoMapper.database.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
def create_models
|
23
|
+
@movie_class = Object.const_set(:Movie, Class.new)
|
24
|
+
@movie_class.send(:include, ::MongoMapper::Document)
|
25
|
+
@movie_class.key(:name, :class => String)
|
26
|
+
@movie_class.class_eval do
|
27
|
+
def display_name
|
28
|
+
"Movie: #{name}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def destroy_models
|
34
|
+
Object.send(:remove_const, :Movie)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/test/test_helper.rb
CHANGED
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: 5
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 7
|
9
|
-
-
|
10
|
-
version: 0.7.
|
9
|
+
- 3
|
10
|
+
version: 0.7.3
|
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: 2011-05-
|
18
|
+
date: 2011-05-13 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -79,9 +79,24 @@ dependencies:
|
|
79
79
|
type: :development
|
80
80
|
version_requirements: *id004
|
81
81
|
- !ruby/object:Gem::Dependency
|
82
|
-
name:
|
82
|
+
name: mongo_mapper
|
83
83
|
prerelease: false
|
84
84
|
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 25
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
- 9
|
93
|
+
version: "0.9"
|
94
|
+
type: :development
|
95
|
+
version_requirements: *id005
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: bson_ext
|
98
|
+
prerelease: false
|
99
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
85
100
|
none: false
|
86
101
|
requirements:
|
87
102
|
- - ~>
|
@@ -93,11 +108,11 @@ dependencies:
|
|
93
108
|
- 0
|
94
109
|
version: 1.3.0
|
95
110
|
type: :development
|
96
|
-
version_requirements: *
|
111
|
+
version_requirements: *id006
|
97
112
|
- !ruby/object:Gem::Dependency
|
98
113
|
name: shoulda
|
99
114
|
prerelease: false
|
100
|
-
requirement: &
|
115
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
101
116
|
none: false
|
102
117
|
requirements:
|
103
118
|
- - ~>
|
@@ -109,7 +124,7 @@ dependencies:
|
|
109
124
|
- 1
|
110
125
|
version: 2.11.1
|
111
126
|
type: :development
|
112
|
-
version_requirements: *
|
127
|
+
version_requirements: *id007
|
113
128
|
description: Use jQuery's autocomplete plugin with Rails 3.
|
114
129
|
email: david.padilla@crowdint.com
|
115
130
|
executables: []
|
@@ -230,7 +245,9 @@ files:
|
|
230
245
|
- test/form_helper_test.rb
|
231
246
|
- test/generators/generator_test.rb
|
232
247
|
- test/implementations_test.rb
|
248
|
+
- test/lib/rails3-jquery-autocomplete/helpers_test.rb
|
233
249
|
- test/support/active_record.rb
|
250
|
+
- test/support/mongo_mapper.rb
|
234
251
|
- test/support/mongoid.rb
|
235
252
|
- test/test_controller.rb
|
236
253
|
- test/test_helper.rb
|
@@ -272,7 +289,9 @@ test_files:
|
|
272
289
|
- test/form_helper_test.rb
|
273
290
|
- test/generators/generator_test.rb
|
274
291
|
- test/implementations_test.rb
|
292
|
+
- test/lib/rails3-jquery-autocomplete/helpers_test.rb
|
275
293
|
- test/support/active_record.rb
|
294
|
+
- test/support/mongo_mapper.rb
|
276
295
|
- test/support/mongoid.rb
|
277
296
|
- test/test_controller.rb
|
278
297
|
- test/test_helper.rb
|