simple_autocomplete 0.3.4 → 0.3.5
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.markdown +11 -4
- data/VERSION +1 -1
- data/lib/simple_autocomplete.rb +12 -31
- data/simple_autocomplete.gemspec +2 -2
- data/spec/simple_autocomplete_spec.rb +23 -5
- metadata +3 -3
data/README.markdown
CHANGED
@@ -29,11 +29,17 @@ and sorts by the autocomplete field.
|
|
29
29
|
`autocomplete_for` takes a third parameter, an options hash which is used in the find:
|
30
30
|
|
31
31
|
autocomplete_for :user, :name, :limit => 15, :order => 'created_at DESC'
|
32
|
-
|
33
|
-
With
|
32
|
+
|
33
|
+
With :match option you can filter for different columns
|
34
|
+
|
35
|
+
# find and order with firstname/lastname
|
36
|
+
# return full_name of records for auto-completion
|
37
|
+
autocomplete_for :user, :full_name, :match => [:firstname, :lastname]
|
38
|
+
|
39
|
+
With a block you can generate any output you need (ERB allowed):
|
34
40
|
|
35
41
|
autocomplete_for :post, :title do |items|
|
36
|
-
items.map{|item| "#{item.title} -- #{item.id}"}.join("\n")
|
42
|
+
items.map{|item| "#{item.title} -- #{item.id} <%= Time.now %>"}.join("\n")
|
37
43
|
end
|
38
44
|
|
39
45
|
The items passed into the block is an ActiveRecord scope allowing further scopes to be chained:
|
@@ -91,8 +97,9 @@ Authors
|
|
91
97
|
Inspired by DHH`s 'obstrusive' autocomplete_plugin.
|
92
98
|
|
93
99
|
###Contributors (alphabetical)
|
94
|
-
- [Bryan Ash](http://bryan-ash.blogspot.com
|
100
|
+
- [Bryan Ash](http://bryan-ash.blogspot.com)
|
95
101
|
- [David Leal](http://github.com/david)
|
102
|
+
- [mlessard](http://github.com/mlessard)
|
96
103
|
- [Splendeo](http://www.splendeo.es)
|
97
104
|
|
98
105
|
[Michael Grosser](http://pragmatig.wordpress.com)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.5
|
data/lib/simple_autocomplete.rb
CHANGED
@@ -2,40 +2,20 @@ module SimpleAutocomplete
|
|
2
2
|
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
|
3
3
|
end
|
4
4
|
|
5
|
-
#
|
6
|
-
#
|
7
|
-
# # Controller
|
8
|
-
# class BlogController < ApplicationController
|
9
|
-
# autocomplete_for :post, :title
|
10
|
-
# end
|
11
|
-
#
|
12
|
-
# # View
|
13
|
-
# <%= text_field :post, title, :class => 'autocomplete', 'data-autocomplete-url'=>autocomplete_for_post_title_posts_path %>
|
14
|
-
#
|
15
|
-
# #routes.rb
|
16
|
-
# map.resources :users, :collection => { :autocomplete_for_user_name => :get}
|
17
|
-
#
|
18
|
-
# # Options
|
19
|
-
# By default, autocomplete_for limits the results to 10 entries,
|
20
|
-
# and sorts by the given field.
|
21
|
-
#
|
22
|
-
# autocomplete_for takes a third parameter, an options hash to
|
23
|
-
# the find method used to search for the records:
|
24
|
-
#
|
25
|
-
# autocomplete_for :post, :title, :limit => 15, :order => 'created_at DESC'
|
26
|
-
#
|
27
|
-
# # Block
|
28
|
-
# with a block you can generate any output you need(passed into render :inline):
|
29
|
-
# autocomplete_for :post, :title do |items|
|
30
|
-
# items.map{|item| "#{item.title} -- #{item.id}"}.join("\n")
|
31
|
-
# end
|
5
|
+
# see Readme for details
|
32
6
|
class ActionController::Base
|
33
7
|
def self.autocomplete_for(object, method, options = {}, &block)
|
8
|
+
options = options.dup
|
34
9
|
define_method("autocomplete_for_#{object}_#{method}") do
|
10
|
+
methods = options.delete(:match) || [*method]
|
11
|
+
condition = methods.map{|m| "LOWER(#{m}) LIKE ?"} * " OR "
|
12
|
+
values = methods.map{|m| "%#{params[:q].to_s.downcase}%"}
|
13
|
+
conditions = [condition, *values]
|
14
|
+
|
35
15
|
model = object.to_s.camelize.constantize
|
36
16
|
find_options = {
|
37
|
-
:conditions =>
|
38
|
-
:order => "#{
|
17
|
+
:conditions => conditions,
|
18
|
+
:order => "#{methods.first} ASC",
|
39
19
|
:limit => 10
|
40
20
|
}.merge!(options)
|
41
21
|
|
@@ -44,7 +24,7 @@ class ActionController::Base
|
|
44
24
|
out = if block_given?
|
45
25
|
instance_exec @items, &block
|
46
26
|
else
|
47
|
-
%Q[<%= @items.map {|item| h(item.#{
|
27
|
+
%Q[<%= @items.map {|item| h(item.#{methods.first})}.uniq.join("\n")%>]
|
48
28
|
end
|
49
29
|
render :inline => out
|
50
30
|
end
|
@@ -56,6 +36,7 @@ end
|
|
56
36
|
# -> the auto_user_name field will be resolved to a User, using User.find_by_autocomplete_name(value)
|
57
37
|
# -> Post has autocomplete_for('user','name')
|
58
38
|
# -> User has find_by_autocomplete('name')
|
39
|
+
# see Readme for more details
|
59
40
|
class ActiveRecord::Base
|
60
41
|
def self.autocomplete_for(model, attribute, options={})
|
61
42
|
name = options[:name] || model.to_s.underscore
|
@@ -86,4 +67,4 @@ class ActiveRecord::Base
|
|
86
67
|
self.first(:conditions => [ "LOWER(#{attr}) = ?", value.to_s.downcase ])
|
87
68
|
end
|
88
69
|
end
|
89
|
-
end
|
70
|
+
end
|
data/simple_autocomplete.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{simple_autocomplete}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Michael Grosser"]
|
12
|
-
s.date = %q{2010-05-
|
12
|
+
s.date = %q{2010-05-12}
|
13
13
|
s.email = %q{grosser.michael@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.markdown"
|
@@ -24,11 +24,7 @@ describe 'Controller extensions' do
|
|
24
24
|
class UserAddress < ActiveRecord::Base
|
25
25
|
set_table_name :users
|
26
26
|
end
|
27
|
-
|
28
|
-
UserAddress.should_receive(:scoped).with do |options|
|
29
|
-
options[:conditions] == ['LOWER(full_name) LIKE ?','%hans%']
|
30
|
-
end
|
31
|
-
|
27
|
+
UserAddress.should_receive(:scoped).with hash_including(:conditions => ['LOWER(full_name) LIKE ?','%hans%'])
|
32
28
|
@c.stub!(:params).and_return :q=>'Hans'
|
33
29
|
UsersController.autocomplete_for(:user_address,:full_name)
|
34
30
|
@c.autocomplete_for_user_address_full_name
|
@@ -56,6 +52,28 @@ describe 'Controller extensions' do
|
|
56
52
|
@c.autocomplete_for_user_name
|
57
53
|
end
|
58
54
|
end
|
55
|
+
|
56
|
+
describe "autocomplete with :match" do
|
57
|
+
before do
|
58
|
+
UsersController.autocomplete_for(:user, :name, :match => [:full_name, :name])
|
59
|
+
end
|
60
|
+
|
61
|
+
it "renders the items inline with method" do
|
62
|
+
@c.should_receive(:render).with {|hash| hash[:inline] =~ /@items.map \{|item| h(item.full_name)\}.uniq.join(\'\n\')/}
|
63
|
+
@c.autocomplete_for_user_name(:match => [:full_name, :name])
|
64
|
+
end
|
65
|
+
|
66
|
+
it "orders ASC by match" do
|
67
|
+
User.should_receive(:scoped).with(hash_including(:order => 'full_name ASC'))
|
68
|
+
@c.autocomplete_for_user_name(:match => [:full_name, :name])
|
69
|
+
end
|
70
|
+
|
71
|
+
it "finds by match" do
|
72
|
+
@c.stub!(:params).and_return :q=>'Hans'
|
73
|
+
User.should_receive(:scoped).with(hash_including(:conditions => ["LOWER(full_name) LIKE ? OR LOWER(name) LIKE ?", "%hans%", "%hans%"]))
|
74
|
+
@c.autocomplete_for_user_name(:match => [:full_name, :name])
|
75
|
+
end
|
76
|
+
end
|
59
77
|
|
60
78
|
describe "autocomplete using blocks" do
|
61
79
|
it "evaluates the block" do
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 3
|
8
|
-
-
|
9
|
-
version: 0.3.
|
8
|
+
- 5
|
9
|
+
version: 0.3.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Grosser
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-12 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|