autocomplete_rails 0.1.0
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.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/Rakefile +37 -0
- data/config/routes.rb +2 -0
- data/lib/autocomplete_rails.rb +8 -0
- data/lib/autocomplete_rails/controller.rb +132 -0
- data/lib/autocomplete_rails/engine.rb +11 -0
- data/lib/autocomplete_rails/version.rb +3 -0
- data/lib/tasks/autocomplete_rails_tasks.rake +4 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3705d9900a36155396f2cda10e3d54db913e0e1d
|
4
|
+
data.tar.gz: 1ac76f990acb239985ab9c0f9f4a2aee1f761046
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8e4924deed44b2137c982b28d4a37a141ea513594279f9fe80d4f714ab8d5787ef6fde4d78072658a02147d8178e21d32404ccf475f84d74086df430b70fc98d
|
7
|
+
data.tar.gz: 7e3ef03657f129966b61399be51f3483b0916fb8c4f2a664e57b30cc467a76987cf55977d404f2fc06aa0d28143c86b837a1c9466310f08d28b9e79d964e805d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2016 Justin Tomich
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'AutocompleteRails'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.rdoc')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
Bundler::GemHelper.install_tasks
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
|
29
|
+
Rake::TestTask.new(:test) do |t|
|
30
|
+
t.libs << 'lib'
|
31
|
+
t.libs << 'test'
|
32
|
+
t.pattern = 'test/**/*_test.rb'
|
33
|
+
t.verbose = false
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
task default: :test
|
data/config/routes.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
module AutocompleteRails
|
2
|
+
module Controller
|
3
|
+
def self.included(target)
|
4
|
+
target.extend AutocompleteRails::Controller::ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
#
|
9
|
+
# Generate an autocomplete controller action.
|
10
|
+
#
|
11
|
+
# The controller action is intended to interact with jquery UI's autocomplete widget. The autocomplete
|
12
|
+
# controller provides suggestions while you type into a field that is provisioned with JQuery's autocomplete
|
13
|
+
# widget.
|
14
|
+
#
|
15
|
+
# The generated method is named "autocomplete_#{model_symbol}_#{value_method}", for example:
|
16
|
+
#
|
17
|
+
# class UsersController
|
18
|
+
# autocomplete :user, :email
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# generates a method named `autocomplete_user_email`.
|
22
|
+
#
|
23
|
+
#
|
24
|
+
# Parameters:
|
25
|
+
# * model_symbol - model class to autocomplete, e.g.
|
26
|
+
# * value_method - method on model to autocomplete. This supplies the 'value' field in results.
|
27
|
+
# Also used as the label unless you supply options[:label_method].
|
28
|
+
# * options - hash of optional settings.
|
29
|
+
#
|
30
|
+
#
|
31
|
+
# Options accepts a hash of:
|
32
|
+
# * :label_method - call a separate method for the label, otherwise defaults to value_method. If your label
|
33
|
+
# method is a method that is *not* a column in your DB, you may need options[:full_model].
|
34
|
+
# * :full_model - load full model instead of only selecting the specified values. Default is false.
|
35
|
+
# * :limit - default is 10.
|
36
|
+
# * :case_sensitive - if true, the search is case sensitive. Default is false.
|
37
|
+
# * :additional_data - collect additional data. Will be added to select unless full_model is invoked.
|
38
|
+
# * :full_search - search the entire value string for the term. Defaults to false, in which case the value
|
39
|
+
# field being searched (see value_method above) must start with the search term.
|
40
|
+
# * :scopes - limit query to these ActiveRecord scopes, passed in as an array,
|
41
|
+
# for example: `scopes: [:scope1, :scope2]`
|
42
|
+
#
|
43
|
+
# Be sure to add a route to reach the generated controller method. Example:
|
44
|
+
#
|
45
|
+
# resources :users do
|
46
|
+
# get :autocomplete_user_email, on: :collection
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# The following example searches for users by email, but displays their :full_name as the label.
|
50
|
+
# The full_model flag is also loaded, as full_name is a method that synthesizes multiple columns.
|
51
|
+
#
|
52
|
+
# class UsersController
|
53
|
+
# autocomplete :user, :email, label_method: full_name, full_model: true
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
def autocomplete(model_symbol, value_method, options = {})
|
57
|
+
label_method = options[:label_method] || value_method
|
58
|
+
model = model_symbol.to_s.camelize.constantize
|
59
|
+
autocomplete_method_name = "autocomplete_#{model_symbol}_#{value_method}"
|
60
|
+
|
61
|
+
define_method(autocomplete_method_name) do
|
62
|
+
results = autocomplete_results(model, value_method, label_method, options)
|
63
|
+
render json: autocomplete_build_json(results, value_method, label_method, options), root: false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
protected
|
69
|
+
|
70
|
+
def autocomplete_results(model, value_method, label_method = nil, options)
|
71
|
+
term = params[:term]
|
72
|
+
return {} if term.blank?
|
73
|
+
|
74
|
+
results = model.where(nil) # make an empty scope to add select, where, etc, to.
|
75
|
+
scopes = Array(options[:scopes])
|
76
|
+
scopes.each { |scope| results = results.send(scope) } unless scopes.empty?
|
77
|
+
results = results.select(autocomplete_select_clause(model, value_method, label_method, options)) unless
|
78
|
+
options[:full_model]
|
79
|
+
results.
|
80
|
+
where(autocomplete_where_clause(term, model, value_method, options)).
|
81
|
+
limit(autocomplete_limit_clause(options)).
|
82
|
+
order(autocomplete_order_clause(model, value_method, options))
|
83
|
+
end
|
84
|
+
|
85
|
+
def autocomplete_select_clause(model, value_method, label_method, options)
|
86
|
+
table_name = model.table_name
|
87
|
+
selects = []
|
88
|
+
selects << "#{table_name}.#{model.primary_key}"
|
89
|
+
selects << "#{table_name}.#{value_method}"
|
90
|
+
selects << "#{table_name}.#{label_method}" if label_method
|
91
|
+
options[:additional_data].each { |datum| selects << "#{table_name}.#{datum}" } if options[:additional_data]
|
92
|
+
selects
|
93
|
+
end
|
94
|
+
|
95
|
+
def autocomplete_where_clause(term, model, value_method, options)
|
96
|
+
term = term.gsub(/[_%]/) { |x| "\\#{x}" } # escape any _'s or %'s in the search term
|
97
|
+
term = "#{term}%"
|
98
|
+
term = "%#{term}" if options[:full_search]
|
99
|
+
table_name = model.table_name
|
100
|
+
lower = options[:case_sensitive] ? '' : 'LOWER'
|
101
|
+
["#{lower}(#{table_name}.#{value_method}) LIKE #{lower}(?) ESCAPE \"\\\"", term]
|
102
|
+
end
|
103
|
+
|
104
|
+
def autocomplete_limit_clause(options)
|
105
|
+
options[:limit] ||= 10
|
106
|
+
end
|
107
|
+
|
108
|
+
def autocomplete_order_clause(model, value_method, options)
|
109
|
+
return options[:order] if options[:order]
|
110
|
+
|
111
|
+
# default to ASC order
|
112
|
+
table_prefix = "#{model.table_name}."
|
113
|
+
"LOWER(#{table_prefix}#{value_method}) ASC"
|
114
|
+
end
|
115
|
+
|
116
|
+
def autocomplete_build_json(results, value_method, label_method, options)
|
117
|
+
results.collect do |result|
|
118
|
+
data = HashWithIndifferentAccess.new(id: result.id,
|
119
|
+
label: result.send(label_method),
|
120
|
+
value: result.send(value_method))
|
121
|
+
options[:additional_data].each do |method|
|
122
|
+
data[method] = result.send(method)
|
123
|
+
end if options[:additional_data]
|
124
|
+
data
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def postgres?(model_class)
|
129
|
+
model_class.connection.class.to_s.match /Postgre/
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: autocomplete_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Tomich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.1'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '4.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.1'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: sqlite3
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.3'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.3'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: factory_girl
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4.4'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '4.4'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec-rails
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.1'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.1'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec-mocks
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.1'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.1'
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: shoulda-matchers
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '2.8'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '2.8'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: database_cleaner
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.5'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '1.5'
|
117
|
+
- !ruby/object:Gem::Dependency
|
118
|
+
name: timecop
|
119
|
+
requirement: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0.8'
|
124
|
+
type: :development
|
125
|
+
prerelease: false
|
126
|
+
version_requirements: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0.8'
|
131
|
+
description: Easily use jQuery UI's autocomplete widget with Rails applications.
|
132
|
+
email:
|
133
|
+
- justin@tomich.org
|
134
|
+
executables: []
|
135
|
+
extensions: []
|
136
|
+
extra_rdoc_files: []
|
137
|
+
files:
|
138
|
+
- MIT-LICENSE
|
139
|
+
- Rakefile
|
140
|
+
- config/routes.rb
|
141
|
+
- lib/autocomplete_rails.rb
|
142
|
+
- lib/autocomplete_rails/controller.rb
|
143
|
+
- lib/autocomplete_rails/engine.rb
|
144
|
+
- lib/autocomplete_rails/version.rb
|
145
|
+
- lib/tasks/autocomplete_rails_tasks.rake
|
146
|
+
homepage: https://github.com/tomichj/autocomplete_rails
|
147
|
+
licenses:
|
148
|
+
- MIT
|
149
|
+
metadata: {}
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.5.1
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: Easily use jQuery UI's autocomplete widget with Rails applications.
|
170
|
+
test_files: []
|