simple_autocomplete 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/Readme.md CHANGED
@@ -18,7 +18,7 @@ Examples
18
18
 
19
19
  Controller
20
20
  ----------
21
- By default, `autocomplete_for` limits the results to 10 entries,
21
+ By default, `autocomplete_for` limits the results to 10 entries,<br/>
22
22
  and sorts by the autocomplete field.
23
23
 
24
24
  class UsersController < ApplicationController
@@ -36,6 +36,13 @@ With :match option you can filter for different columns
36
36
  # return full_name of records for auto-completion
37
37
  autocomplete_for :user, :full_name, :match => [:firstname, :lastname]
38
38
 
39
+ With :query and :mask option you can change the generated query
40
+
41
+ # default: "LOWER(full_name) LIKE('%peter%')"
42
+ # now: "full_name LIKE('peter%') AND deleted_at IS NULL"
43
+ autocomplete_for :user, :full_name, :query => "%{field} LIKE(%{query}) AND deleted_at IS NULL", :mask => '%{value}%'
44
+
45
+
39
46
  With a block you can generate any output you need (ERB allowed):
40
47
 
41
48
  autocomplete_for :post, :title do |items|
@@ -47,7 +54,7 @@ The items passed into the block is an ActiveRecord scope allowing further scopes
47
54
  autocomplete_for :post, :title do |items|
48
55
  items.for_user(current_user).map(&:title).join("\n")
49
56
  end
50
-
57
+
51
58
  View
52
59
  ----
53
60
  <%= f.text_field :auto_user_name, :class => 'autocomplete', 'data-autocomplete-url'=>autocomplete_for_user_name_users_path %>
@@ -58,7 +65,7 @@ Routes
58
65
 
59
66
  JS
60
67
  --
61
- use any library you like
68
+ use any library you like<br/>
62
69
  (includes examples for jquery jquery.js + jquery.autocomplete.js + jquery.autocomplete.css)
63
70
 
64
71
 
@@ -108,8 +115,8 @@ Inspired by DHH`s 'obstrusive' autocomplete_plugin.
108
115
  - [Oliver Azevedo Barnes](http://github.com/oliverbarnes)
109
116
  - [Splendeo](http://www.splendeo.es)
110
117
  - [Franco Catena](https://github.com/francocatena)
111
-
112
118
 
113
- [Michael Grosser](http://grosser.it)
114
- michael@grosser.it
119
+
120
+ [Michael Grosser](http://grosser.it)<br/>
121
+ michael@grosser.it<br/>
115
122
  Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.3.8
@@ -9,8 +9,10 @@ class ActionController::Base
9
9
  # https://github.com/grosser/simple_auto_complete/pull/6
10
10
  define_method("autocomplete_for_#{object}_#{method}") do
11
11
  methods = options[:match] || [*method]
12
- condition = methods.map{|m| "LOWER(#{m}) LIKE ?"} * " OR "
13
- values = methods.map{|m| "%#{params[:q].to_s.downcase}%"}
12
+ query = options[:query] || 'LOWER(%{field}) LIKE %{query}'
13
+ mask = options[:mask] || '%%%{value}%'
14
+ condition = methods.map{|m| query % {:field => m, :query => '?'}} * ' OR '
15
+ values = methods.map{|m| mask % {:value => params[:q].to_s.downcase}}
14
16
  conditions = [condition, *values]
15
17
 
16
18
  model = object.to_s.camelize.constantize
@@ -18,7 +20,7 @@ class ActionController::Base
18
20
  :conditions => conditions,
19
21
  :order => "#{methods.first} ASC",
20
22
  :limit => 10
21
- }.merge!(options.except(:match))
23
+ }.merge!(options.except(:match, :query, :mask))
22
24
 
23
25
  @items = model.scoped(find_options)
24
26
 
@@ -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.7"
8
+ s.version = "0.3.8"
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{2011-02-03}
12
+ s.date = %q{2011-03-11}
13
13
  s.email = %q{grosser.michael@gmail.com}
14
14
  s.files = [
15
15
  "Gemfile",
@@ -75,7 +75,31 @@ describe 'Controller extensions' do
75
75
  @c.autocomplete_for_user_name(:match => [:full_name, :name])
76
76
  end
77
77
  end
78
-
78
+
79
+ describe "autocomplete with :query" do
80
+ before do
81
+ UsersController.autocomplete_for(:user, :name, :query => '%{field} = LOWER(%{query})')
82
+ end
83
+
84
+ it "finds using the operator" do
85
+ @c.stub!(:params).and_return :q=>'Hans'
86
+ User.should_receive(:scoped).with(hash_including(:conditions => ["name = LOWER(?)", "%hans%"]))
87
+ @c.autocomplete_for_user_name(:query => '%{field} = LOWER(%{query})')
88
+ end
89
+ end
90
+
91
+ describe "autocomplete with :mask" do
92
+ before do
93
+ UsersController.autocomplete_for(:user, :name, :mask => '%{value}%')
94
+ end
95
+
96
+ it "finds using the mask" do
97
+ @c.stub!(:params).and_return :q=>'Hans'
98
+ User.should_receive(:scoped).with(hash_including(:conditions => ["LOWER(name) LIKE ?", "hans%"]))
99
+ @c.autocomplete_for_user_name(:mask => '%{value}%')
100
+ end
101
+ end
102
+
79
103
  describe "autocomplete using blocks" do
80
104
  it "evaluates the block" do
81
105
  x=0
@@ -207,4 +231,4 @@ describe 'Model extensions' do
207
231
  p.tags.should == [@tag]
208
232
  end
209
233
  end
210
- end
234
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_autocomplete
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 7
10
- version: 0.3.7
9
+ - 8
10
+ version: 0.3.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-03 00:00:00 +01:00
18
+ date: 2011-03-11 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21