autocomplete_rails 0.3.1 → 0.4.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 +4 -4
- data/Rakefile +7 -0
- data/lib/autocomplete_rails/controller.rb +18 -5
- data/lib/autocomplete_rails/engine.rb +1 -1
- data/lib/autocomplete_rails/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1252115fac64fe3b2177ac3fe4ecd2cdec6dcc57
|
4
|
+
data.tar.gz: 05bf9a79349b5a78eaa913f928a5347cc9a16efd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a678532046c07d694d814e6f3ff4b89d2c91f0b7bd0f123d9a97752beb346fbe83c53acc6a849b6bfa2331b74139d1fe2359b54597ea579aa84454514faced6
|
7
|
+
data.tar.gz: 3c5fcc78cc3f83bbbba89efc34f77c2aa09d7e7f5e4ba66c85b6db5a152f73a2e3cb9ba012dea61e9ff119e2712272513a7e5abdf2a1dbb993f80d4d9737a19c
|
data/Rakefile
CHANGED
@@ -27,3 +27,10 @@ RSpec::Core::RakeTask.new(:spec)
|
|
27
27
|
desc 'Run all specs in spec directory (excluding plugin specs)'
|
28
28
|
task default: :spec
|
29
29
|
|
30
|
+
task :build do
|
31
|
+
system "gem build autocomplete_rails.gemspec"
|
32
|
+
end
|
33
|
+
|
34
|
+
task release: :build do
|
35
|
+
system "gem push bundler-#{AutocompleteRails::VERSION}"
|
36
|
+
end
|
@@ -26,6 +26,8 @@ module AutocompleteRails
|
|
26
26
|
# * value_method - method on model to autocomplete. This supplies the 'value' field in results.
|
27
27
|
# Also used as the label unless you supply options[:label_method].
|
28
28
|
# * options - hash of optional settings.
|
29
|
+
# * &block - an optional block to further modify the results set,
|
30
|
+
# e.g. `{ |results| results.where(smth: @instance_variable) }`
|
29
31
|
#
|
30
32
|
#
|
31
33
|
# Options accepts a hash of:
|
@@ -39,6 +41,8 @@ module AutocompleteRails
|
|
39
41
|
# field being searched (see value_method above) must start with the search term.
|
40
42
|
# * :scopes - Build your autocomplete query from the specified ActiveRecord scope(s). Multiple scopes can be
|
41
43
|
# used, pass them in as an array. Example: `scopes: [:scope1, :scope2]`
|
44
|
+
# If you need to pass additional arguments to your scope, define them as an array of arrays.
|
45
|
+
# Example: `scopes: [:scope1, [:scope2, 'argument 1', 'argument 2] ]`
|
42
46
|
# * :order - specify an order clause, defaults to 'LOWER(#{table}.#{value_method}) ASC'
|
43
47
|
#
|
44
48
|
# Be sure to add a route to reach the generated controller method. Example:
|
@@ -54,13 +58,13 @@ module AutocompleteRails
|
|
54
58
|
# autocomplete :user, :email, label_method: full_name, full_model: true
|
55
59
|
# end
|
56
60
|
#
|
57
|
-
def autocomplete(model_symbol, value_method, options = {})
|
61
|
+
def autocomplete(model_symbol, value_method, options = {}, &block)
|
58
62
|
label_method = options[:label_method] || value_method
|
59
63
|
model = model_symbol.to_s.camelize.constantize
|
60
64
|
autocomplete_method_name = "autocomplete_#{model_symbol}_#{value_method}"
|
61
65
|
|
62
66
|
define_method(autocomplete_method_name) do
|
63
|
-
results = autocomplete_results(model, value_method, label_method, options)
|
67
|
+
results = autocomplete_results(model, value_method, label_method, options, &block)
|
64
68
|
render json: autocomplete_build_json(results, value_method, label_method, options), root: false
|
65
69
|
end
|
66
70
|
end
|
@@ -68,13 +72,22 @@ module AutocompleteRails
|
|
68
72
|
|
69
73
|
protected
|
70
74
|
|
71
|
-
def autocomplete_results(model, value_method, label_method = nil, options)
|
75
|
+
def autocomplete_results(model, value_method, label_method = nil, options, &block)
|
72
76
|
term = params[:term]
|
73
77
|
return {} if term.blank?
|
74
78
|
|
75
79
|
results = model.where(nil) # make an empty scope to add select, where, etc, to.
|
76
80
|
scopes = Array(options[:scopes])
|
77
|
-
|
81
|
+
unless scopes.empty?
|
82
|
+
scopes.each do |scope|
|
83
|
+
if scope.is_a?(Array)
|
84
|
+
results = results.send(scope.slice(0), *scope.drop(1))
|
85
|
+
else
|
86
|
+
results = results.send(scope)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
results = instance_exec(results, &block) if block
|
78
91
|
results = results.select(autocomplete_select_clause(model, value_method, label_method, options)) unless
|
79
92
|
options[:full_model]
|
80
93
|
results.
|
@@ -112,7 +125,7 @@ module AutocompleteRails
|
|
112
125
|
|
113
126
|
# default to ASC order
|
114
127
|
table_prefix = "#{model.table_name}."
|
115
|
-
"LOWER(#{table_prefix}#{value_method}) ASC"
|
128
|
+
Arel.sql("LOWER(#{table_prefix}#{value_method}) ASC")
|
116
129
|
end
|
117
130
|
|
118
131
|
def autocomplete_build_json(results, value_method, label_method, options)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: autocomplete_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Tomich
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: '4.0'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '5.
|
22
|
+
version: '5.3'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,21 +29,21 @@ dependencies:
|
|
29
29
|
version: '4.0'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '5.
|
32
|
+
version: '5.3'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
|
-
name:
|
34
|
+
name: factory_bot
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - "~>"
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version:
|
39
|
+
version: 4.10.0
|
40
40
|
type: :development
|
41
41
|
prerelease: false
|
42
42
|
version_requirements: !ruby/object:Gem::Requirement
|
43
43
|
requirements:
|
44
44
|
- - "~>"
|
45
45
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
46
|
+
version: 4.10.0
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec-rails
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|