rails3-jquery-autocomplete 0.7.5 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +1 -0
- data/README.markdown +10 -3
- data/Rakefile +9 -1
- data/lib/generators/autocomplete/install_generator.rb +14 -0
- data/lib/generators/autocomplete/uncompressed_generator.rb +14 -0
- data/lib/generators/templates/autocomplete-rails-uncompressed.js +113 -0
- data/lib/generators/templates/autocomplete-rails.js +1 -98
- data/lib/rails3-jquery-autocomplete/version.rb +1 -1
- data/rails3-jquery-autocomplete.gemspec +1 -0
- data/test/generators/autocomplete/install_generator_test.rb +25 -0
- data/test/generators/autocomplete/uncompressed_generator_test.rb +25 -0
- metadata +26 -8
- data/lib/generators/autocomplete_generator.rb +0 -12
- data/test/generators/generator_test.rb +0 -23
data/CHANGELOG.md
CHANGED
data/README.markdown
CHANGED
@@ -23,7 +23,7 @@ TODO
|
|
23
23
|
|
24
24
|
## Before you start
|
25
25
|
|
26
|
-
Make sure your project is using jQuery-
|
26
|
+
Make sure your project is using jQuery-UI and the autocomplete widget
|
27
27
|
before you continue.
|
28
28
|
|
29
29
|
You can find more info about that here:
|
@@ -46,7 +46,7 @@ Install it
|
|
46
46
|
|
47
47
|
Run the generator
|
48
48
|
|
49
|
-
rails generate autocomplete
|
49
|
+
rails generate autocomplete:install
|
50
50
|
|
51
51
|
And include autocomplete-rails.js on your layouts
|
52
52
|
|
@@ -56,10 +56,17 @@ And include autocomplete-rails.js on your layouts
|
|
56
56
|
|
57
57
|
If you are upgrading from a previous version, run the generator after installing to replace the javascript file.
|
58
58
|
|
59
|
-
rails generate autocomplete
|
59
|
+
rails generate autocomplete:install
|
60
60
|
|
61
61
|
I'd recommend you do this every time you update to make sure you have the latest JS file.
|
62
62
|
|
63
|
+
## Uncompressed Javascript file
|
64
|
+
|
65
|
+
If you want to make changes to the JS file, you can install the
|
66
|
+
uncompressed version by running:
|
67
|
+
|
68
|
+
rails generate autocomplete:uncompressed
|
69
|
+
|
63
70
|
## Usage
|
64
71
|
|
65
72
|
### Model Example
|
data/Rakefile
CHANGED
@@ -3,10 +3,18 @@ Bundler::GemHelper.install_tasks
|
|
3
3
|
|
4
4
|
require 'rake/testtask'
|
5
5
|
|
6
|
-
task :default => :test
|
6
|
+
task :default => [:uglify, :test]
|
7
7
|
|
8
8
|
Rake::TestTask.new(:test) do |test|
|
9
9
|
test.libs << 'lib' << 'test'
|
10
10
|
test.pattern = 'test/**/*_test.rb'
|
11
11
|
test.verbose = true
|
12
12
|
end
|
13
|
+
|
14
|
+
task :uglify do
|
15
|
+
require 'uglifier'
|
16
|
+
file_folder = "lib/generators/templates"
|
17
|
+
File.open("#{file_folder}/autocomplete-rails.js", "w") do |f|
|
18
|
+
f << Uglifier.compile(File.read("#{file_folder}/autocomplete-rails-uncompressed.js"))
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Autocomplete
|
4
|
+
class InstallGenerator < Rails::Generators::Base
|
5
|
+
def install
|
6
|
+
# Copy the unobtrusive JS file
|
7
|
+
copy_file('autocomplete-rails.js', 'public/javascripts/autocomplete-rails.js')
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
File.join(File.dirname(__FILE__), '..', 'templates')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Autocomplete
|
4
|
+
class UncompressedGenerator < Rails::Generators::Base
|
5
|
+
def install
|
6
|
+
# Copy the unobtrusive JS file
|
7
|
+
copy_file('autocomplete-rails-uncompressed.js', 'public/javascripts/autocomplete-rails.js')
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.source_root
|
11
|
+
File.join(File.dirname(__FILE__), '..', 'templates')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
/*
|
2
|
+
* Unobtrusive autocomplete
|
3
|
+
*
|
4
|
+
* To use it, you just have to include the HTML attribute autocomplete
|
5
|
+
* with the autocomplete URL as the value
|
6
|
+
*
|
7
|
+
* Example:
|
8
|
+
* <input type="text" data-autocomplete="/url/to/autocomplete">
|
9
|
+
*
|
10
|
+
* Optionally, you can use a jQuery selector to specify a field that can
|
11
|
+
* be updated with the element id whenever you find a matching value
|
12
|
+
*
|
13
|
+
* Example:
|
14
|
+
* <input type="text" data-autocomplete="/url/to/autocomplete" id_element="#id_field">
|
15
|
+
*/
|
16
|
+
|
17
|
+
$(document).ready(function(){
|
18
|
+
$('input[data-autocomplete]').railsAutocomplete();
|
19
|
+
});
|
20
|
+
|
21
|
+
(function(jQuery)
|
22
|
+
{
|
23
|
+
var self = null;
|
24
|
+
jQuery.fn.railsAutocomplete = function() {
|
25
|
+
return this.live('focus',function() {
|
26
|
+
if (!this.railsAutoCompleter) {
|
27
|
+
this.railsAutoCompleter = new jQuery.railsAutocomplete(this);
|
28
|
+
}
|
29
|
+
});
|
30
|
+
};
|
31
|
+
|
32
|
+
jQuery.railsAutocomplete = function (e) {
|
33
|
+
_e = e;
|
34
|
+
this.init(_e);
|
35
|
+
};
|
36
|
+
|
37
|
+
jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
|
38
|
+
railsAutocomplete: '0.0.1'
|
39
|
+
};
|
40
|
+
|
41
|
+
jQuery.railsAutocomplete.fn.extend = jQuery.railsAutocomplete.extend = jQuery.extend;
|
42
|
+
jQuery.railsAutocomplete.fn.extend({
|
43
|
+
init: function(e) {
|
44
|
+
e.delimiter = $(e).attr('data-delimiter') || null;
|
45
|
+
function split( val ) {
|
46
|
+
return val.split( e.delimiter );
|
47
|
+
}
|
48
|
+
function extractLast( term ) {
|
49
|
+
return split( term ).pop().replace(/^\s+/,"");
|
50
|
+
}
|
51
|
+
|
52
|
+
$(e).autocomplete({
|
53
|
+
source: function( request, response ) {
|
54
|
+
$.getJSON( $(e).attr('data-autocomplete'), {
|
55
|
+
term: extractLast( request.term )
|
56
|
+
}, function() {
|
57
|
+
$(arguments[0]).each(function(i, el) {
|
58
|
+
var obj = {};
|
59
|
+
obj[el.id] = el;
|
60
|
+
$(e).data(obj);
|
61
|
+
});
|
62
|
+
response.apply(null, arguments);
|
63
|
+
});
|
64
|
+
},
|
65
|
+
search: function() {
|
66
|
+
// custom minLength
|
67
|
+
var term = extractLast( this.value );
|
68
|
+
if ( term.length < 2 ) {
|
69
|
+
return false;
|
70
|
+
}
|
71
|
+
},
|
72
|
+
focus: function() {
|
73
|
+
// prevent value inserted on focus
|
74
|
+
return false;
|
75
|
+
},
|
76
|
+
select: function( event, ui ) {
|
77
|
+
var terms = split( this.value );
|
78
|
+
// remove the current input
|
79
|
+
terms.pop();
|
80
|
+
// add the selected item
|
81
|
+
terms.push( ui.item.value );
|
82
|
+
// add placeholder to get the comma-and-space at the end
|
83
|
+
if (e.delimiter != null) {
|
84
|
+
terms.push( "" );
|
85
|
+
this.value = terms.join( e.delimiter );
|
86
|
+
} else {
|
87
|
+
this.value = terms.join("");
|
88
|
+
if ($(this).attr('id_element')) {
|
89
|
+
$($(this).attr('id_element')).val(ui.item.id);
|
90
|
+
}
|
91
|
+
if ($(this).attr('data-update-elements')) {
|
92
|
+
var data = $(this).data(ui.item.id.toString());
|
93
|
+
var update_elements = $.parseJSON($(this).attr("data-update-elements"));
|
94
|
+
for (var key in update_elements) {
|
95
|
+
$(update_elements[key]).val(data[key]);
|
96
|
+
}
|
97
|
+
}
|
98
|
+
}
|
99
|
+
var remember_string = this.value;
|
100
|
+
$(this).bind('keyup.clearId', function(){
|
101
|
+
if($(this).val().trim() != remember_string.trim()){
|
102
|
+
$($(this).attr('id_element')).val("");
|
103
|
+
$(this).unbind('keyup.clearId');
|
104
|
+
}
|
105
|
+
});
|
106
|
+
$(this).trigger('railsAutocomplete.select');
|
107
|
+
|
108
|
+
return false;
|
109
|
+
}
|
110
|
+
});
|
111
|
+
}
|
112
|
+
});
|
113
|
+
})(jQuery);
|
@@ -13,101 +13,4 @@
|
|
13
13
|
* Example:
|
14
14
|
* <input type="text" data-autocomplete="/url/to/autocomplete" id_element="#id_field">
|
15
15
|
*/
|
16
|
-
|
17
|
-
$(document).ready(function(){
|
18
|
-
$('input[data-autocomplete]').railsAutocomplete();
|
19
|
-
});
|
20
|
-
|
21
|
-
(function(jQuery)
|
22
|
-
{
|
23
|
-
var self = null;
|
24
|
-
jQuery.fn.railsAutocomplete = function() {
|
25
|
-
return this.live('focus',function() {
|
26
|
-
if (!this.railsAutoCompleter) {
|
27
|
-
this.railsAutoCompleter = new jQuery.railsAutocomplete(this);
|
28
|
-
}
|
29
|
-
});
|
30
|
-
};
|
31
|
-
|
32
|
-
jQuery.railsAutocomplete = function (e) {
|
33
|
-
_e = e;
|
34
|
-
this.init(_e);
|
35
|
-
};
|
36
|
-
|
37
|
-
jQuery.railsAutocomplete.fn = jQuery.railsAutocomplete.prototype = {
|
38
|
-
railsAutocomplete: '0.0.1'
|
39
|
-
};
|
40
|
-
|
41
|
-
jQuery.railsAutocomplete.fn.extend = jQuery.railsAutocomplete.extend = jQuery.extend;
|
42
|
-
jQuery.railsAutocomplete.fn.extend({
|
43
|
-
init: function(e) {
|
44
|
-
e.delimiter = $(e).attr('data-delimiter') || null;
|
45
|
-
function split( val ) {
|
46
|
-
return val.split( e.delimiter );
|
47
|
-
}
|
48
|
-
function extractLast( term ) {
|
49
|
-
return split( term ).pop().replace(/^\s+/,"");
|
50
|
-
}
|
51
|
-
|
52
|
-
$(e).autocomplete({
|
53
|
-
source: function( request, response ) {
|
54
|
-
$.getJSON( $(e).attr('data-autocomplete'), {
|
55
|
-
term: extractLast( request.term )
|
56
|
-
}, function() {
|
57
|
-
$(arguments[0]).each(function(i, el) {
|
58
|
-
var obj = {};
|
59
|
-
obj[el.id] = el;
|
60
|
-
$(e).data(obj);
|
61
|
-
});
|
62
|
-
response.apply(null, arguments);
|
63
|
-
});
|
64
|
-
},
|
65
|
-
search: function() {
|
66
|
-
// custom minLength
|
67
|
-
var term = extractLast( this.value );
|
68
|
-
if ( term.length < 2 ) {
|
69
|
-
return false;
|
70
|
-
}
|
71
|
-
},
|
72
|
-
focus: function() {
|
73
|
-
// prevent value inserted on focus
|
74
|
-
return false;
|
75
|
-
},
|
76
|
-
select: function( event, ui ) {
|
77
|
-
var terms = split( this.value );
|
78
|
-
// remove the current input
|
79
|
-
terms.pop();
|
80
|
-
// add the selected item
|
81
|
-
terms.push( ui.item.value );
|
82
|
-
// add placeholder to get the comma-and-space at the end
|
83
|
-
if (e.delimiter != null) {
|
84
|
-
terms.push( "" );
|
85
|
-
this.value = terms.join( e.delimiter );
|
86
|
-
} else {
|
87
|
-
this.value = terms.join("");
|
88
|
-
if ($(this).attr('id_element')) {
|
89
|
-
$($(this).attr('id_element')).val(ui.item.id);
|
90
|
-
}
|
91
|
-
if ($(this).attr('data-update-elements')) {
|
92
|
-
var data = $(this).data(ui.item.id.toString());
|
93
|
-
var update_elements = $.parseJSON($(this).attr("data-update-elements"));
|
94
|
-
for (var key in update_elements) {
|
95
|
-
$(update_elements[key]).val(data[key]);
|
96
|
-
}
|
97
|
-
}
|
98
|
-
}
|
99
|
-
var remember_string = this.value;
|
100
|
-
$(this).bind('keyup.clearId', function(){
|
101
|
-
if($(this).val().trim() != remember_string.trim()){
|
102
|
-
$($(this).attr('id_element')).val("");
|
103
|
-
$(this).unbind('keyup.clearId');
|
104
|
-
}
|
105
|
-
});
|
106
|
-
$(this).trigger('railsAutocomplete.select');
|
107
|
-
|
108
|
-
return false;
|
109
|
-
}
|
110
|
-
});
|
111
|
-
}
|
112
|
-
});
|
113
|
-
})(jQuery);
|
16
|
+
$(document).ready(function(){$("input[data-autocomplete]").railsAutocomplete()}),function(a){var b=null;a.fn.railsAutocomplete=function(){return this.live("focus",function(){this.railsAutoCompleter||(this.railsAutoCompleter=new a.railsAutocomplete(this))})},a.railsAutocomplete=function(a){_e=a,this.init(_e)},a.railsAutocomplete.fn=a.railsAutocomplete.prototype={railsAutocomplete:"0.0.1"},a.railsAutocomplete.fn.extend=a.railsAutocomplete.extend=a.extend,a.railsAutocomplete.fn.extend({init:function(a){function c(a){return b(a).pop().replace(/^\s+/,"")}function b(b){return b.split(a.delimiter)}a.delimiter=$(a).attr("data-delimiter")||null,$(a).autocomplete({source:function(b,d){$.getJSON($(a).attr("data-autocomplete"),{term:c(b.term)},function(){$(arguments[0]).each(function(b,c){var d={};d[c.id]=c,$(a).data(d)}),d.apply(null,arguments)})},search:function(){var a=c(this.value);if(a.length<2)return!1},focus:function(){return!1},select:function(c,d){var f=b(this.value);f.pop(),f.push(d.item.value);if(a.delimiter!=null)f.push(""),this.value=f.join(a.delimiter);else{this.value=f.join(""),$(this).attr("id_element")&&$($(this).attr("id_element")).val(d.item.id);if($(this).attr("data-update-elements")){var g=$(this).data(d.item.id.toString()),h=$.parseJSON($(this).attr("data-update-elements"));for(var i in h)$(h[i]).val(g[i])}}var j=this.value;$(this).bind("keyup.clearId",function(){$(this).val().trim()!=j.trim()&&($($(this).attr("id_element")).val(""),$(this).unbind("keyup.clearId"))}),$(this).trigger("railsAutocomplete.select");return!1}})}})}(jQuery)
|
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
|
|
20
20
|
s.add_development_dependency('mongo_mapper', '>= 0.9')
|
21
21
|
s.add_development_dependency('bson_ext', '~>1.3.0')
|
22
22
|
s.add_development_dependency('shoulda', '~>2.11.1')
|
23
|
+
s.add_development_dependency('uglifier')
|
23
24
|
|
24
25
|
s.files = `git ls-files`.split("\n")
|
25
26
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/autocomplete/install_generator'
|
3
|
+
|
4
|
+
module Autocomplete
|
5
|
+
class InstallGeneratorTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@destination = File.join('tmp', 'test_app')
|
8
|
+
@source = InstallGenerator.source_root
|
9
|
+
@filename = File.join(@destination, 'public', 'javascripts', 'autocomplete-rails.js')
|
10
|
+
|
11
|
+
File.unlink(@filename) if File.exists?(@filename)
|
12
|
+
|
13
|
+
InstallGenerator.start('', :destination_root => @destination)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_install
|
17
|
+
assert File.exists?(@filename)
|
18
|
+
|
19
|
+
assert_equal(
|
20
|
+
File.read(File.join(@source, 'autocomplete-rails.js')),
|
21
|
+
File.read(@filename)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'generators/autocomplete/uncompressed_generator'
|
3
|
+
|
4
|
+
module Autocomplete
|
5
|
+
class UncompressedGeneratorTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@destination = File.join('tmp', 'test_app')
|
8
|
+
@source = UncompressedGenerator.source_root
|
9
|
+
@filename = File.join(@destination, 'public', 'javascripts', 'autocomplete-rails.js')
|
10
|
+
|
11
|
+
File.unlink(@filename) if File.exists?(@filename)
|
12
|
+
|
13
|
+
InstallGenerator.start('', :destination_root => @destination)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_install
|
17
|
+
assert File.exists?(@filename)
|
18
|
+
|
19
|
+
assert_equal(
|
20
|
+
File.read(File.join(@source, 'autocomplete-rails.js')),
|
21
|
+
File.read(@filename)
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
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: 63
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.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: 2011-06-
|
18
|
+
date: 2011-06-18 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -124,6 +124,20 @@ dependencies:
|
|
124
124
|
version: 2.11.1
|
125
125
|
type: :development
|
126
126
|
version_requirements: *id007
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: uglifier
|
129
|
+
prerelease: false
|
130
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
131
|
+
none: false
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
hash: 3
|
136
|
+
segments:
|
137
|
+
- 0
|
138
|
+
version: "0"
|
139
|
+
type: :development
|
140
|
+
version_requirements: *id008
|
127
141
|
description: Use jQuery's autocomplete plugin with Rails 3.
|
128
142
|
email: david.padilla@crowdint.com
|
129
143
|
executables: []
|
@@ -231,7 +245,9 @@ files:
|
|
231
245
|
- integration/spec/acceptance/support/paths.rb
|
232
246
|
- integration/spec/spec_helper.rb
|
233
247
|
- lib/cucumber/autocomplete.rb
|
234
|
-
- lib/generators/
|
248
|
+
- lib/generators/autocomplete/install_generator.rb
|
249
|
+
- lib/generators/autocomplete/uncompressed_generator.rb
|
250
|
+
- lib/generators/templates/autocomplete-rails-uncompressed.js
|
235
251
|
- lib/generators/templates/autocomplete-rails.js
|
236
252
|
- lib/rails3-jquery-autocomplete.rb
|
237
253
|
- lib/rails3-jquery-autocomplete/autocomplete.rb
|
@@ -243,7 +259,8 @@ files:
|
|
243
259
|
- lib/steak/autocomplete.rb
|
244
260
|
- rails3-jquery-autocomplete.gemspec
|
245
261
|
- test/form_helper_test.rb
|
246
|
-
- test/generators/
|
262
|
+
- test/generators/autocomplete/install_generator_test.rb
|
263
|
+
- test/generators/autocomplete/uncompressed_generator_test.rb
|
247
264
|
- test/implementations_test.rb
|
248
265
|
- test/lib/rails3-jquery-autocomplete/helpers_test.rb
|
249
266
|
- test/support/active_record.rb
|
@@ -287,7 +304,8 @@ specification_version: 3
|
|
287
304
|
summary: Use jQuery's autocomplete plugin with Rails 3.
|
288
305
|
test_files:
|
289
306
|
- test/form_helper_test.rb
|
290
|
-
- test/generators/
|
307
|
+
- test/generators/autocomplete/install_generator_test.rb
|
308
|
+
- test/generators/autocomplete/uncompressed_generator_test.rb
|
291
309
|
- test/implementations_test.rb
|
292
310
|
- test/lib/rails3-jquery-autocomplete/helpers_test.rb
|
293
311
|
- test/support/active_record.rb
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'rails/generators'
|
2
|
-
|
3
|
-
class AutocompleteGenerator < Rails::Generators::Base
|
4
|
-
def install
|
5
|
-
# Copy the unobtrusive JS file
|
6
|
-
copy_file('autocomplete-rails.js', 'public/javascripts/autocomplete-rails.js')
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.source_root
|
10
|
-
File.join(File.dirname(__FILE__), 'templates')
|
11
|
-
end
|
12
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'generators/autocomplete_generator'
|
3
|
-
|
4
|
-
class AutocompleteGeneratorTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
@destination = File.join('tmp', 'test_app')
|
7
|
-
@source = AutocompleteGenerator.source_root
|
8
|
-
@filename = File.join(@destination, 'public', 'javascripts', 'autocomplete-rails.js')
|
9
|
-
|
10
|
-
File.unlink(@filename) if File.exists?(@filename)
|
11
|
-
|
12
|
-
AutocompleteGenerator.start('', :destination_root => @destination)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_install
|
16
|
-
assert File.exists?(@filename)
|
17
|
-
|
18
|
-
assert_equal(
|
19
|
-
File.read(File.join(@source, 'autocomplete-rails.js')),
|
20
|
-
File.read(@filename)
|
21
|
-
)
|
22
|
-
end
|
23
|
-
end
|