rails-jquery-autocomplete 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +3 -0
- data/README.md +9 -4
- data/lib/assets/javascripts/autocomplete-rails-uncompressed.js +9 -5
- data/lib/assets/javascripts/autocomplete-rails.js +1 -1
- data/lib/rails-jquery-autocomplete/autocomplete.rb +1 -1
- data/lib/rails-jquery-autocomplete/orm/active_record.rb +7 -4
- data/lib/rails-jquery-autocomplete/orm/mongo_mapper.rb +3 -1
- data/lib/rails-jquery-autocomplete/orm/mongoid.rb +3 -1
- data/lib/rails-jquery-autocomplete/version.rb +1 -1
- data/test/lib/rails-jquery-autocomplete/autocomplete_test.rb +11 -0
- data/test/lib/rails-jquery-autocomplete/orm/active_record_test.rb +4 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e21bc924451dba3cd1ccc4a3da6ec66e3b9eddacd534cd6a32a3fe6909472bad
|
4
|
+
data.tar.gz: 4de8ba96113b9358f3f9da751e0c5d834e62cc894d38de8be686f4e4696f9839
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97dbdb5a5f5c046354bcae02e454dc02f3edee15df554f4868e8f18591951443289182d15cd599574f1bf6ed5b6385c3ca18028a751eb0e38bf5f8fc5ee58ba7
|
7
|
+
data.tar.gz: 38e820dcf8ac26a62bfd02d1ac2ad528508f17af473b181c8dbf4a6336e9484be9a8619b164eccad5a3602300480278d4556c66665c47bb640c72b9bffd6bd2d
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
|
12
12
|
An easy way to use jQuery's autocomplete with Rails.
|
13
13
|
|
14
|
-
Supports both ActiveRecord, [mongoid](
|
14
|
+
Supports both ActiveRecord, [mongoid](https://github.com/mongodb/mongoid), and [MongoMapper](https://github.com/jnunemaker/mongomapper).
|
15
15
|
|
16
16
|
Works with [Formtastic](http://github.com/justinfrench/formtastic)
|
17
17
|
and [SimpleForm](https://github.com/plataformatec/simple_form)
|
@@ -80,7 +80,7 @@ Just add it to your app/assets/javascripts/application.js file
|
|
80
80
|
|
81
81
|
//= require jquery
|
82
82
|
//= require jquery_ujs
|
83
|
-
//= require jquery-ui/autocomplete
|
83
|
+
//= require jquery-ui/widgets/autocomplete
|
84
84
|
//= require autocomplete-rails
|
85
85
|
|
86
86
|
## Usage
|
@@ -95,7 +95,7 @@ A live demo can be seen
|
|
95
95
|
|
96
96
|
As a new developer, I had some issues getting this to work by following the documentation. However after trying some things and reading [Yoni Weisbrod](http://www.yoniweisbrod.com/autocomplete-magic-with-rails/)'s blog post, I was able to make the autocomplete work and implement a few useful features.
|
97
97
|
|
98
|
-
##Some implemented features
|
98
|
+
## Some implemented features
|
99
99
|
|
100
100
|
1. The css has been changed such that the results show up better against the box of suggestions. See `<app/assets/stylesheets/food.scss>` for details. I obtained this from a gist(forgive me I don't remember who the author is at the moment, please contact me if you do and I'll give credit). Upon mouseover/arrowkey presses, the selection will be highlighted.
|
101
101
|
|
@@ -149,7 +149,7 @@ The following terms would match the query 'un':
|
|
149
149
|
|
150
150
|
#### :full => false (default behavior)
|
151
151
|
|
152
|
-
Only the following terms
|
152
|
+
Only the following terms would match the query 'un':
|
153
153
|
|
154
154
|
* Unacceptable
|
155
155
|
|
@@ -209,6 +209,11 @@ e.g `:scopes => [:scope1, :scope2]`
|
|
209
209
|
By default autocomplete uses method name as column name. Now it can be specified using column_name options
|
210
210
|
`:column_name => 'name'`
|
211
211
|
|
212
|
+
#### :case_sensitive
|
213
|
+
|
214
|
+
Normally autocomplete performs a case insensitive search. In cases where this is not desirable, or causes too high
|
215
|
+
a performance penalty, search can be made case sensitive by specifying `:case_sensitive => true`
|
216
|
+
|
212
217
|
#### json encoder
|
213
218
|
|
214
219
|
Autocomplete uses Yajl as JSON encoder/decoder, but you can specify your own
|
@@ -17,14 +17,17 @@
|
|
17
17
|
(function(jQuery)
|
18
18
|
{
|
19
19
|
var self = null;
|
20
|
-
jQuery.fn.railsAutocomplete = function() {
|
20
|
+
jQuery.fn.railsAutocomplete = function(selector) {
|
21
21
|
var handler = function() {
|
22
22
|
if (!this.railsAutoCompleter) {
|
23
23
|
this.railsAutoCompleter = new jQuery.railsAutocomplete(this);
|
24
24
|
}
|
25
25
|
};
|
26
26
|
if (jQuery.fn.on !== undefined) {
|
27
|
-
|
27
|
+
if (!selector) {
|
28
|
+
return;
|
29
|
+
}
|
30
|
+
return jQuery(document).on('focus',selector,handler);
|
28
31
|
}
|
29
32
|
else {
|
30
33
|
return this.live('focus',handler);
|
@@ -48,7 +51,7 @@
|
|
48
51
|
jQuery.railsAutocomplete.fn.extend({
|
49
52
|
init: function(e) {
|
50
53
|
e.delimiter = jQuery(e).attr('data-delimiter') || null;
|
51
|
-
e.min_length = jQuery(e).attr('min-length') || 2;
|
54
|
+
e.min_length = jQuery(e).attr('data-min-length') || jQuery(e).attr('min-length') || 2;
|
52
55
|
e.append_to = jQuery(e).attr('data-append-to') || null;
|
53
56
|
e.autoFocus = jQuery(e).attr('data-auto-focus') || false;
|
54
57
|
function split( val ) {
|
@@ -79,7 +82,7 @@
|
|
79
82
|
options[key] = attrVal ? attrVal : value;
|
80
83
|
}
|
81
84
|
});
|
82
|
-
if(arguments[0].length == 0 && jQuery.inArray(options.showNoMatches, [true, 'true'])) {
|
85
|
+
if(arguments[0].length == 0 && jQuery.inArray(options.showNoMatches, [true, 'true']) >= 0) {
|
83
86
|
arguments[0] = [];
|
84
87
|
arguments[0][0] = { id: "", label: options.noMatchesLabel };
|
85
88
|
}
|
@@ -184,10 +187,11 @@
|
|
184
187
|
return false;
|
185
188
|
}
|
186
189
|
});
|
190
|
+
jQuery(e).trigger('railsAutocomplete.init');
|
187
191
|
}
|
188
192
|
});
|
189
193
|
|
190
194
|
jQuery(document).ready(function(){
|
191
|
-
jQuery('input[data-autocomplete]').railsAutocomplete();
|
195
|
+
jQuery('input[data-autocomplete]').railsAutocomplete('input[data-autocomplete]');
|
192
196
|
});
|
193
197
|
})(jQuery);
|
@@ -1 +1 @@
|
|
1
|
-
!function(t){t.fn.railsAutocomplete=function(){var
|
1
|
+
!function(t){t.fn.railsAutocomplete=function(e){var a=function(){this.railsAutoCompleter||(this.railsAutoCompleter=new t.railsAutocomplete(this))};if(void 0!==t.fn.on){if(!e)return;return t(document).on("focus",e,a)}return this.live("focus",a)},t.railsAutocomplete=function(t){var e=t;this.init(e)},t.railsAutocomplete.options={showNoMatches:!0,noMatchesLabel:"no existing match"},t.railsAutocomplete.fn=t.railsAutocomplete.prototype={railsAutocomplete:"0.0.1"},t.railsAutocomplete.fn.extend=t.railsAutocomplete.extend=t.extend,t.railsAutocomplete.fn.extend({init:function(e){function a(t){return t.split(e.delimiter)}function i(t){return a(t).pop().replace(/^\s+/,"")}e.delimiter=t(e).attr("data-delimiter")||null,e.min_length=t(e).attr("data-min-length")||t(e).attr("min-length")||2,e.append_to=t(e).attr("data-append-to")||null,e.autoFocus=t(e).attr("data-auto-focus")||!1,t(e).autocomplete({appendTo:e.append_to,autoFocus:e.autoFocus,delay:t(e).attr("delay")||0,source:function(a,r){var n=this.element[0],o={term:i(a.term)};t(e).attr("data-autocomplete-fields")&&t.each(t.parseJSON(t(e).attr("data-autocomplete-fields")),function(e,a){o[e]=t(a).val()}),t.getJSON(t(e).attr("data-autocomplete"),o,function(){var a={};t.extend(a,t.railsAutocomplete.options),t.each(a,function(i,r){if(a.hasOwnProperty(i)){var n=t(e).attr("data-"+i);a[i]=n?n:r}}),0==arguments[0].length&&t.inArray(a.showNoMatches,[!0,"true"])>=0&&(arguments[0]=[],arguments[0][0]={id:"",label:a.noMatchesLabel}),t(arguments[0]).each(function(a,i){var r={};r[i.id]=i,t(e).data(r)}),r.apply(null,arguments),t(n).trigger("railsAutocomplete.source",arguments)})},change:function(e,a){if(t(this).is("[data-id-element]")&&""!==t(t(this).attr("data-id-element")).val()&&(t(t(this).attr("data-id-element")).val(a.item?a.item.id:"").trigger("change"),t(this).attr("data-update-elements"))){var i=t.parseJSON(t(this).attr("data-update-elements")),r=a.item?t(this).data(a.item.id.toString()):{};if(i&&""===t(i.id).val())return;for(var n in i){var o=t(i[n]);o.is(":checkbox")?null!=r[n]&&o.prop("checked",r[n]):o.val(a.item?r[n]:"").trigger("change")}}},search:function(){var t=i(this.value);return t.length<e.min_length?!1:void 0},focus:function(){return!1},select:function(i,r){if(r.item.value=r.item.value.toString(),-1!=r.item.value.toLowerCase().indexOf("no match")||-1!=r.item.value.toLowerCase().indexOf("too many results"))return t(this).trigger("railsAutocomplete.noMatch",r),!1;var n=a(this.value);if(n.pop(),n.push(r.item.value),null!=e.delimiter)n.push(""),this.value=n.join(e.delimiter);else if(this.value=n.join(""),t(this).attr("data-id-element")&&t(t(this).attr("data-id-element")).val(r.item.id).trigger("change"),t(this).attr("data-update-elements")){var o=r.item,l=-1!=r.item.value.indexOf("Create New")?!0:!1,u=t.parseJSON(t(this).attr("data-update-elements"));for(var s in u)"checkbox"===t(u[s]).attr("type")?o[s]===!0||1===o[s]?t(u[s]).attr("checked","checked"):t(u[s]).removeAttr("checked"):l&&o[s]&&-1==o[s].indexOf("Create New")||!l?t(u[s]).val(o[s]).trigger("change"):t(u[s]).val("").trigger("change")}var c=this.value;return t(this).bind("keyup.clearId",function(){t.trim(t(this).val())!=t.trim(c)&&(t(t(this).attr("data-id-element")).val("").trigger("change"),t(this).unbind("keyup.clearId"))}),t(e).trigger("railsAutocomplete.select",r),!1}}),t(e).trigger("railsAutocomplete.init")}}),t(document).ready(function(){t("input[data-autocomplete]").railsAutocomplete("input[data-autocomplete]")})}(jQuery);
|
@@ -98,7 +98,7 @@ module RailsJQueryAutocomplete
|
|
98
98
|
#
|
99
99
|
def json_for_autocomplete(items, method, extra_data=[])
|
100
100
|
items = items.collect do |item|
|
101
|
-
hash = {"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)}
|
101
|
+
hash = HashWithIndifferentAccess.new({"id" => item.id.to_s, "label" => item.send(method), "value" => item.send(method)})
|
102
102
|
extra_data.each do |datum|
|
103
103
|
hash[datum] = item.send(datum)
|
104
104
|
end if extra_data
|
@@ -50,13 +50,16 @@ module RailsJQueryAutocomplete
|
|
50
50
|
def get_autocomplete_where_clause(model, term, method, options)
|
51
51
|
table_name = model.table_name
|
52
52
|
is_full_search = options[:full]
|
53
|
-
|
53
|
+
is_case_sensitive_search = options[:case_sensitive]
|
54
|
+
like_clause = (postgres?(model) && !is_case_sensitive_search ? 'ILIKE' : 'LIKE')
|
55
|
+
column_transform = is_case_sensitive_search ? '' : 'LOWER'
|
56
|
+
term = "#{(is_full_search ? '%' : '')}#{term.gsub(/([_%\\])/, '\\\\\1')}%"
|
54
57
|
if options[:hstore]
|
55
|
-
["
|
58
|
+
["#{column_transform}(#{table_name}.#{method} -> '#{options[:hstore][:key]}') LIKE #{column_transform}(?)", term]
|
56
59
|
elsif sqlite?
|
57
|
-
["
|
60
|
+
["#{column_transform}(#{method}) #{like_clause} #{column_transform}(?)", term]
|
58
61
|
else
|
59
|
-
["
|
62
|
+
["#{column_transform}(#{table_name}.#{method}) #{like_clause} #{column_transform}(?)", term]
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
@@ -18,12 +18,14 @@ module RailsJQueryAutocomplete
|
|
18
18
|
method = parameters[:method]
|
19
19
|
options = parameters[:options]
|
20
20
|
is_full_search = options[:full]
|
21
|
+
is_case_sensitive_search = options[:case_sensitive]
|
21
22
|
term = parameters[:term]
|
22
23
|
limit = get_autocomplete_limit(options)
|
23
24
|
order = mongo_mapper_get_autocomplete_order(method, options)
|
24
25
|
|
25
26
|
search = (is_full_search ? '.*' : '^') + term + '.*'
|
26
|
-
|
27
|
+
search = Regexp.new(search, !is_case_sensitive_search)
|
28
|
+
items = model.where(method.to_sym => search).limit(limit).sort(order)
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
@@ -18,6 +18,7 @@ module RailsJQueryAutocomplete
|
|
18
18
|
method = parameters[:method]
|
19
19
|
options = parameters[:options]
|
20
20
|
is_full_search = options[:full]
|
21
|
+
is_case_sensitive_search = options[:case_sensitive]
|
21
22
|
term = parameters[:term]
|
22
23
|
limit = get_autocomplete_limit(options)
|
23
24
|
order = mongoid_get_autocomplete_order(method, options)
|
@@ -27,7 +28,8 @@ module RailsJQueryAutocomplete
|
|
27
28
|
else
|
28
29
|
search = '^' + Regexp.escape(term)
|
29
30
|
end
|
30
|
-
|
31
|
+
search = Regexp.new(search, !is_case_sensitive_search)
|
32
|
+
items = model.where(method.to_sym => search).limit(limit).order_by(order)
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
@@ -62,6 +62,17 @@ module RailsJQueryAutocomplete
|
|
62
62
|
assert_equal response["label"], "Object Name"
|
63
63
|
end
|
64
64
|
|
65
|
+
should 'return an instance of HashWithIndifferentAccess' do
|
66
|
+
item = mock(Object)
|
67
|
+
mock(item).send(:name).times(2) { 'Object Name' }
|
68
|
+
mock(item).id { 1 }
|
69
|
+
items = [item]
|
70
|
+
response = self.json_for_autocomplete(items, :name).first
|
71
|
+
assert_equal response.is_a?(HashWithIndifferentAccess), true
|
72
|
+
assert_equal response["id"], "1"
|
73
|
+
assert_equal response[:id], "1"
|
74
|
+
end
|
75
|
+
|
65
76
|
context 'with extra data' do
|
66
77
|
should 'add that extra data to result' do
|
67
78
|
item = mock(Object)
|
@@ -130,14 +130,14 @@ module RailsJQueryAutocomplete
|
|
130
130
|
context 'Not Postgres' do
|
131
131
|
should 'return options for where' do
|
132
132
|
mock(self).postgres?(@model) { false }
|
133
|
-
assert_equal ["LOWER(table_name.method) LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
133
|
+
assert_equal ["LOWER(table_name.method) LIKE LOWER(?)", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
134
134
|
end
|
135
135
|
end
|
136
136
|
|
137
137
|
context 'Postgres' do
|
138
138
|
should 'return options for where with ILIKE' do
|
139
139
|
mock(self).postgres?(@model) { true }
|
140
|
-
assert_equal ["LOWER(table_name.method) ILIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
140
|
+
assert_equal ["LOWER(table_name.method) ILIKE LOWER(?)", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
@@ -146,7 +146,7 @@ module RailsJQueryAutocomplete
|
|
146
146
|
mock(self).postgres?(@model) { true }
|
147
147
|
@options[:hstore] = {method: :hsmethod, key: :hskey}
|
148
148
|
@method = :hsmethod
|
149
|
-
assert_equal ["LOWER(table_name.hsmethod -> 'hskey') LIKE ?", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
149
|
+
assert_equal ["LOWER(table_name.hsmethod -> 'hskey') LIKE LOWER(?)", "query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -154,7 +154,7 @@ module RailsJQueryAutocomplete
|
|
154
154
|
should 'return options for where with the term sourrounded by %%' do
|
155
155
|
mock(self).postgres?(@model) { false }
|
156
156
|
@options[:full] = true
|
157
|
-
assert_equal ["LOWER(table_name.method) LIKE ?", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
157
|
+
assert_equal ["LOWER(table_name.method) LIKE LOWER(?)", "%query%"], get_autocomplete_where_clause(@model, @term, @method, @options)
|
158
158
|
end
|
159
159
|
end
|
160
160
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-jquery-autocomplete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Padilla
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2018-07-12 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -253,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
253
253
|
version: '0'
|
254
254
|
requirements: []
|
255
255
|
rubyforge_project:
|
256
|
-
rubygems_version: 2.
|
256
|
+
rubygems_version: 2.7.7
|
257
257
|
signing_key:
|
258
258
|
specification_version: 4
|
259
259
|
summary: Use jQuery's autocomplete plugin with Rails 4+.
|