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 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 technicalpickles-jeweler -s http://gems.github.com"
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.0
1
+ 0.3.1
@@ -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, method, options={})
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_#{method}"
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(:#{method}) to #{model}?"
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("auto_#{name}_#{method}=") do |value|
73
- found = model.send(finder, value)
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("auto_#{name}_#{method}") do
79
- send(name) ? send(name).send(method) : ""
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
- class_eval <<-end_eval
85
- def self.find_by_autocomplete_#{attr}(value)
86
- return nil if value.blank?
87
- self.find(
88
- :first,
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
@@ -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.0"
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{2009-12-23}
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.5}
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 == ['xx']
74
+ items.to_a.should =~ [u1, u3]
70
75
  end
71
- User.should_receive(:find).and_return ['xx']
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
- version: 0.3.0
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: 2009-12-23 00:00:00 +01:00
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.5
70
+ rubygems_version: 1.3.6
64
71
  signing_key:
65
72
  specification_version: 3
66
73
  summary: "Rails: Simple, customizable, unobstrusive - auto complete"