simple_autocomplete 0.3.5 → 0.3.6
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 +6 -0
- data/VERSION +1 -1
- data/lib/simple_autocomplete.rb +44 -18
- data/simple_autocomplete.gemspec +2 -2
- data/spec/setup_test_model.rb +12 -0
- data/spec/simple_autocomplete_spec.rb +31 -0
- metadata +3 -3
data/README.markdown
CHANGED
@@ -91,6 +91,11 @@ Example for a post with autocompleted user name:
|
|
91
91
|
autocomplete_for(:user, :name, :name=>:creator) #--> f.text_field :auto_creator_name (creator must a an User)
|
92
92
|
end
|
93
93
|
|
94
|
+
class Group
|
95
|
+
has_many :users
|
96
|
+
add_by_autocomplete(:user, :name) #--> f.text_field :add_by_auto_user_name
|
97
|
+
end
|
98
|
+
|
94
99
|
|
95
100
|
Authors
|
96
101
|
=======
|
@@ -100,6 +105,7 @@ Inspired by DHH`s 'obstrusive' autocomplete_plugin.
|
|
100
105
|
- [Bryan Ash](http://bryan-ash.blogspot.com)
|
101
106
|
- [David Leal](http://github.com/david)
|
102
107
|
- [mlessard](http://github.com/mlessard)
|
108
|
+
- [Oliver Azevedo Barnes](http://github.com/oliverbarnes)
|
103
109
|
- [Splendeo](http://www.splendeo.es)
|
104
110
|
|
105
111
|
[Michael Grosser](http://pragmatig.wordpress.com)
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.6
|
data/lib/simple_autocomplete.rb
CHANGED
@@ -31,23 +31,14 @@ class ActionController::Base
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
# Store the value of the autocomplete field as association
|
35
|
-
# autocomplete_for('user','name')
|
36
|
-
# -> the auto_user_name field will be resolved to a User, using User.find_by_autocomplete_name(value)
|
37
|
-
# -> Post has autocomplete_for('user','name')
|
38
|
-
# -> User has find_by_autocomplete('name')
|
39
|
-
# see Readme for more details
|
40
34
|
class ActiveRecord::Base
|
35
|
+
# Store autocomplete field as association
|
36
|
+
# in post.rb: autocomplete_for('user', 'name')
|
37
|
+
# in user.rb add find_by_autocomplete :name
|
38
|
+
# Post.first.auto_user_name=(value) will be resolved to a User, using User.find_by_autocomplete_name(value)
|
41
39
|
def self.autocomplete_for(model, attribute, options={})
|
42
|
-
name = options[:name]
|
43
|
-
|
44
|
-
model = model.to_s.camelize.constantize
|
45
|
-
|
46
|
-
# is the correct finder defined <-> warn users
|
47
|
-
finder = "find_by_autocomplete_#{attribute}"
|
48
|
-
unless model.respond_to? finder
|
49
|
-
raise "#{model} does not respond to #{finder}, maybe you forgot to add auto_complete_for(:#{attribute}) to #{model}?"
|
50
|
-
end
|
40
|
+
model, name = autocomplete_model_and_name(model, options[:name])
|
41
|
+
finder = autocomplete_finder_for(model, attribute)
|
51
42
|
|
52
43
|
#auto_user_name= "Hans"
|
53
44
|
define_method "auto_#{name}_#{attribute}=" do |value|
|
@@ -60,11 +51,46 @@ class ActiveRecord::Base
|
|
60
51
|
end
|
61
52
|
end
|
62
53
|
|
63
|
-
|
54
|
+
# Add association by autocomplete field
|
55
|
+
# in post.rb add_by_autocomplete('user', 'name) for has_many() association, like on github's collaborators search
|
56
|
+
# in user.rb add find_by_autocomplete :name
|
57
|
+
# Post.first.add_bu_auto_user_name=(value) will be resolved to a User, using User.find_by_autocomplete_name(value)
|
58
|
+
def self.add_by_autocomplete(model, attribute, options={})
|
59
|
+
model, name = autocomplete_model_and_name(model, options[:name])
|
60
|
+
finder = autocomplete_finder_for(model, attribute)
|
61
|
+
|
62
|
+
#add_user_by_autocomplete= "Hans"
|
63
|
+
define_method "add_by_auto_#{name}_#{attribute}=" do |value|
|
64
|
+
send(name.pluralize).send('<<', model.send(finder, value)) if value and !value.empty?
|
65
|
+
end
|
66
|
+
|
67
|
+
# avoid method missing error when rendering a form field
|
68
|
+
define_method("add_by_auto_#{name}_#{attribute}"){}
|
69
|
+
end
|
70
|
+
|
71
|
+
def self.find_by_autocomplete(attribute)
|
64
72
|
metaclass = (class << self; self; end)
|
65
|
-
metaclass.send(:define_method, "find_by_autocomplete_#{
|
73
|
+
metaclass.send(:define_method, "find_by_autocomplete_#{attribute}") do |value|
|
66
74
|
return if value.blank?
|
67
|
-
self.first(:conditions => [ "LOWER(#{
|
75
|
+
self.first(:conditions => [ "LOWER(#{attribute}) = ?", value.to_s.downcase ])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def self.autocomplete_model_and_name(model, name)
|
82
|
+
name = name || model.to_s.underscore
|
83
|
+
name = name.to_s
|
84
|
+
model = model.to_s.camelize.constantize
|
85
|
+
[model, name]
|
86
|
+
end
|
87
|
+
|
88
|
+
# is the correct finder defined <-> warn users
|
89
|
+
def self.autocomplete_finder_for(model, attribute)
|
90
|
+
finder = "find_by_autocomplete_#{attribute}"
|
91
|
+
unless model.respond_to? finder
|
92
|
+
raise "#{model} does not respond to #{finder}, maybe you forgot to add auto_complete_for(:#{attribute}) to #{model}?"
|
68
93
|
end
|
94
|
+
finder
|
69
95
|
end
|
70
96
|
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.6"
|
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-
|
12
|
+
s.date = %q{2010-06-28}
|
13
13
|
s.email = %q{grosser.michael@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.markdown"
|
data/spec/setup_test_model.rb
CHANGED
@@ -18,6 +18,11 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
18
18
|
create_table :posts do |t|
|
19
19
|
t.integer :author_id
|
20
20
|
end
|
21
|
+
|
22
|
+
create_table :tags do |t|
|
23
|
+
t.integer :post_id
|
24
|
+
t.string :name
|
25
|
+
end
|
21
26
|
end
|
22
27
|
|
23
28
|
class User < ActiveRecord::Base
|
@@ -28,7 +33,14 @@ class Author < ActiveRecord::Base
|
|
28
33
|
find_by_autocomplete :name
|
29
34
|
end
|
30
35
|
|
36
|
+
class Tag < ActiveRecord::Base
|
37
|
+
belongs_to :post
|
38
|
+
find_by_autocomplete :name
|
39
|
+
end
|
40
|
+
|
31
41
|
class Post < ActiveRecord::Base
|
32
42
|
belongs_to :author
|
43
|
+
has_many :tags
|
33
44
|
autocomplete_for :author, :name
|
45
|
+
add_by_autocomplete :tag, :name
|
34
46
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
|
1
2
|
require "spec/spec_helper"
|
2
3
|
|
3
4
|
describe SimpleAutocomplete do
|
@@ -176,4 +177,34 @@ describe 'Model extensions' do
|
|
176
177
|
Author.find_by_autocomplete_name('bob').should == nil
|
177
178
|
end
|
178
179
|
end
|
180
|
+
|
181
|
+
describe "add_by_auto_{name}_{attribute}" do
|
182
|
+
it "is always nil when associated is not present" do
|
183
|
+
Post.new.add_by_auto_tag_name.should == nil
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
describe "add_by_auto_{name}_{attribute}=" do
|
188
|
+
before do
|
189
|
+
Tag.delete_all
|
190
|
+
Tag.create!(:name => 'economics')
|
191
|
+
@tag = Tag.create!(:name => 'politics')
|
192
|
+
Tag.create!(:name => '')
|
193
|
+
end
|
194
|
+
|
195
|
+
it "does nothing when blank is set" do
|
196
|
+
p = Post.new(:add_by_auto_tag_name => '' )
|
197
|
+
p.tags.should be_empty
|
198
|
+
end
|
199
|
+
|
200
|
+
it "does nothing when nil is net" do
|
201
|
+
p = Post.new(:add_by_auto_tag_name => nil)
|
202
|
+
p.tags.should be_empty
|
203
|
+
end
|
204
|
+
|
205
|
+
it "finds the correct associated and sets it" do
|
206
|
+
p = Post.new(:add_by_auto_tag_name => 'politics')
|
207
|
+
p.tags.should == [@tag]
|
208
|
+
end
|
209
|
+
end
|
179
210
|
end
|
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
|
+
- 6
|
9
|
+
version: 0.3.6
|
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-
|
17
|
+
date: 2010-06-28 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|