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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 14259ef77061c26e563a431f2aee2b43d4f0828c
4
- data.tar.gz: 1f1c53649481c320639e4cb64fe2e01cbe3d33d8
3
+ metadata.gz: 7b64e1009c98554934753968a12b66710e022918
4
+ data.tar.gz: ead6c3855b4285718d8a1bf503adb342eb4ae476
5
5
  SHA512:
6
- metadata.gz: 7c64ec8f29c0f2e7d1f19388afa7dc4c5fc3e9bacc55a484647783106de139acdb677a950c42a3d6f85d86a7161c8baf77f4143d87e451656034513a64eba82f
7
- data.tar.gz: 6da12838083832a22f23d841c022a91852998587cd83e19245805b424be2569a5dbd032f4f97411c51b6c013bc898e69cecf385c25326c9bb3e8dc778b5221d1
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.try(:[], :key)
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
- results.order(default_sort_sql)
146
+ order_by_sort_option(default_sort_option)
146
147
  elsif sort_option[:string_or_proc].is_a?(String)
147
- results.order(basic_sort_sql)
148
+ order_by_sort_option(sort_option)
148
149
  elsif sort_option[:string_or_proc].is_a?(Proc)
149
- apply_sort_proc
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 default_sort_sql
180
- "#{results.model.table_name}.id asc"
181
- end
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
- #{sort_option[:opts][:nulls_last] ? 'NULLS LAST' : ''}
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 apply_sort_proc
193
- sort_key = sort_option[: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
- sort_option[:string_or_proc].call(results, matches, self)
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
@@ -1,3 +1,3 @@
1
1
  module Filterer
2
- VERSION = '1.0.0.beta.2'
2
+ VERSION = '1.0.0.beta.3'
3
3
  end
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.2
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-03 00:00:00.000000000 Z
11
+ date: 2015-06-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails