simple_autocomplete 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +1 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/simple_autocomplete.rb +12 -17
- data/simple_autocomplete.gemspec +3 -3
- data/spec/simple_autocomplete_spec.rb +23 -2
- metadata +12 -5
data/README.markdown
CHANGED
@@ -10,8 +10,7 @@ As Rails plugin:
|
|
10
10
|
Or As Gem:
|
11
11
|
sudo gem install simple_autocomplete
|
12
12
|
|
13
|
-
Then
|
14
|
-
copy javascripts/css from [example folder](http://github.com/grosser/simple_auto_complete/tree/master/example_js/) OR use your own
|
13
|
+
Then copy javascripts/css from [example folder](http://github.com/grosser/simple_auto_complete/tree/master/example_js/) OR use your own
|
15
14
|
|
16
15
|
|
17
16
|
Examples
|
data/Rakefile
CHANGED
@@ -15,5 +15,5 @@ begin
|
|
15
15
|
|
16
16
|
Jeweler::GemcutterTasks.new
|
17
17
|
rescue LoadError
|
18
|
-
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install
|
18
|
+
puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install jeweler"
|
19
19
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/simple_autocomplete.rb
CHANGED
@@ -57,38 +57,33 @@ end
|
|
57
57
|
# -> Post has autocomplete_for('user','name')
|
58
58
|
# -> User has find_by_autocomplete('name')
|
59
59
|
class ActiveRecord::Base
|
60
|
-
def self.autocomplete_for(model,
|
60
|
+
def self.autocomplete_for(model, attribute, options={})
|
61
61
|
name = options[:name] || model.to_s.underscore
|
62
62
|
name = name.to_s
|
63
63
|
model = model.to_s.camelize.constantize
|
64
64
|
|
65
65
|
# is the correct finder defined <-> warn users
|
66
|
-
finder = "find_by_autocomplete_#{
|
66
|
+
finder = "find_by_autocomplete_#{attribute}"
|
67
67
|
unless model.respond_to? finder
|
68
|
-
raise "#{model} does not respond to #{finder}, maybe you forgot to add auto_complete_for(:#{
|
68
|
+
raise "#{model} does not respond to #{finder}, maybe you forgot to add auto_complete_for(:#{attribute}) to #{model}?"
|
69
69
|
end
|
70
70
|
|
71
71
|
#auto_user_name= "Hans"
|
72
|
-
define_method
|
73
|
-
|
74
|
-
send("#{name}=", found)
|
72
|
+
define_method "auto_#{name}_#{attribute}=" do |value|
|
73
|
+
send "#{name}=", model.send(finder, value)
|
75
74
|
end
|
76
75
|
|
77
76
|
#auto_user_name
|
78
|
-
define_method
|
79
|
-
send(name)
|
77
|
+
define_method "auto_#{name}_#{attribute}" do
|
78
|
+
send(name).try(:send, attribute).to_s
|
80
79
|
end
|
81
80
|
end
|
82
81
|
|
83
82
|
def self.find_by_autocomplete(attr)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
:conditions => [ "LOWER(#{attr}) = ?", value.to_s.downcase ]
|
90
|
-
)
|
91
|
-
end
|
92
|
-
end_eval
|
83
|
+
metaclass = (class << self; self; end)
|
84
|
+
metaclass.send(:define_method, "find_by_autocomplete_#{attr}") do |value|
|
85
|
+
return if value.blank?
|
86
|
+
self.first(:conditions => [ "LOWER(#{attr}) = ?", value.to_s.downcase ])
|
87
|
+
end
|
93
88
|
end
|
94
89
|
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.1"
|
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{
|
12
|
+
s.date = %q{2010-03-23}
|
13
13
|
s.email = %q{grosser.michael@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.markdown"
|
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
s.homepage = %q{http://github.com/grosser/simple_autocomplete}
|
34
34
|
s.rdoc_options = ["--charset=UTF-8"]
|
35
35
|
s.require_paths = ["lib"]
|
36
|
-
s.rubygems_version = %q{1.3.
|
36
|
+
s.rubygems_version = %q{1.3.6}
|
37
37
|
s.summary = %q{Rails: Simple, customizable, unobstrusive - auto complete}
|
38
38
|
s.test_files = [
|
39
39
|
"spec/spec_helper.rb",
|
@@ -65,10 +65,16 @@ describe 'Controller extensions' do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
it "passes found items to the block" do
|
68
|
+
User.delete_all
|
69
|
+
u1 = User.create!(:name => 'xxx')
|
70
|
+
User.create!(:name => 'zzz')
|
71
|
+
u3 = User.create!(:name => 'xxx')
|
72
|
+
|
68
73
|
UsersController.autocomplete_for(:user, :name) do |items|
|
69
|
-
items.should
|
74
|
+
items.to_a.should =~ [u1, u3]
|
70
75
|
end
|
71
|
-
|
76
|
+
|
77
|
+
@c.stub!(:params).and_return :q=>'xxx'
|
72
78
|
@c.autocomplete_for_user_name
|
73
79
|
end
|
74
80
|
|
@@ -126,4 +132,19 @@ describe 'Model extensions' do
|
|
126
132
|
p.author.should == @pete
|
127
133
|
end
|
128
134
|
end
|
135
|
+
|
136
|
+
describe "find_by_autocomplete_{attribute}" do
|
137
|
+
before do
|
138
|
+
Author.delete_all
|
139
|
+
@author = Author.create!(:name => 'john')
|
140
|
+
end
|
141
|
+
|
142
|
+
it "finds when possible" do
|
143
|
+
Author.find_by_autocomplete_name('john').should == @author
|
144
|
+
end
|
145
|
+
|
146
|
+
it "returns nil when nothing was found" do
|
147
|
+
Author.find_by_autocomplete_name('bob').should == nil
|
148
|
+
end
|
149
|
+
end
|
129
150
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_autocomplete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 1
|
9
|
+
version: 0.3.1
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Michael Grosser
|
@@ -9,7 +14,7 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-23 00:00:00 +01:00
|
13
18
|
default_executable:
|
14
19
|
dependencies: []
|
15
20
|
|
@@ -49,18 +54,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
54
|
requirements:
|
50
55
|
- - ">="
|
51
56
|
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 0
|
52
59
|
version: "0"
|
53
|
-
version:
|
54
60
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
61
|
requirements:
|
56
62
|
- - ">="
|
57
63
|
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
58
66
|
version: "0"
|
59
|
-
version:
|
60
67
|
requirements: []
|
61
68
|
|
62
69
|
rubyforge_project:
|
63
|
-
rubygems_version: 1.3.
|
70
|
+
rubygems_version: 1.3.6
|
64
71
|
signing_key:
|
65
72
|
specification_version: 3
|
66
73
|
summary: "Rails: Simple, customizable, unobstrusive - auto complete"
|