jquery_autocomplete 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,48 @@
1
+ HEAD [2010-08-28]
2
+ ====================
3
+
4
+ Features
5
+
6
+ Enhancements
7
+
8
+ Changes
9
+
10
+ Bugfixes
11
+
12
+ 0.3.0 [2010-08-28]
13
+ ====================
14
+
15
+ Changes
16
+ * renamed plugin jquery_autocomplete
17
+
18
+ 0.2.2 [2010-08-28]
19
+ ====================
20
+
21
+ Changes
22
+ * no more jRails gem dependancy
23
+
24
+ 0.2.1 [2010-08-27]
25
+ ====================
26
+
27
+ Features
28
+ * added :disabled option
29
+ Enhancements
30
+ * avoid a Rails 3 deprecation warning
31
+ Changes
32
+ * updated jquery-ui to v1.8.4
33
+
34
+ 0.2.0 [2010-05-08]
35
+ ====================
36
+
37
+ Features
38
+ * named scope items filtering
39
+ Enhancements
40
+ * gem available
41
+ * documentation with yard
42
+ Bugfixes
43
+ * fix gem initialization in rails
44
+
45
+ 0.1.0 [2010-02-03]
46
+ ====================
47
+
48
+ First release.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Michele Franzin
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.textile ADDED
@@ -0,0 +1,79 @@
1
+ h1. jQuery autocomplete
2
+
3
+ Rails Auto Completion plugin using jQuery-UI[1] widget; it works similarly to the original rails auto complete, but generating unobtrusive javascript. Additional features:
4
+
5
+ * named scope items filtering, based on a customized list of autocomplete options.
6
+
7
+ *WARNING NOTE*: This is an active work in progress - currently only used on few live projects right now. Use at your own discretion.
8
+
9
+ h2. Usage example
10
+
11
+ In the controller you can use the macro function as such
12
+
13
+ <pre><code>class ExampleController < ApplicationController
14
+ auto_complete_for :fruit, :name
15
+ ...
16
+ end
17
+ </code></pre>
18
+
19
+ In your views:
20
+
21
+ <pre><code><%= text_field_with_auto_complete :fruit, :name %>
22
+ </code></pre>
23
+
24
+ be sure to include the jquery/jquery-ui javascript in your layout:
25
+
26
+ <pre><code><%= javascript_include_tag 'jquery, 'jquery-ui.min', 'jquery-ui-i18n.min' %>
27
+ </code></pre>
28
+
29
+ h2. Named scopes with auto_complete_for:
30
+
31
+ auto_complete_for now optionally accepts a block that is called with the item list and HTTP parameters when the auto complete AJAX request is received. This block can be used to specify that a named scope be used to generate a customized list of autocomplete options.
32
+
33
+ h3. Example using anonymous scope:
34
+
35
+ <pre><code>auto_complete_for :fruit, :shape do |items, params|
36
+ items.scoped( { :conditions => [ "colour = ?", params['fruit']['color'] ] })
37
+ end
38
+ </code></pre>
39
+
40
+ h3. Example using named scope:
41
+
42
+ Having the model:
43
+
44
+ <pre><code>class Fruit < ActiveRecord::Base
45
+ belongs_to :owner
46
+ named_scope :by_owner,
47
+ lambda { |owner_name| {
48
+ :include => :owner,
49
+ :conditions => [ "owner.name = ?", owner_name ]
50
+ } }
51
+ end
52
+ </code></pre>
53
+
54
+ In the controller you can use the macro function as such
55
+
56
+ <pre><code>auto_complete_for :fruit, :name do | items, params |
57
+ items.by_owner(params['owner'])
58
+ end
59
+ </code></pre>
60
+
61
+ h2. Todo
62
+
63
+ * test, test... test!
64
+
65
+ h2. Found a Bug?
66
+
67
+ Please direct all queries to the issue tracker: "http://github.com/michelefranzin/jquery_autocomplete/issues":http://github.com/michelefranzin/jquery_autocomplete/issues
68
+
69
+ h2. Credits
70
+
71
+ Thanks to Paul Smith for the "original idea":http://github.com/elandesign/formtastic_autocomplete.git and Pat Shaughnessy for "inspirating":http://patshaughnessy.net/repeated_auto_complete me the named scope part.
72
+
73
+ h2. References
74
+
75
+ fn1. "jQuery-UI":http://jqueryui.com/
76
+
77
+ h2. Copyright
78
+
79
+ Copyright (c) 2010 Michele Franzin. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,59 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "jquery_autocomplete"
8
+ gemspec.summary = "rails autocomplete using jQuery-UI widget"
9
+ gemspec.description = "Adds autocomplete support to rails inputs, using jQuery-UI; it " +
10
+ "works similarly to the original rails autocomplete plugin, but " +
11
+ "generating unobtrusive javascript."
12
+ gemspec.email = "michele.franzin@gmail.com"
13
+ gemspec.homepage = "http://github.com/michelefranzin/jquery_autocomplete"
14
+ gemspec.authors = ["Michele Franzin"]
15
+
16
+ gemspec.add_development_dependency 'yard', '>=0.5.4'
17
+ gemspec.add_dependency 'rails', '>=2.1'
18
+ gemspec.files.exclude '*install.rb'
19
+ gemspec.files.exclude '.gitignore'
20
+ gemspec.files.exclude 'javascripts/*'
21
+ end
22
+ Jeweler::GemcutterTasks.new
23
+ rescue LoadError
24
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
25
+ end
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ begin
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ end
41
+ rescue LoadError
42
+ task :rcov do
43
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
44
+ end
45
+ end
46
+
47
+ task :test => :check_dependencies
48
+
49
+ task :default => :test
50
+
51
+ begin
52
+ require 'yard'
53
+ YARD::Rake::YardocTask.new
54
+ rescue LoadError
55
+ task :yardoc do
56
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
57
+ end
58
+ end
59
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ include 'rails/init'
@@ -0,0 +1,44 @@
1
+ module JqueryAutocomplete
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ #
7
+ # Example:
8
+ #
9
+ # # Controller
10
+ # class BlogController < ApplicationController
11
+ # auto_complete_for :post, :title
12
+ # end
13
+ #
14
+ # # View
15
+ # <%= text_field_with_auto_complete :post, title %>
16
+ #
17
+ # By default, auto_complete_for limits the results to 10 entries,
18
+ # and sorts by the given field.
19
+ #
20
+ # auto_complete_for takes a third parameter, an options hash to
21
+ # the find method used to search for the records:
22
+ #
23
+ # auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
24
+ # def auto_complete_for(object_name, method_name, options = {})
25
+ #
26
+ module ClassMethods
27
+ def auto_complete_for(object_name, method_name, options = {})
28
+ self.send(:define_method, "auto_complete_for_#{object_name}_#{method_name}") do
29
+ model = object_name.to_s.camelize.constantize
30
+ full_method_name = "#{model.quoted_table_name}.#{method_name}"
31
+ find_options= {
32
+ :conditions => ["LOWER(#{full_method_name}) LIKE ?", '%' + params['term'].downcase + '%'],
33
+ :order => "#{full_method_name} ASC",
34
+ :limit => 10
35
+ }.merge!(options)
36
+
37
+ @items = model.scoped(find_options)
38
+ @items = yield(@items, params) if block_given?
39
+
40
+ render :inline => "<%= auto_complete_result @items, '#{method_name}', '#{params['term']}' %>"
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ module JqueryAutocompleteFormBuilderHelper
2
+
3
+ def text_field_with_auto_complete(method, options = {}, auto_complete_options = {})
4
+ text_field(method, options) + auto_complete_for(method, auto_complete_options)
5
+ end
6
+
7
+ def auto_complete_for(method, options = {})
8
+ @template.auto_complete_for(@object_name, method, options)
9
+ end
10
+
11
+ end
@@ -0,0 +1,89 @@
1
+ module JqueryAutocompleteMacrosHelper
2
+ # Adds JQuery autocomplete functionality to the text input field with the
3
+ # DOM ID specified by +field_id+.
4
+ #
5
+ # You'll probably want to turn the browser's built-in autocompletion off,
6
+ # so be sure to include an <tt>autocomplete="off"</tt> attribute with your text
7
+ # input field.
8
+ #
9
+ # Required +options+ are:
10
+ # <tt>:source</tt>:: URL to call for autocompletion results
11
+ # in url_for format.
12
+ #
13
+ # Addtional +options+ are:
14
+ # <tt>:update</tt>:: Specifies the DOM ID of the element whose
15
+ # innerHTML should be updated with the autocomplete
16
+ # entries returned by the request.
17
+ # Defaults to <tt>field_id</tt> + '_auto_complete'
18
+ # <tt>:disabled</tt>:: Disables (true) or enables (false) the autocomplete.
19
+ # Can be set when initialising (first creating) the autocomplete.
20
+ # Defaults to <tt>false</tt>.
21
+ # <tt>:delay</tt>:: The delay in milliseconds the Autocomplete waits after
22
+ # a keystroke to activate itself. A zero-delay makes sense
23
+ # for local data (more responsive), but can produce a lot
24
+ # of load for remote data, while being less responsive.
25
+ # Defaults to <tt>300</tt>.
26
+ # <tt>:min_length</tt>:: The minimum number of characters a user has to type
27
+ # before the Autocomplete activates. Zero is useful for
28
+ # local data with just a few items. Should be increased
29
+ # when there are a lot of items, where a single character
30
+ # would match a few thousand items.
31
+ # Defaults to <tt>1</tt>.
32
+ # <tt>:search</tt>:: Before a request (source-option) is started, after
33
+ # minLength and delay are met. Can be canceled (return false),
34
+ # then no request will be started and no items suggested.
35
+ # <tt>:open</tt>:: Triggered when the suggestion menu is opened.
36
+ # <tt>:focus</tt>:: Before focus is moved to an item (not selecting), ui.item
37
+ # refers to the focused item. The default action of focus is
38
+ # to replace the text field's value with the value of the focused
39
+ # item. Canceling this event prevents the value from being updated,
40
+ # but does not prevent the menu item from being focused.
41
+ # <tt>:select</tt>:: Triggered when an item is selected from the menu; ui.item
42
+ # refers to the selected item. The default action of select is
43
+ # to replace the text field's value with the value of the
44
+ # selected item. Canceling this event prevents the value from
45
+ # being updated, but does not prevent the menu from closing.
46
+ # <tt>:close</tt>:: When the list is hidden - doesn't have to occur together
47
+ # with a change.
48
+ # <tt>:change</tt>:: After an item was selected; ui.item refers to the selected
49
+ # item. Always triggered after the close event.
50
+ def auto_complete_field(field_id, options = {})
51
+ js_options = {}
52
+ js_options[:source] = "'#{url_for(options[:source])}'"
53
+ js_options[:update] = "'" + (options[:update] || "#{field_id}_auto_complete") + "'"
54
+
55
+ {:min_length => :minLength, :delay => :delay, :disabled => :disabled}.each do |k, v|
56
+ js_options[v] = options[k] if options[k]
57
+ end
58
+
59
+ [:search, :open, :focus, :select, :close, :change].each do |k|
60
+ js_options[k] = "function(event,ui){#{options[k]}}" if options[k]
61
+ end
62
+
63
+ function = "$('##{field_id}').autocomplete("
64
+ function << options_for_javascript(js_options) + ')'
65
+
66
+ javascript_tag(function)
67
+ end
68
+
69
+ def auto_complete_result(entries, field, phrase = nil)
70
+ return unless entries
71
+ unique_entries= entries.uniq.map do |entry|
72
+ {'id' => entry[:id],
73
+ 'value' => h(entry[field]),
74
+ 'label' => phrase ? highlight(entry.send(field), phrase) : h(entry[field])}
75
+ end
76
+ unique_entries.to_json
77
+ end
78
+
79
+ def auto_complete_for(object, method, options = {})
80
+ content_tag('div', '', :id => "#{object}_#{method}_auto_complete") +
81
+ auto_complete_field("#{object}_#{method}", { :source => { :action => "auto_complete_for_#{object}_#{method}" } }.update(options))
82
+ end
83
+
84
+ def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
85
+ text_field(object, method, tag_options) +
86
+ auto_complete_for(object, method, completion_options)
87
+ end
88
+
89
+ end
@@ -0,0 +1,12 @@
1
+ module JqueryAutocompleteTagHelper
2
+
3
+ def to_auto_complete(options = {})
4
+ send(:add_default_name_and_id, options)
5
+ @template_object.auto_complete_field(options['id'], options)
6
+ end
7
+
8
+ def to_text_field_with_auto_complete(options = {}, text_field_options = {})
9
+ to_input_field_tag('text', text_field_options) + to_auto_complete(options)
10
+ end
11
+
12
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. lib jquery_autocomplete])
2
+ require File.join(File.dirname(__FILE__), *%w[.. lib jquery_autocomplete_macros_helper])
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib jquery_autocomplete_form_builder_helper])
4
+ require File.join(File.dirname(__FILE__), *%w[.. lib jquery_autocomplete_tag_helper])
5
+
6
+ ActionController::Base.send :include, JqueryAutocomplete
7
+ ActionController::Base.helper JqueryAutocompleteMacrosHelper
8
+ ActionView::Helpers::FormBuilder.send :include, JqueryAutocompleteFormBuilderHelper
9
+ ActionView::Helpers::TagHelper.send :include, JqueryAutocompleteTagHelper
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class JqueryAutocompleteTest < Test::Unit::TestCase
4
+ # Replace this with your real tests.
5
+ def test_this_plugin
6
+ flunk
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jquery_autocomplete
3
+ version: !ruby/object:Gem::Version
4
+ hash: 19
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
+ platform: ruby
12
+ authors:
13
+ - Michele Franzin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-28 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yard
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ - 5
33
+ - 4
34
+ version: 0.5.4
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 1
46
+ segments:
47
+ - 2
48
+ - 1
49
+ version: "2.1"
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Adds autocomplete support to rails inputs, using jQuery-UI; it works similarly to the original rails autocomplete plugin, but generating unobtrusive javascript.
53
+ email: michele.franzin@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.textile
61
+ files:
62
+ - CHANGELOG
63
+ - LICENSE
64
+ - README.textile
65
+ - Rakefile
66
+ - VERSION
67
+ - init.rb
68
+ - lib/jquery_autocomplete.rb
69
+ - lib/jquery_autocomplete_form_builder_helper.rb
70
+ - lib/jquery_autocomplete_macros_helper.rb
71
+ - lib/jquery_autocomplete_tag_helper.rb
72
+ - rails/init.rb
73
+ - test/jquery_autocomplete_test.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/michelefranzin/jquery_autocomplete
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: rails autocomplete using jQuery-UI widget
108
+ test_files:
109
+ - test/jquery_autocomplete_test.rb