filterer 1.0.0.beta.2 → 1.0.0.beta.3
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/lib/filterer/base.rb +31 -16
- data/lib/filterer/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b64e1009c98554934753968a12b66710e022918
|
4
|
+
data.tar.gz: ead6c3855b4285718d8a1bf503adb342eb4ae476
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6dfa2494a649caec1aa3cd17bd7a8ba74a5d63611f3d3eb53a824fc9e44c05855f9dccdd25f97c29a60c84f568018eb94c1240f6df044577e498fdf8bb07ff89
|
7
|
+
data.tar.gz: f3fe30e437c2719d3ea7ebfbe30947c69f8ecdf6d646f193da2daf43d061d88393190e2435d75814a61e73a98e5819811e54c68799eaf3f5e35ec6b04baeea54
|
data/lib/filterer/base.rb
CHANGED
@@ -86,12 +86,13 @@ module Filterer
|
|
86
86
|
params[:direction].try(:downcase) == 'desc' ? 'desc' : 'asc'
|
87
87
|
end
|
88
88
|
|
89
|
+
# @return [String] the key for the applied sort option.
|
89
90
|
def sort
|
90
91
|
@sort ||= begin
|
91
92
|
if params[:sort] && find_sort_option_from_param(params[:sort])
|
92
93
|
params[:sort]
|
93
94
|
else
|
94
|
-
default_sort_option
|
95
|
+
default_sort_option[:key]
|
95
96
|
end
|
96
97
|
end
|
97
98
|
end
|
@@ -142,11 +143,19 @@ module Filterer
|
|
142
143
|
|
143
144
|
def order_results
|
144
145
|
self.results = if !sort_option
|
145
|
-
|
146
|
+
order_by_sort_option(default_sort_option)
|
146
147
|
elsif sort_option[:string_or_proc].is_a?(String)
|
147
|
-
|
148
|
+
order_by_sort_option(sort_option)
|
148
149
|
elsif sort_option[:string_or_proc].is_a?(Proc)
|
149
|
-
|
150
|
+
sort_string = sort_proc_to_string(sort_option)
|
151
|
+
|
152
|
+
if sort_string
|
153
|
+
order_by_sort_option(sort_option.merge(
|
154
|
+
string_or_proc: sort_string
|
155
|
+
))
|
156
|
+
else
|
157
|
+
order_by_sort_option(filterer_default_sort_option)
|
158
|
+
end
|
150
159
|
end
|
151
160
|
end
|
152
161
|
|
@@ -166,6 +175,8 @@ module Filterer
|
|
166
175
|
@sort_option ||= find_sort_option_from_param(sort)
|
167
176
|
end
|
168
177
|
|
178
|
+
# @param x [String]
|
179
|
+
# @return [Hash] sort_option
|
169
180
|
def find_sort_option_from_param(x)
|
170
181
|
self.class.sort_options.detect do |sort_option|
|
171
182
|
if sort_option[:key].is_a?(Regexp)
|
@@ -176,29 +187,33 @@ module Filterer
|
|
176
187
|
end
|
177
188
|
end
|
178
189
|
|
179
|
-
def
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
def basic_sort_sql
|
184
|
-
%{
|
185
|
-
#{sort_option[:string_or_proc]}
|
190
|
+
def order_by_sort_option(opt)
|
191
|
+
results.order %{
|
192
|
+
#{opt[:string_or_proc]}
|
186
193
|
#{direction}
|
187
|
-
#{
|
194
|
+
#{opt[:opts][:nulls_last] ? 'NULLS LAST' : ''}
|
188
195
|
#{tiebreaker_sort_string ? ', ' + tiebreaker_sort_string : ''}
|
189
196
|
}.squish
|
190
197
|
end
|
191
198
|
|
192
|
-
def
|
193
|
-
sort_key =
|
199
|
+
def sort_proc_to_string(opt)
|
200
|
+
sort_key = opt[:key]
|
194
201
|
matches = sort_key.is_a?(Regexp) && params[:sort].match(sort_key)
|
195
|
-
|
202
|
+
opt[:string_or_proc].call(matches)
|
196
203
|
end
|
197
204
|
|
198
205
|
def default_sort_option
|
199
206
|
self.class.sort_options.detect do |sort_option|
|
200
207
|
sort_option[:opts][:default]
|
201
|
-
end
|
208
|
+
end || filterer_default_sort_option
|
209
|
+
end
|
210
|
+
|
211
|
+
def filterer_default_sort_option
|
212
|
+
{
|
213
|
+
key: 'default',
|
214
|
+
string_or_proc: "#{results.model.table_name}.id",
|
215
|
+
opts: {}
|
216
|
+
}
|
202
217
|
end
|
203
218
|
|
204
219
|
def tiebreaker_sort_string
|
data/lib/filterer/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filterer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.beta.
|
4
|
+
version: 1.0.0.beta.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Becker
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|