miscellany 0.1.22 → 0.1.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cbc281a50690bfaa8e5745c43786179654ffd59a87a5df621d1f7423f8a1cb6c
4
- data.tar.gz: 105181f0df9279f89e85ba59a35ddda7d390d2dc86068b8a5d2e28ee5cc4887e
3
+ metadata.gz: 6de3e8ac55956c58bff00af5ffae6ce8972b142be6a5aad47096c4776658ba4e
4
+ data.tar.gz: 4668195d6a891348e495fff7ecf187415eef6f2588d14e33be556f04f7358042
5
5
  SHA512:
6
- metadata.gz: f393bac71597cef4b0879b0a8d9a21003d5e36355887706951880f4cac5ea769ffc8088f5b4d523ea2cdbe7c0b2552d56a4a759db0a9ae4acf69134a2e4e15b8
7
- data.tar.gz: a9265249c223ebd121fe8189aacfd462376250b3fb13726ea1878e2b2931317604cec856d6db7f457ecefb77e645bc178a07d45e7a3b95b7347cb8e9eda9515c
6
+ metadata.gz: 2cec7d17686d2d9faf88dcfc20ac2ca5fba8f10ca7c1e021a7e0b67af4f6506b8d0a03444437ac1aae23eaa39e3a116cbb9e9ef62b649ec73faaf3b45e155772
7
+ data.tar.gz: 7a0c8b98668ad66aebd4b51e3d41aa969a13e3138992021f36bf95f04be3e1db289f95fb43e95f04e8b8876c2f5e4652a9e1609b1dfbd907595f21f7d9513d4d
@@ -203,12 +203,12 @@ module Miscellany
203
203
 
204
204
  base_module ||= mod
205
205
 
206
- if mod.name.ends_with? "Patch"
206
+ if mod.name.end_with? "Patch"
207
207
  base_full_name = base_module.to_s
208
208
  mod_full_name = mod.to_s
209
209
  mod_rel_name = mod_full_name.sub(base_full_name, '')
210
210
  mod_rel_bits = mod_rel_name.split('::').select(&:present?).map do |bit|
211
- bit.ends_with?('Patch') ? bit[0..-6] : bit
211
+ bit.end_with?('Patch') ? bit[0..-6] : bit
212
212
  end
213
213
  final_mod_name = [install_base, *mod_rel_bits].select(&:present?).join("::")
214
214
  install_mod = final_mod_name.constantize
@@ -202,6 +202,18 @@ module Miscellany
202
202
  limit -= offset unless limit.nil?
203
203
 
204
204
  query = items
205
+
206
+ applied_sorts.each do |sort|
207
+ # TODO Throw an error if joins: is present on a non-AR::Relation?
208
+ if sort[:joins].is_a?(ActiveRecord::Relation)
209
+ query = query.merge(sort[:joins])
210
+ elsif sort[:joins].is_a?(Proc)
211
+ query = sort[:joins].call(query)
212
+ elsif sort[:joins].present?
213
+ query = query.joins(sort[:joins])
214
+ end
215
+ end
216
+
205
217
  sort_clause = sort_sql
206
218
  query = query.order(Arel.sql(sort_clause)) if sort_clause.present?
207
219
 
@@ -219,13 +231,19 @@ module Miscellany
219
231
  [slice_start, slice_end == -1 ? nil : slice_end]
220
232
  end
221
233
 
222
- def sort_sql
223
- sorts = [ *Array(self.sort), *options[:sort_parser]&.default_sorts]
224
- sorts.compact!
234
+ def applied_sorts
235
+ @applied_sorts ||= begin
236
+ sorts = [ *Array(self.sort), *options[:sort_parser]&.default_sorts]
237
+ sorts.compact!
238
+ sorts = Miscellany::SortLang.distinct_sorts(sorts)
239
+ sorts
240
+ end
241
+ end
225
242
 
226
- return nil unless sorts.present?
243
+ def sort_sql
244
+ return nil unless applied_sorts.present?
227
245
 
228
- Miscellany::SortLang.sqlize(sorts)
246
+ Miscellany::SortLang.sqlize(applied_sorts)
229
247
  end
230
248
  end
231
249
  end
@@ -22,11 +22,11 @@ module Miscellany
22
22
  sort.compact
23
23
  end
24
24
 
25
- def self.sqlize(sorts)
25
+ def self.distinct_sorts(sorts)
26
26
  seen_sorts = Set.new
27
27
 
28
28
  # Only include each sort key/"column" once
29
- sorts = sorts.select do |sort|
29
+ sorts.select do |sort|
30
30
  sid = sort[:key] || sort[:column]
31
31
  next true unless sid.present?
32
32
 
@@ -37,7 +37,9 @@ module Miscellany
37
37
  true
38
38
  end
39
39
  end
40
+ end
40
41
 
42
+ def self.sqlize(sorts)
41
43
  sorts.map do |sort|
42
44
  order = sort[:order] || 'ASC'
43
45
  if sort[:column].is_a?(Proc)
@@ -1,3 +1,3 @@
1
1
  module Miscellany
2
- VERSION = "0.1.22".freeze
2
+ VERSION = "0.1.24".freeze
3
3
  end
@@ -30,14 +30,17 @@ describe Miscellany::SortLang do
30
30
  end
31
31
  end
32
32
 
33
- describe ".sqlize" do
33
+ describe ".distinct_sorts" do
34
34
  it "only includes each sort key once" do
35
- expect(Miscellany::SortLang.sqlize([
35
+ distinct = Miscellany::SortLang.distinct_sorts([
36
36
  { column: "created_at", order: "ASC" },
37
37
  { column: "created_at", order: "DESC" },
38
- ])).to eql "created_at ASC NULLS FIRST"
38
+ ])
39
+ expect(Miscellany::SortLang.sqlize(distinct)).to eql "created_at ASC NULLS FIRST"
39
40
  end
41
+ end
40
42
 
43
+ describe ".sqlize" do
41
44
  describe "nulls: option" do
42
45
  it "accepts a nulls: option" do
43
46
  expect(Miscellany::SortLang.sqlize([
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miscellany
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.22
4
+ version: 0.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ethan Knapp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-20 00:00:00.000000000 Z
11
+ date: 2024-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails