mreinsch-auto_complete_jquery 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/README.rdoc +67 -0
- data/Rakefile +11 -0
- data/auto_complete_jquery.gemspec +19 -0
- data/init.rb +1 -0
- data/lib/auto_complete_jquery.rb +69 -0
- metadata +61 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,5 @@
|
|
1
|
+
* 10 June 2009: 0.2.1
|
2
|
+
- some documentation updates and code cleanups
|
3
|
+
- support for named scopes
|
4
|
+
|
5
|
+
* 1 July 2008: Switched to a different jQuery autocomplete plugin to be used with this plugin. The previous plugin was not handling case insensitivity, was slow, and lacked various other options. The new jQuery autocomplete plugin this depends on his a nice improvement, being massively faster, providing a CSS style you can use while your AJAX request runs, handling case properly, and adding a slew of options.
|
data/README.rdoc
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
This plugin provides a auto-complete method for your controllers to be used
|
2
|
+
with Dylan Verheul's jquery autocomplete plugin. This jQuery plugin is not
|
3
|
+
included, and can be obtained from:
|
4
|
+
|
5
|
+
http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
|
6
|
+
|
7
|
+
This auto_complete_jquery plugin is just a modified version of the standard
|
8
|
+
Rails auto_complete plugin. It provides the same auto_complete_for method
|
9
|
+
for your controllers, but eliminates the various view helper methods, as
|
10
|
+
those are not needed when using jQuery and Unobtrusive JavaScript.
|
11
|
+
|
12
|
+
To use this, you need to have jQuery and the autocomplete plugin mentioned
|
13
|
+
above (as well as appropriate CSS). Then, there are four aspects of setting
|
14
|
+
up an auto-complete field:
|
15
|
+
|
16
|
+
1) Create the text field in your view, which is just a regular form text
|
17
|
+
field as you'd create in a Rails erb view:
|
18
|
+
|
19
|
+
<%= post.text_field :title, :autocomplete =>"off" %>
|
20
|
+
|
21
|
+
2) Include the appropriate JS files and CSS in your layout or similar:
|
22
|
+
|
23
|
+
<%= stylesheet_link_tag 'jquery.autocomplete' %>
|
24
|
+
<%= javascript_include_tag 'jquery.min', 'jquery.autocomplete.min', :cache => 'jquery' %>
|
25
|
+
|
26
|
+
3) Use the auto_complete_for controller macro to generate the appropriate
|
27
|
+
auto-complete method in your controller, such as:
|
28
|
+
|
29
|
+
# Controller
|
30
|
+
class BlogController < ApplicationController
|
31
|
+
auto_complete_for :post, :title
|
32
|
+
end
|
33
|
+
|
34
|
+
4) Hook up the text field's autocomplete event in your JavaScript file
|
35
|
+
using jQuery and the jquery-autocomplete plugin mechanism, for example:
|
36
|
+
|
37
|
+
$(document).ready(function() {
|
38
|
+
$("input#post_title").autocomplete({ajax: "/blog/auto_complete_for_post_title"})
|
39
|
+
});
|
40
|
+
|
41
|
+
By default, auto_complete_for limits the results to 10 entries,
|
42
|
+
and sorts by the given field.
|
43
|
+
|
44
|
+
auto_complete_for takes a third parameter, an options hash to
|
45
|
+
the find method used to search for the records:
|
46
|
+
|
47
|
+
auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
48
|
+
|
49
|
+
5) Create a route for the auto complete function. You can add the following
|
50
|
+
generic route at the beginning of your config/routes.rb to take care of this:
|
51
|
+
|
52
|
+
map.auto_complete ':controller/:action',
|
53
|
+
:requirements => { :action => /auto_complete_for_\S+/ },
|
54
|
+
:conditions => { :method => :get }
|
55
|
+
|
56
|
+
|
57
|
+
For more information, see:
|
58
|
+
* jQuery site: http://jquery.com
|
59
|
+
* Dylan Verheul jquery autocomplete plugin site: http://www.dyve.net/jquery/?autocomplete
|
60
|
+
* Good article on jQuery and Rails (note this mentions a different jquery autocomplete
|
61
|
+
plugin, which was originally used in this plugin): http://errtheblog.com/posts/73-the-jskinny-on-jquery
|
62
|
+
* Original Rails auto_complete plugin: http://github.com/rails/auto_complete
|
63
|
+
|
64
|
+
|
65
|
+
Copyright (c) 2008 Cobalt Edge LLC, released under the MIT license.
|
66
|
+
Copyright (c) 2009 mobalean LLC, released under the MIT license.
|
67
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
desc 'Generate documentation for auto_complete plugin.'
|
5
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
6
|
+
rdoc.rdoc_dir = 'rdoc'
|
7
|
+
rdoc.title = 'Auto Complete jQuery'
|
8
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
9
|
+
rdoc.rdoc_files.include('README.rdoc')
|
10
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
11
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "auto_complete_jquery"
|
3
|
+
s.version = "0.2.1"
|
4
|
+
s.date = "2009-06-10"
|
5
|
+
s.summary = "auto-complete method for Rails controllers using Dylan Verheul's jQuery autocomplete plugin"
|
6
|
+
s.email = "michael@mobalean.com"
|
7
|
+
s.homepage = "http://github.com/mreinsch/auto_complete_jquery"
|
8
|
+
s.description = "This plugin provides a auto-complete method for your controllers to be used with Dylan Verheul's jquery autocomplete plugin."
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Chris Bailey", "Michael Reinsch"]
|
11
|
+
s.files = ["CHANGELOG",
|
12
|
+
"README.rdoc",
|
13
|
+
"Rakefile",
|
14
|
+
"auto_complete_jquery.gemspec",
|
15
|
+
"init.rb",
|
16
|
+
"lib/auto_complete_jquery.rb"]
|
17
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
18
|
+
s.extra_rdoc_files = ["CHANGELOG", "README.rdoc"]
|
19
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ActionController::Base.send :include, AutoCompleteJquery
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module AutoCompleteJquery
|
2
|
+
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# # Controller
|
11
|
+
# class BlogController < ApplicationController
|
12
|
+
# auto_complete_for :post, :title
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# By default, auto_complete_for limits the results to 10 entries,
|
16
|
+
# and sorts by the given field.
|
17
|
+
#
|
18
|
+
# auto_complete_for takes a third parameter, an options hash to
|
19
|
+
# the find method used to search for the records:
|
20
|
+
#
|
21
|
+
# auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
22
|
+
#
|
23
|
+
# You can also use named scopes to limit the search results. Two types of
|
24
|
+
# named scopes are supported.
|
25
|
+
#
|
26
|
+
# 1) Without parameters:
|
27
|
+
#
|
28
|
+
# auto_complete_for :post, :title, :scope => :red
|
29
|
+
# named_scope :red, conditions => {color: => 'red'}
|
30
|
+
#
|
31
|
+
# This will use Post.red.find(:all, ...) to lookup the post titles.
|
32
|
+
#
|
33
|
+
# 2) With the current_user as parameter:
|
34
|
+
#
|
35
|
+
# auto_complete_for :post, :title, :user_scope => :author
|
36
|
+
# named_scope :author, lambda { |user|
|
37
|
+
# { :conditions => { :author_id => user } }
|
38
|
+
# }
|
39
|
+
#
|
40
|
+
# This would use Post.author(current_user).find(:all, ...) to lookup the
|
41
|
+
# post titles.
|
42
|
+
#
|
43
|
+
# For more on jQuery auto-complete, see the docs for the jQuery autocomplete
|
44
|
+
# plugin used in conjunction with this plugin:
|
45
|
+
# * http://www.dyve.net/jquery/?autocomplete
|
46
|
+
module ClassMethods
|
47
|
+
def auto_complete_for(object, method, options = {})
|
48
|
+
object_constant = object.to_s.camelize.constantize
|
49
|
+
scope = options.delete(:scope)
|
50
|
+
user_scope = options.delete(:user_scope)
|
51
|
+
|
52
|
+
finder = scope.nil? ? object_constant : object_constant.send(scope)
|
53
|
+
generic_find_options = {
|
54
|
+
:order => "#{method} ASC",
|
55
|
+
:select => "#{object_constant.table_name}.#{method}",
|
56
|
+
:limit => 10 }.merge!(options)
|
57
|
+
|
58
|
+
define_method("auto_complete_for_#{object}_#{method}") do
|
59
|
+
find_options = generic_find_options.clone
|
60
|
+
find_options[:conditions] = [ "LOWER(#{method}) LIKE ?", '%' + params[:q].downcase + '%' ]
|
61
|
+
find_options[:limit] = [find_options[:limit], params[:limit].to_i].min if params[:limit]
|
62
|
+
|
63
|
+
render :text => (user_scope.nil? ? finder : finder.send(user_scope, current_user)).
|
64
|
+
find(:all, find_options).collect(&method).join("\n")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mreinsch-auto_complete_jquery
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Bailey
|
8
|
+
- Michael Reinsch
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-06-10 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: This plugin provides a auto-complete method for your controllers to be used with Dylan Verheul's jquery autocomplete plugin.
|
18
|
+
email: michael@mobalean.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- CHANGELOG
|
25
|
+
- README.rdoc
|
26
|
+
files:
|
27
|
+
- CHANGELOG
|
28
|
+
- README.rdoc
|
29
|
+
- Rakefile
|
30
|
+
- auto_complete_jquery.gemspec
|
31
|
+
- init.rb
|
32
|
+
- lib/auto_complete_jquery.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/mreinsch/auto_complete_jquery
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --main
|
38
|
+
- README.rdoc
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.2.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 2
|
59
|
+
summary: auto-complete method for Rails controllers using Dylan Verheul's jQuery autocomplete plugin
|
60
|
+
test_files: []
|
61
|
+
|