jrails_auto_complete 0.2.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/CHANGELOG ADDED
@@ -0,0 +1,26 @@
1
+ HEAD [2010-05-08]
2
+ ====================
3
+
4
+ Features
5
+
6
+ Enhancements
7
+
8
+ Changes
9
+
10
+ Bugfixes
11
+
12
+ 0.2.0 [2010-05-08]
13
+ ====================
14
+
15
+ Features
16
+ * named scope items filtering
17
+ Enhancements
18
+ * gem available
19
+ * documentation with yard
20
+ Bugfixes
21
+ * fix gem initialization in rails
22
+
23
+ 0.1.0 [2010-02-03]
24
+ ====================
25
+
26
+ 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,81 @@
1
+ h1. jRails autocomplete
2
+
3
+ Auto Completion plugin using jRails[1] and jQuery-UI[2]; 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 shure 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/jrails_auto_complete/issues":http://github.com/michelefranzin/jrails_auto_complete/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. "JRails @ GitHub":http://github.com/aaronchi/jrails
76
+
77
+ fn2. "jQuery-UI":http://jqueryui.com/
78
+
79
+ h2. Copyright
80
+
81
+ Copyright (c) 2010 Michele Franzin. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jrails_auto_complete"
8
+ gem.summary = "jRails autocomplete using jQuery-UI"
9
+ gem.description = "Adds autocomplete support to jRails, using jQuery-UI; it " +
10
+ "works similarly to the original rails autocomplete plugin, but " +
11
+ "generating unobtrusive javascript."
12
+ gem.email = "michele.franzin@gmail.com"
13
+ #gem.homepage = "http://github.com/michelefranzin/jrails_auto_complete"
14
+ gem.authors = ["Michele Franzin"]
15
+
16
+ gem.add_development_dependency "yard", ">= 0.5.4"
17
+ gem.add_dependency('rails', '>= 2.1')
18
+ gem.add_dependency('jrails', '>= 0.6')
19
+ gem.files.exclude '*install.rb'
20
+ gem.files.exclude '.gitignore'
21
+ gem.files.exclude 'javascripts/*'
22
+ end
23
+ Jeweler::GemcutterTasks.new
24
+ rescue LoadError
25
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
+ end
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ begin
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+ rescue LoadError
43
+ task :rcov do
44
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
45
+ end
46
+ end
47
+
48
+ task :test => :check_dependencies
49
+
50
+ task :default => :test
51
+
52
+ begin
53
+ require 'yard'
54
+ YARD::Rake::YardocTask.new
55
+ rescue LoadError
56
+ task :yardoc do
57
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
58
+ end
59
+ end
60
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ include 'rails/init'
@@ -0,0 +1,44 @@
1
+ module JrailsAutoComplete
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 JrailsAutoCompleteFormBuilderHelper
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,83 @@
1
+ module JrailsAutoCompleteMacrosHelper
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>:delay</tt>:: The delay in milliseconds the Autocomplete waits after
19
+ # a keystroke to activate itself. A zero-delay makes sense
20
+ # for local data (more responsive), but can produce a lot
21
+ # of load for remote data, while being less responsive.
22
+ # <tt>:min_length</tt>:: The minimum number of characters a user has to type
23
+ # before the Autocomplete activates. Zero is useful for
24
+ # local data with just a few items. Should be increased
25
+ # when there are a lot of items, where a single character
26
+ # would match a few thousand items.
27
+ # <tt>:open</tt>:: Before a request (source-option) is started, after
28
+ # minLength and delay are met. Can be canceled (return false),
29
+ # then no request will be started and no items suggested.
30
+ # <tt>:focus</tt>:: Before focus is moved to an item (not selecting), ui.item
31
+ # refers to the focused item. The default action of focus is
32
+ # to replace the text field's value with the value of the focused
33
+ # item. Canceling this event prevents the value from being updated,
34
+ # but does not prevent the menu item from being focused.
35
+ # <tt>:select</tt>:: Triggered when an item is selected from the menu; ui.item
36
+ # refers to the selected item. The default action of select is
37
+ # to replace the text field's value with the value of the
38
+ # selected item. Canceling this event prevents the value from
39
+ # being updated, but does not prevent the menu from closing.
40
+ # <tt>:close</tt>:: When the list is hidden - doesn't have to occur together
41
+ # with a change.
42
+ # <tt>:change</tt>:: After an item was selected; ui.item refers to the selected
43
+ # item. Always triggered after the close event.
44
+ def auto_complete_field(field_id, options = {})
45
+ js_options = {}
46
+ js_options[:source] = "'#{url_for(options[:source])}'"
47
+ js_options[:update] = "'" + (options[:update] || "#{field_id}_auto_complete") + "'"
48
+
49
+ {:min_length => :minLength, :delay => :delay}.each do |k, v|
50
+ js_options[v] = options[k] if options[k]
51
+ end
52
+
53
+ [:search, :open, :focus, :select, :close, :change].each do |k|
54
+ js_options[k] = "function(event,ui){#{options[k]}}" if options[k]
55
+ end
56
+
57
+ function = "#{ActionView::Helpers::PrototypeHelper::JQUERY_VAR}('##{field_id}').autocomplete("
58
+ function << options_for_javascript(js_options) + ')'
59
+
60
+ javascript_tag(function)
61
+ end
62
+
63
+ def auto_complete_result(entries, field, phrase = nil)
64
+ return unless entries
65
+ unique_entries= entries.uniq.map do |entry|
66
+ {'id' => entry[:id],
67
+ 'value' => h(entry[field]),
68
+ 'label' => phrase ? highlight(entry.send(field), phrase) : h(entry[field])}
69
+ end
70
+ unique_entries.to_json
71
+ end
72
+
73
+ def auto_complete_for(object, method, options = {})
74
+ content_tag('div', '', :id => "#{object}_#{method}_auto_complete") +
75
+ auto_complete_field("#{object}_#{method}", { :source => { :action => "auto_complete_for_#{object}_#{method}" } }.update(options))
76
+ end
77
+
78
+ def text_field_with_auto_complete(object, method, tag_options = {}, completion_options = {})
79
+ text_field(object, method, tag_options) +
80
+ auto_complete_for(object, method, completion_options)
81
+ end
82
+
83
+ end
@@ -0,0 +1,12 @@
1
+ module JrailsAutoCompleteTagHelper
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 jrails_auto_complete])
2
+ require File.join(File.dirname(__FILE__), *%w[.. lib jrails_auto_complete_macros_helper])
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib jrails_auto_complete_form_builder_helper])
4
+ require File.join(File.dirname(__FILE__), *%w[.. lib jrails_auto_complete_tag_helper])
5
+
6
+ ActionController::Base.send :include, JrailsAutoComplete
7
+ ActionController::Base.helper JrailsAutoCompleteMacrosHelper
8
+ ActionView::Helpers::FormBuilder.send :include, JrailsAutoCompleteFormBuilderHelper
9
+ ActionView::Helpers::TagHelper.send :include, JrailsAutoCompleteTagHelper
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :jrails_auto_complete do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test/unit'
2
+
3
+ class JrailsAutoCompleteTest < 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,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jrails_auto_complete
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - Michele Franzin
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-08 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: yard
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 5
30
+ - 4
31
+ version: 0.5.4
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rails
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 1
44
+ version: "2.1"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: jrails
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ - 6
57
+ version: "0.6"
58
+ type: :runtime
59
+ version_requirements: *id003
60
+ description: Adds autocomplete support to jRails, using jQuery-UI; it works similarly to the original rails autocomplete plugin, but generating unobtrusive javascript.
61
+ email: michele.franzin@gmail.com
62
+ executables: []
63
+
64
+ extensions: []
65
+
66
+ extra_rdoc_files:
67
+ - LICENSE
68
+ - README.textile
69
+ files:
70
+ - CHANGELOG
71
+ - LICENSE
72
+ - README.textile
73
+ - Rakefile
74
+ - VERSION
75
+ - init.rb
76
+ - lib/jrails_auto_complete.rb
77
+ - lib/jrails_auto_complete_form_builder_helper.rb
78
+ - lib/jrails_auto_complete_macros_helper.rb
79
+ - lib/jrails_auto_complete_tag_helper.rb
80
+ - rails/init.rb
81
+ - tasks/jrails_auto_complete_tasks.rake
82
+ - test/jrails_auto_complete_test.rb
83
+ has_rdoc: true
84
+ homepage:
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options:
89
+ - --charset=UTF-8
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ requirements: []
107
+
108
+ rubyforge_project:
109
+ rubygems_version: 1.3.6
110
+ signing_key:
111
+ specification_version: 3
112
+ summary: jRails autocomplete using jQuery-UI
113
+ test_files:
114
+ - test/jrails_auto_complete_test.rb