rails3-jquery-autocomplete-moc 0.1.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/.document +5 -0
- data/.gitignore +24 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +94 -0
- data/LICENSE +20 -0
- data/README.markdown +148 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/lib/generators/autocomplete_generator.rb +12 -0
- data/lib/generators/templates/autocomplete-rails.js +29 -0
- data/lib/rails3-jquery-autocomplete.rb +51 -0
- data/rails3-jquery-autocomplete.gemspec +60 -0
- data/test/autocomplete_generator_test.rb +22 -0
- data/test/controller_module_test.rb +137 -0
- data/test/test_helper.rb +34 -0
- metadata +99 -0
data/.document
ADDED
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.0)
|
6
|
+
actionpack (= 3.0.0)
|
7
|
+
mail (~> 2.2.5)
|
8
|
+
actionpack (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.4.1)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.12)
|
16
|
+
rack-test (~> 0.5.4)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.0)
|
19
|
+
activesupport (= 3.0.0)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.4.1)
|
22
|
+
activerecord (3.0.0)
|
23
|
+
activemodel (= 3.0.0)
|
24
|
+
activesupport (= 3.0.0)
|
25
|
+
arel (~> 1.0.0)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.0)
|
28
|
+
activemodel (= 3.0.0)
|
29
|
+
activesupport (= 3.0.0)
|
30
|
+
activesupport (3.0.0)
|
31
|
+
arel (1.0.1)
|
32
|
+
activesupport (~> 3.0.0)
|
33
|
+
builder (2.1.2)
|
34
|
+
erubis (2.6.6)
|
35
|
+
abstract (>= 1.0.0)
|
36
|
+
gemcutter (0.6.1)
|
37
|
+
git (1.2.5)
|
38
|
+
i18n (0.4.1)
|
39
|
+
jeweler (1.4.0)
|
40
|
+
gemcutter (>= 0.1.0)
|
41
|
+
git (>= 1.2.5)
|
42
|
+
rubyforge (>= 2.0.0)
|
43
|
+
json_pure (1.4.6)
|
44
|
+
mail (2.2.5)
|
45
|
+
activesupport (>= 2.3.6)
|
46
|
+
mime-types
|
47
|
+
treetop (>= 1.4.5)
|
48
|
+
mime-types (1.16)
|
49
|
+
polyglot (0.3.1)
|
50
|
+
rack (1.2.1)
|
51
|
+
rack-mount (0.6.12)
|
52
|
+
rack (>= 1.0.0)
|
53
|
+
rack-test (0.5.4)
|
54
|
+
rack (>= 1.0)
|
55
|
+
rails (3.0.0)
|
56
|
+
actionmailer (= 3.0.0)
|
57
|
+
actionpack (= 3.0.0)
|
58
|
+
activerecord (= 3.0.0)
|
59
|
+
activeresource (= 3.0.0)
|
60
|
+
activesupport (= 3.0.0)
|
61
|
+
bundler (~> 1.0.0)
|
62
|
+
railties (= 3.0.0)
|
63
|
+
railties (3.0.0)
|
64
|
+
actionpack (= 3.0.0)
|
65
|
+
activesupport (= 3.0.0)
|
66
|
+
rake (>= 0.8.4)
|
67
|
+
thor (~> 0.14.0)
|
68
|
+
rake (0.8.7)
|
69
|
+
reek (1.2.8)
|
70
|
+
ruby2ruby (~> 1.2)
|
71
|
+
ruby_parser (~> 2.0)
|
72
|
+
sexp_processor (~> 3.0)
|
73
|
+
ruby2ruby (1.2.4)
|
74
|
+
ruby_parser (~> 2.0)
|
75
|
+
sexp_processor (~> 3.0)
|
76
|
+
ruby_parser (2.0.4)
|
77
|
+
sexp_processor (~> 3.0)
|
78
|
+
rubyforge (2.0.4)
|
79
|
+
json_pure (>= 1.1.7)
|
80
|
+
sexp_processor (3.0.4)
|
81
|
+
sqlite3-ruby (1.3.1)
|
82
|
+
thor (0.14.0)
|
83
|
+
treetop (1.4.8)
|
84
|
+
polyglot (>= 0.3.1)
|
85
|
+
tzinfo (0.3.23)
|
86
|
+
|
87
|
+
PLATFORMS
|
88
|
+
ruby
|
89
|
+
|
90
|
+
DEPENDENCIES
|
91
|
+
jeweler
|
92
|
+
rails (= 3.0.0)
|
93
|
+
reek
|
94
|
+
sqlite3-ruby
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 David Padilla
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
# rails3-jquery-autocomplete
|
2
|
+
|
3
|
+
An easy way to use jQuery's autocomplete with Rails 3. You can find a [detailed example](http://github.com/crowdint/rails3-jquery-autocomplete-app)
|
4
|
+
on how to use this gem [here](http://github.com/crowdint/rails3-jquery-autocomplete-app).
|
5
|
+
|
6
|
+
## Before you start
|
7
|
+
|
8
|
+
Make sure your project is using jQuery-ui with the autocomplete widget
|
9
|
+
before you continue.
|
10
|
+
|
11
|
+
You can find more info about that here:
|
12
|
+
|
13
|
+
* http://jquery.com/
|
14
|
+
* http://jqueryui.com/demos/autocomplete/
|
15
|
+
* http://github.com/rails/jquery-ujs
|
16
|
+
|
17
|
+
I'd encourage you to understand how to use those 3 amazing tools before attempting to use this gem.
|
18
|
+
|
19
|
+
## Installing
|
20
|
+
|
21
|
+
Include the gem on your Gemfile
|
22
|
+
|
23
|
+
gem 'rails3-jquery-autocomplete', '>= 0.2.0'
|
24
|
+
|
25
|
+
Install it
|
26
|
+
|
27
|
+
bundle install
|
28
|
+
|
29
|
+
Run the generator
|
30
|
+
|
31
|
+
rails generate autocomplete
|
32
|
+
|
33
|
+
And include autocomplete-rails.js on your layouts
|
34
|
+
|
35
|
+
javascript_include_tag "autocomplete-rails.js"
|
36
|
+
|
37
|
+
## Usage
|
38
|
+
|
39
|
+
### Model Example
|
40
|
+
|
41
|
+
Assuming you have a Brand model:
|
42
|
+
|
43
|
+
class Brand < ActiveRecord::Base
|
44
|
+
end
|
45
|
+
|
46
|
+
create_table :brand do |t|
|
47
|
+
t.column :name, :string
|
48
|
+
end
|
49
|
+
|
50
|
+
### Controller
|
51
|
+
|
52
|
+
To set up the required action on your controller, all you have to do is call it with the class name and the method
|
53
|
+
as in the following example:
|
54
|
+
|
55
|
+
class ProductsController < Admin::BaseController
|
56
|
+
autocomplete :brand, :name
|
57
|
+
end
|
58
|
+
|
59
|
+
This will create an action _autocomplete_brand_name_ on your controller, don't forget to add it on your routes file
|
60
|
+
|
61
|
+
resources :products do
|
62
|
+
get :autocomplete_brand_name, :on => :collection
|
63
|
+
end
|
64
|
+
|
65
|
+
### Options
|
66
|
+
|
67
|
+
#### :full => true
|
68
|
+
|
69
|
+
By default, the search starts from the beginning of the string you're searching for. If you want to do a full search, set the _full_ parameter to true.
|
70
|
+
|
71
|
+
class ProductsController < Admin::BaseController
|
72
|
+
autocomplete :brand, :name, :full => true
|
73
|
+
end
|
74
|
+
|
75
|
+
The following terms would match the query 'un':
|
76
|
+
|
77
|
+
* Luna
|
78
|
+
* Unacceptable
|
79
|
+
* Rerun
|
80
|
+
|
81
|
+
#### :full => false (default behavior)
|
82
|
+
|
83
|
+
Only the following terms mould match the query 'un':
|
84
|
+
|
85
|
+
* Unacceptable
|
86
|
+
|
87
|
+
#### :display_value
|
88
|
+
|
89
|
+
If you want to display a different version of what you're looking for, you can use the :display_value option.
|
90
|
+
|
91
|
+
This options receives a method name as the parameter, and that method will be called on the instance when displaying the results.
|
92
|
+
|
93
|
+
class Brand < ActiveRecord::Base
|
94
|
+
def funky_method
|
95
|
+
"#{self.name}.camelize"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
class ProductsController < Admin::BaseController
|
101
|
+
autocomplete :brand, :name, :display_value => :funky_method
|
102
|
+
end
|
103
|
+
|
104
|
+
In the example above, you will search by _name_, but the autocomplete list will display the result of _funky_method_
|
105
|
+
|
106
|
+
This wouldn't really make much sense unless you use it with the :id_element HTML tag. (See below)
|
107
|
+
|
108
|
+
### View
|
109
|
+
|
110
|
+
On your view, all you have to do is include the attribute autocomplete on the text field
|
111
|
+
using the url to the autocomplete action as the value.
|
112
|
+
form_for @product do |f|
|
113
|
+
f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
|
114
|
+
end
|
115
|
+
|
116
|
+
This will generate an HTML tag that looks like:
|
117
|
+
|
118
|
+
<input type="text" autocomplete="products/autocomplete_brand_name">
|
119
|
+
|
120
|
+
Now your autocomplete JS code is unobtrusive, Rails 3 style.
|
121
|
+
|
122
|
+
### Getting the object id
|
123
|
+
|
124
|
+
If you need to use the id of the selected object, you can use the *:id_element* HTML tag too:
|
125
|
+
|
126
|
+
f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path, :id_element => '#some_element'
|
127
|
+
|
128
|
+
This will update the field with id *#some_element with the id of the selected object. The value for this option can be any jQuery selector.
|
129
|
+
|
130
|
+
## Development
|
131
|
+
|
132
|
+
If you want to make changes to the gem, first install bundler 1.0.0:
|
133
|
+
|
134
|
+
gem install bundler --pre
|
135
|
+
|
136
|
+
And then, install all your dependencies:
|
137
|
+
|
138
|
+
bundle install
|
139
|
+
|
140
|
+
### Running the test suite
|
141
|
+
|
142
|
+
rake test
|
143
|
+
|
144
|
+
# About the Author
|
145
|
+
|
146
|
+
[Crowd Interactive](http://www.crowdint.com) is an American web design and development company that happens to work in Colima, Mexico.
|
147
|
+
We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit.
|
148
|
+
Find more info [here](http://www.crowdint.com)!
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "rails3-jquery-autocomplete-moc"
|
8
|
+
gem.summary = "Adaptation of rails3-jquery-autocomplete"
|
9
|
+
gem.description = "Adapted from dabit's rails3-jquery-autocomplete for Moving Off Campus"
|
10
|
+
gem.email = "doug@adeviasoftware.com"
|
11
|
+
gem.homepage = "http://github.com/adeviadoug/rails3-jquery-autocomplete-moc"
|
12
|
+
gem.authors = ["doug schlenker"]
|
13
|
+
gem.rubyforge_project = "rails3-jquery-autocomplete-moc"
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
Jeweler::RubyforgeTasks.new do |rubyforge|
|
19
|
+
rubyforge.doc_task = "rdoc"
|
20
|
+
end
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
Rake::TestTask.new(:test) do |test|
|
27
|
+
test.libs << 'lib' << 'test'
|
28
|
+
test.pattern = 'test/**/*_test.rb'
|
29
|
+
test.verbose = true
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'rcov/rcovtask'
|
34
|
+
Rcov::RcovTask.new do |test|
|
35
|
+
test.libs << 'test'
|
36
|
+
test.pattern = 'test/**/test_*.rb'
|
37
|
+
test.verbose = true
|
38
|
+
end
|
39
|
+
rescue LoadError
|
40
|
+
task :rcov do
|
41
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
task :test => :check_dependencies
|
46
|
+
|
47
|
+
task :default => :test
|
48
|
+
|
49
|
+
require 'rake/rdoctask'
|
50
|
+
Rake::RDocTask.new do |rdoc|
|
51
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "rails3-jquery-autocomplete #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,12 @@
|
|
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
|
@@ -0,0 +1,29 @@
|
|
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" 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" autocomplete="/url/to/autocomplete" id_element="#id_field">
|
15
|
+
*/
|
16
|
+
|
17
|
+
$(document).ready(function(){
|
18
|
+
$('input[autocomplete]').each(function(i){
|
19
|
+
$(this).autocomplete({
|
20
|
+
source: $(this).attr('autocomplete'),
|
21
|
+
select: function(event, ui) {
|
22
|
+
if($(this).attr('id_element')) {
|
23
|
+
$($(this).attr('id_element')).val(ui.item.id);
|
24
|
+
}
|
25
|
+
return false;
|
26
|
+
}
|
27
|
+
});
|
28
|
+
});
|
29
|
+
});
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Rails3JQueryAutocomplete
|
2
|
+
def self.included(base)
|
3
|
+
base.extend(ClassMethods)
|
4
|
+
end
|
5
|
+
|
6
|
+
# Inspired on DHH's autocomplete plugin
|
7
|
+
#
|
8
|
+
# Usage:
|
9
|
+
#
|
10
|
+
# class ProductsController < Admin::BaseController
|
11
|
+
# autocomplete :brand, :name
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# This will magically generate an action autocomplete_brand_name, so,
|
15
|
+
# don't forget to add it on your routes file
|
16
|
+
#
|
17
|
+
# resources :products do
|
18
|
+
# get :autocomplete_brand_name, :on => :collection
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# Now, on your view, all you have to do is have a text field like:
|
22
|
+
#
|
23
|
+
# f.text_field :brand_name, :autocomplete => autocomplete_brand_name_products_path
|
24
|
+
#
|
25
|
+
#
|
26
|
+
module ClassMethods
|
27
|
+
def autocomplete(object, method, options = {})
|
28
|
+
limit = options[:limit] || 10
|
29
|
+
order = options[:order] || "#{method} ASC"
|
30
|
+
|
31
|
+
define_method("autocomplete_#{object}_#{method}") do
|
32
|
+
unless params[:term] && params[:term].empty?
|
33
|
+
items = object.to_s.camelize.constantize.where(["LOWER(#{method}) LIKE ?", "#{(options[:full] ? '%' : '')}#{params[:term].downcase}%"]).limit(limit).order(order)
|
34
|
+
else
|
35
|
+
items = {}
|
36
|
+
end
|
37
|
+
|
38
|
+
render :json => json_for_autocomplete(items, (options[:display_value] ? options[:display_value] : method))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def json_for_autocomplete(items, method)
|
45
|
+
items.collect {|i| {"id" => i.id, "label" => i.send(method), "value" => i.send(method)}}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class ActionController::Base
|
50
|
+
include Rails3JQueryAutocomplete
|
51
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{rails3-jquery-autocomplete}
|
8
|
+
s.version = "0.2.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["David Padilla"]
|
12
|
+
s.date = %q{2010-08-31}
|
13
|
+
s.description = %q{Use jQuery's autocomplete plugin with Rails 3.}
|
14
|
+
s.email = %q{david.padilla@crowdint.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE",
|
25
|
+
"README.markdown",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/generators/autocomplete_generator.rb",
|
29
|
+
"lib/generators/templates/autocomplete-rails.js",
|
30
|
+
"lib/rails3-jquery-autocomplete.rb",
|
31
|
+
"rails3-jquery-autocomplete.gemspec",
|
32
|
+
"test/autocomplete_generator_test.rb",
|
33
|
+
"test/controller_module_test.rb",
|
34
|
+
"test/test_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/crowdint/rails3-jquery-autocomplete}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.7}
|
40
|
+
s.summary = %q{Use jQuery's autocomplete plugin with Rails 3.}
|
41
|
+
s.test_files = [
|
42
|
+
"test/autocomplete_generator_test.rb",
|
43
|
+
"test/controller_module_test.rb",
|
44
|
+
"test/test_helper.rb"
|
45
|
+
]
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_development_dependency(%q<rails>, [">= 3.0.0.rc2"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rails>, [">= 3.0.0.rc2"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rails>, [">= 3.0.0.rc2"])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
@@ -0,0 +1,22 @@
|
|
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
|
+
|
9
|
+
AutocompleteGenerator.start('', :destination_root => @destination)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_install
|
13
|
+
assert File.exists?(
|
14
|
+
File.join(@destination, 'public', 'javascripts', 'autocomplete-rails.js')
|
15
|
+
)
|
16
|
+
|
17
|
+
assert_equal(
|
18
|
+
File.read(File.join(@source, 'autocomplete-rails.js')),
|
19
|
+
File.read(File.join(@destination, 'public', 'javascripts', 'autocomplete-rails.js'))
|
20
|
+
)
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,137 @@
|
|
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
|
+
def setup
|
40
|
+
setup_db
|
41
|
+
|
42
|
+
@controller = ActorsController.new
|
43
|
+
@controller.request = @request = ActionController::TestRequest.new
|
44
|
+
@controller.response = @response = ActionController::TestResponse.new
|
45
|
+
end
|
46
|
+
|
47
|
+
def teardown
|
48
|
+
teardown_db
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_response_succesful
|
52
|
+
get :autocomplete_movie_name, :term => 'Al'
|
53
|
+
assert_response :success
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_response_json
|
57
|
+
@movie = Movie.create(:name => 'Alpha')
|
58
|
+
|
59
|
+
get :autocomplete_movie_name, :term => 'Al'
|
60
|
+
json_response = JSON.parse(@response.body)
|
61
|
+
assert_equal(json_response.first["label"], @movie.name)
|
62
|
+
assert_equal(json_response.first["value"], @movie.name)
|
63
|
+
assert_equal(json_response.first["id"], @movie.id)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_alphabetic_order
|
67
|
+
@movie = Movie.create(:name => 'Alzpha')
|
68
|
+
@movie = Movie.create(:name => 'Alspha')
|
69
|
+
@movie = Movie.create(:name => 'Alpha')
|
70
|
+
|
71
|
+
get :autocomplete_movie_name, :term => 'Al'
|
72
|
+
json_response = JSON.parse(@response.body)
|
73
|
+
assert_equal(json_response.first["label"], "Alpha")
|
74
|
+
assert_equal(json_response.last["label"], "Alzpha")
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_alternative_sort_order
|
78
|
+
@movie = Movie.create(:name => 'Alzpha')
|
79
|
+
@movie = Movie.create(:name => 'Alspha')
|
80
|
+
@movie = Movie.create(:name => 'Alpha')
|
81
|
+
|
82
|
+
ActorsController.send(:autocomplete, :movie, :name, {:order => "name DESC"})
|
83
|
+
|
84
|
+
get :autocomplete_movie_name, :term => 'Al'
|
85
|
+
json_response = JSON.parse(@response.body)
|
86
|
+
assert_equal(json_response.first["label"], "Alzpha")
|
87
|
+
assert_equal(json_response.last["label"], "Alpha")
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_response_limit
|
91
|
+
@movie = Movie.create(:name => 'Alzpha')
|
92
|
+
@movie = Movie.create(:name => 'Alspha')
|
93
|
+
@movie = Movie.create(:name => 'Alpha')
|
94
|
+
|
95
|
+
ActorsController.send(:autocomplete, :movie, :name, {:limit => 1})
|
96
|
+
|
97
|
+
get :autocomplete_movie_name, :term => 'Al'
|
98
|
+
json_response = JSON.parse(@response.body)
|
99
|
+
assert_equal(json_response.length, 1)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_downcase
|
103
|
+
@movie = Movie.create(:name => 'aLpHa')
|
104
|
+
|
105
|
+
ActorsController.send(:autocomplete, :movie, :name)
|
106
|
+
|
107
|
+
get :autocomplete_movie_name, :term => 'Al'
|
108
|
+
json_response = JSON.parse(@response.body)
|
109
|
+
assert_equal(json_response.length, 1)
|
110
|
+
assert_equal(json_response.first["label"], 'aLpHa')
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_full_search
|
114
|
+
@movie = Movie.create(:name => 'aLpHa')
|
115
|
+
|
116
|
+
ActorsController.send(:autocomplete, :movie, :name, {:full => true})
|
117
|
+
|
118
|
+
get :autocomplete_movie_name, :term => 'ph'
|
119
|
+
json_response = JSON.parse(@response.body)
|
120
|
+
assert_equal(json_response.length, 1)
|
121
|
+
assert_equal(json_response.first["label"], 'aLpHa')
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_value_option
|
125
|
+
ActorsController.send(:autocomplete, :movie, :name, {:display_value => :display_name})
|
126
|
+
|
127
|
+
@movie = Movie.create(:name => 'Alpha')
|
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
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
gem 'rails', '>=3.0.0.rc'
|
5
|
+
gem 'sqlite3-ruby'
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
require "active_support"
|
11
|
+
require "active_record"
|
12
|
+
require "active_model"
|
13
|
+
require "action_controller"
|
14
|
+
require "rails/railtie"
|
15
|
+
|
16
|
+
require 'rails3-jquery-autocomplete'
|
17
|
+
|
18
|
+
class ApplicationController < ActionController::Base; end
|
19
|
+
|
20
|
+
ActionController::Base.view_paths = File.join(File.dirname(__FILE__), 'views')
|
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'
|
26
|
+
end
|
27
|
+
|
28
|
+
ActionController::Base.send :include, Rails3JQueryAutocomplete::Routes.url_helpers
|
29
|
+
|
30
|
+
class ActiveSupport::TestCase
|
31
|
+
setup do
|
32
|
+
@routes = Rails3JQueryAutocomplete::Routes
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails3-jquery-autocomplete-moc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- doug schlenker
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-06 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 2
|
33
|
+
- 9
|
34
|
+
version: 1.2.9
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Adapted from dabit's rails3-jquery-autocomplete for Moving Off Campus
|
38
|
+
email: doug@adeviasoftware.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.markdown
|
46
|
+
files:
|
47
|
+
- .document
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE
|
52
|
+
- README.markdown
|
53
|
+
- Rakefile
|
54
|
+
- VERSION
|
55
|
+
- lib/generators/autocomplete_generator.rb
|
56
|
+
- lib/generators/templates/autocomplete-rails.js
|
57
|
+
- lib/rails3-jquery-autocomplete.rb
|
58
|
+
- rails3-jquery-autocomplete.gemspec
|
59
|
+
- test/autocomplete_generator_test.rb
|
60
|
+
- test/controller_module_test.rb
|
61
|
+
- test/test_helper.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://github.com/adeviadoug/rails3-jquery-autocomplete-moc
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options:
|
68
|
+
- --charset=UTF-8
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project: rails3-jquery-autocomplete-moc
|
92
|
+
rubygems_version: 1.3.7
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Adaptation of rails3-jquery-autocomplete
|
96
|
+
test_files:
|
97
|
+
- test/test_helper.rb
|
98
|
+
- test/autocomplete_generator_test.rb
|
99
|
+
- test/controller_module_test.rb
|