innetra-auto_complete 0.0.1
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/README +23 -0
- data/Rakefile +12 -0
- data/auto_complete.gemspec +32 -0
- data/init.rb +2 -0
- data/lib/auto_complete.rb +49 -0
- data/lib/auto_complete_macros_helper.rb +144 -0
- data/test/auto_complete_test.rb +67 -0
- metadata +67 -0
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Example:
|
2
|
+
|
3
|
+
# Controller
|
4
|
+
class BlogController < ApplicationController
|
5
|
+
auto_complete_for :post, :title
|
6
|
+
end
|
7
|
+
|
8
|
+
# View
|
9
|
+
<%= text_field_with_auto_complete :post, title %>
|
10
|
+
|
11
|
+
By default, auto_complete_for limits the results to 10 entries,
|
12
|
+
and sorts by the given field.
|
13
|
+
|
14
|
+
auto_complete_for takes a third parameter, an options hash to
|
15
|
+
the find method used to search for the records:
|
16
|
+
|
17
|
+
auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
18
|
+
|
19
|
+
For more examples, see script.aculo.us:
|
20
|
+
* http://script.aculo.us/demos/ajax/autocompleter
|
21
|
+
* http://script.aculo.us/demos/ajax/autocompleter_customized
|
22
|
+
|
23
|
+
Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('auto_complete', '0.0.1') do |e|
|
6
|
+
e.description = "Rails AutoComplete Plugin Gem Version"
|
7
|
+
e.url = "http://github.com/innetra/auto_complete"
|
8
|
+
e.author = "Ivan Torres"
|
9
|
+
e.email = "mexpolk@gmail.com"
|
10
|
+
e.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
+
e.development_dependencies = []
|
12
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{auto_complete}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ivan Torres"]
|
9
|
+
s.date = %q{2009-01-17}
|
10
|
+
s.description = %q{Rails AutoComplete Plugin Gem Version}
|
11
|
+
s.email = %q{mexpolk@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README", "lib/auto_complete.rb", "lib/auto_complete_macros_helper.rb"]
|
13
|
+
s.files = ["Manifest", "README", "auto_complete.gemspec", "test/auto_complete_test.rb", "Rakefile", "init.rb", "lib/auto_complete.rb", "lib/auto_complete_macros_helper.rb"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/innetra/auto_complete}
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Auto_complete", "--main", "README"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{auto_complete}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{Rails AutoComplete Plugin Gem Version}
|
21
|
+
s.test_files = ["test/auto_complete_test.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
else
|
29
|
+
end
|
30
|
+
else
|
31
|
+
end
|
32
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module AutoComplete
|
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
|
+
# # View
|
16
|
+
# <%= text_field_with_auto_complete :post, title %>
|
17
|
+
#
|
18
|
+
# By default, auto_complete_for limits the results to 10 entries,
|
19
|
+
# and sorts by the given field.
|
20
|
+
#
|
21
|
+
# auto_complete_for takes a third parameter, an options hash to
|
22
|
+
# the find method used to search for the records:
|
23
|
+
#
|
24
|
+
# auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
25
|
+
#
|
26
|
+
# For help on defining text input fields with autocompletion,
|
27
|
+
# see ActionView::Helpers::JavaScriptHelper.
|
28
|
+
#
|
29
|
+
# For more examples, see script.aculo.us:
|
30
|
+
# * http://script.aculo.us/demos/ajax/autocompleter
|
31
|
+
# * http://script.aculo.us/demos/ajax/autocompleter_customized
|
32
|
+
module ClassMethods
|
33
|
+
def auto_complete_for(object, method, options = {})
|
34
|
+
define_method("auto_complete_for_#{object}_#{method}") do
|
35
|
+
find_options = {
|
36
|
+
:conditions => [ "LOWER(#{method}) LIKE ?", '%' + params[object][method].downcase + '%' ],
|
37
|
+
:order => "#{method} ASC",
|
38
|
+
:limit => 10 }.merge!(options)
|
39
|
+
|
40
|
+
@items = object.to_s.camelize.constantize.find(:all, find_options)
|
41
|
+
|
42
|
+
render :inline => "<%= auto_complete_result @items, '#{method}' %>"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
ActionController::Base.send :include, AutoComplete
|
@@ -0,0 +1,144 @@
|
|
1
|
+
module AutoCompleteMacrosHelper
|
2
|
+
# Adds AJAX autocomplete functionality to the text input field with the
|
3
|
+
# DOM ID specified by +field_id+.
|
4
|
+
#
|
5
|
+
# This function expects that the called action returns an HTML <ul> list,
|
6
|
+
# or nothing if no entries should be displayed for autocompletion.
|
7
|
+
#
|
8
|
+
# You'll probably want to turn the browser's built-in autocompletion off,
|
9
|
+
# so be sure to include an <tt>autocomplete="off"</tt> attribute with your text
|
10
|
+
# input field.
|
11
|
+
#
|
12
|
+
# The autocompleter object is assigned to a Javascript variable named <tt>field_id</tt>_auto_completer.
|
13
|
+
# This object is useful if you for example want to trigger the auto-complete suggestions through
|
14
|
+
# other means than user input (for that specific case, call the <tt>activate</tt> method on that object).
|
15
|
+
#
|
16
|
+
# Required +options+ are:
|
17
|
+
# <tt>:url</tt>:: URL to call for autocompletion results
|
18
|
+
# in url_for format.
|
19
|
+
#
|
20
|
+
# Addtional +options+ are:
|
21
|
+
# <tt>:update</tt>:: Specifies the DOM ID of the element whose
|
22
|
+
# innerHTML should be updated with the autocomplete
|
23
|
+
# entries returned by the AJAX request.
|
24
|
+
# Defaults to <tt>field_id</tt> + '_auto_complete'
|
25
|
+
# <tt>:with</tt>:: A JavaScript expression specifying the
|
26
|
+
# parameters for the XMLHttpRequest. This defaults
|
27
|
+
# to 'fieldname=value'.
|
28
|
+
# <tt>:frequency</tt>:: Determines the time to wait after the last keystroke
|
29
|
+
# for the AJAX request to be initiated.
|
30
|
+
# <tt>:indicator</tt>:: Specifies the DOM ID of an element which will be
|
31
|
+
# displayed while autocomplete is running.
|
32
|
+
# <tt>:tokens</tt>:: A string or an array of strings containing
|
33
|
+
# separator tokens for tokenized incremental
|
34
|
+
# autocompletion. Example: <tt>:tokens => ','</tt> would
|
35
|
+
# allow multiple autocompletion entries, separated
|
36
|
+
# by commas.
|
37
|
+
# <tt>:min_chars</tt>:: The minimum number of characters that should be
|
38
|
+
# in the input field before an Ajax call is made
|
39
|
+
# to the server.
|
40
|
+
# <tt>:on_hide</tt>:: A Javascript expression that is called when the
|
41
|
+
# autocompletion div is hidden. The expression
|
42
|
+
# should take two variables: element and update.
|
43
|
+
# Element is a DOM element for the field, update
|
44
|
+
# is a DOM element for the div from which the
|
45
|
+
# innerHTML is replaced.
|
46
|
+
# <tt>:on_show</tt>:: Like on_hide, only now the expression is called
|
47
|
+
# then the div is shown.
|
48
|
+
# <tt>:after_update_element</tt>:: A Javascript expression that is called when the
|
49
|
+
# user has selected one of the proposed values.
|
50
|
+
# The expression should take two variables: element and value.
|
51
|
+
# Element is a DOM element for the field, value
|
52
|
+
# is the value selected by the user.
|
53
|
+
# <tt>:select</tt>:: Pick the class of the element from which the value for
|
54
|
+
# insertion should be extracted. If this is not specified,
|
55
|
+
# the entire element is used.
|
56
|
+
# <tt>:method</tt>:: Specifies the HTTP verb to use when the autocompletion
|
57
|
+
# request is made. Defaults to POST.
|
58
|
+
def auto_complete_field(field_id, options = {})
|
59
|
+
function = "var #{field_id}_auto_completer = new Ajax.Autocompleter("
|
60
|
+
function << "'#{field_id}', "
|
61
|
+
function << "'" + (options[:update] || "#{field_id}_auto_complete") + "', "
|
62
|
+
function << "'#{url_for(options[:url])}'"
|
63
|
+
|
64
|
+
js_options = {}
|
65
|
+
js_options[:tokens] = array_or_string_for_javascript(options[:tokens]) if options[:tokens]
|
66
|
+
js_options[:callback] = "function(element, value) { return #{options[:with]} }" if options[:with]
|
67
|
+
js_options[:indicator] = "'#{options[:indicator]}'" if options[:indicator]
|
68
|
+
js_options[:select] = "'#{options[:select]}'" if options[:select]
|
69
|
+
js_options[:paramName] = "'#{options[:param_name]}'" if options[:param_name]
|
70
|
+
js_options[:frequency] = "#{options[:frequency]}" if options[:frequency]
|
71
|
+
js_options[:method] = "'#{options[:method].to_s}'" if options[:method]
|
72
|
+
|
73
|
+
{ :after_update_element => :afterUpdateElement,
|
74
|
+
:on_show => :onShow, :on_hide => :onHide, :min_chars => :minChars }.each do |k,v|
|
75
|
+
js_options[v] = options[k] if options[k]
|
76
|
+
end
|
77
|
+
|
78
|
+
function << (', ' + options_for_javascript(js_options) + ')')
|
79
|
+
|
80
|
+
javascript_tag(function)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Use this method in your view to generate a return for the AJAX autocomplete requests.
|
84
|
+
#
|
85
|
+
# Example action:
|
86
|
+
#
|
87
|
+
# def auto_complete_for_item_title
|
88
|
+
# @items = Item.find(:all,
|
89
|
+
# :conditions => [ 'LOWER(description) LIKE ?',
|
90
|
+
# '%' + request.raw_post.downcase + '%' ])
|
91
|
+
# render :inline => "<%= auto_complete_result(@items, 'description') %>"
|
92
|
+
# end
|
93
|
+
#
|
94
|
+
# The auto_complete_result can of course also be called from a view belonging to the
|
95
|
+
# auto_complete action if you need to decorate it further.
|
96
|
+
def auto_complete_result(entries, field, phrase = nil)
|
97
|
+
return unless entries
|
98
|
+
items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) }
|
99
|
+
content_tag("ul", items.uniq)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Wrapper for text_field with added AJAX autocompletion functionality.
|
103
|
+
#
|
104
|
+
# In your controller, you'll need to define an action called
|
105
|
+
# auto_complete_for to respond the AJAX calls,
|
106
|
+
#
|
107
|
+
def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
|
108
|
+
(completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
|
109
|
+
text_field(object, method, tag_options) +
|
110
|
+
content_tag("div", "", :id => "#{object}_#{method}_auto_complete", :class => "auto_complete") +
|
111
|
+
auto_complete_field("#{object}_#{method}", { :url => { :action => "auto_complete_for_#{object}_#{method}" } }.update(completion_options))
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
def auto_complete_stylesheet
|
116
|
+
content_tag('style', <<-EOT, :type => Mime::CSS)
|
117
|
+
div.auto_complete {
|
118
|
+
width: 350px;
|
119
|
+
background: #fff;
|
120
|
+
}
|
121
|
+
div.auto_complete ul {
|
122
|
+
border:1px solid #888;
|
123
|
+
margin:0;
|
124
|
+
padding:0;
|
125
|
+
width:100%;
|
126
|
+
list-style-type:none;
|
127
|
+
}
|
128
|
+
div.auto_complete ul li {
|
129
|
+
margin:0;
|
130
|
+
padding:3px;
|
131
|
+
}
|
132
|
+
div.auto_complete ul li.selected {
|
133
|
+
background-color: #ffb;
|
134
|
+
}
|
135
|
+
div.auto_complete ul strong.highlight {
|
136
|
+
color: #800;
|
137
|
+
margin:0;
|
138
|
+
padding:0;
|
139
|
+
}
|
140
|
+
EOT
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
ActionController::Base.helper AutoCompleteMacrosHelper
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/test_helper'))
|
2
|
+
|
3
|
+
class AutoCompleteTest < Test::Unit::TestCase
|
4
|
+
include AutoComplete
|
5
|
+
include AutoCompleteMacrosHelper
|
6
|
+
|
7
|
+
include ActionView::Helpers::UrlHelper
|
8
|
+
include ActionView::Helpers::TagHelper
|
9
|
+
include ActionView::Helpers::TextHelper
|
10
|
+
include ActionView::Helpers::FormHelper
|
11
|
+
include ActionView::Helpers::CaptureHelper
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@controller = Class.new do
|
15
|
+
def url_for(options)
|
16
|
+
url = "http://www.example.com/"
|
17
|
+
url << options[:action].to_s if options and options[:action]
|
18
|
+
url
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@controller = @controller.new
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
def test_auto_complete_field
|
26
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {})\n//]]>\n</script>),
|
27
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" });
|
28
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:','})\n//]]>\n</script>),
|
29
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => ',');
|
30
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:[',']})\n//]]>\n</script>),
|
31
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => [',']);
|
32
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {minChars:3})\n//]]>\n</script>),
|
33
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :min_chars => 3);
|
34
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {onHide:function(element, update){alert('me');}})\n//]]>\n</script>),
|
35
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :on_hide => "function(element, update){alert('me');}");
|
36
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {frequency:2})\n//]]>\n</script>),
|
37
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :frequency => 2);
|
38
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {afterUpdateElement:function(element,value){alert('You have chosen: '+value)}})\n//]]>\n</script>),
|
39
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" },
|
40
|
+
:after_update_element => "function(element,value){alert('You have chosen: '+value)}");
|
41
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {paramName:'huidriwusch'})\n//]]>\n</script>),
|
42
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :param_name => 'huidriwusch');
|
43
|
+
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {method:'get'})\n//]]>\n</script>),
|
44
|
+
auto_complete_field("some_input", :url => { :action => "autocomplete" }, :method => :get);
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_auto_complete_result
|
48
|
+
result = [ { :title => 'test1' }, { :title => 'test2' } ]
|
49
|
+
assert_equal %(<ul><li>test1</li><li>test2</li></ul>),
|
50
|
+
auto_complete_result(result, :title)
|
51
|
+
assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li><li>t<strong class=\"highlight\">est</strong>2</li></ul>),
|
52
|
+
auto_complete_result(result, :title, "est")
|
53
|
+
|
54
|
+
resultuniq = [ { :title => 'test1' }, { :title => 'test1' } ]
|
55
|
+
assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li></ul>),
|
56
|
+
auto_complete_result(resultuniq, :title, "est")
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_text_field_with_auto_complete
|
60
|
+
assert_match %(<style type="text/css">),
|
61
|
+
text_field_with_auto_complete(:message, :recipient)
|
62
|
+
|
63
|
+
assert_dom_equal %(<input id=\"message_recipient\" name=\"message[recipient]\" size=\"30\" type=\"text\" /><div class=\"auto_complete\" id=\"message_recipient_auto_complete\"></div><script type=\"text/javascript\">\n//<![CDATA[\nvar message_recipient_auto_completer = new Ajax.Autocompleter('message_recipient', 'message_recipient_auto_complete', 'http://www.example.com/auto_complete_for_message_recipient', {})\n//]]>\n</script>),
|
64
|
+
text_field_with_auto_complete(:message, :recipient, {}, :skip_style => true)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: innetra-auto_complete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Torres
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-17 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rails AutoComplete Plugin Gem Version
|
17
|
+
email: mexpolk@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- lib/auto_complete.rb
|
25
|
+
- lib/auto_complete_macros_helper.rb
|
26
|
+
files:
|
27
|
+
- Manifest
|
28
|
+
- README
|
29
|
+
- auto_complete.gemspec
|
30
|
+
- test/auto_complete_test.rb
|
31
|
+
- Rakefile
|
32
|
+
- init.rb
|
33
|
+
- lib/auto_complete.rb
|
34
|
+
- lib/auto_complete_macros_helper.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/innetra/auto_complete
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options:
|
39
|
+
- --line-numbers
|
40
|
+
- --inline-source
|
41
|
+
- --title
|
42
|
+
- Auto_complete
|
43
|
+
- --main
|
44
|
+
- README
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "1.2"
|
58
|
+
version:
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project: auto_complete
|
62
|
+
rubygems_version: 1.2.0
|
63
|
+
signing_key:
|
64
|
+
specification_version: 2
|
65
|
+
summary: Rails AutoComplete Plugin Gem Version
|
66
|
+
test_files:
|
67
|
+
- test/auto_complete_test.rb
|