active_sort_order 0.9.2 → 0.9.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
  SHA256:
3
- metadata.gz: 344ed5136f8ac05c4dcb05fbd86dce86ee2ffc7e5a97b693057668cd8fe5dd6d
4
- data.tar.gz: 8a11524e7c75f247213c2cb01b7f7838f797258b02147ac7b784da79c02d0aeb
3
+ metadata.gz: '089c16d2abd0a4aeb75c3a377d54f70883a7397758841c55f7bb64940cb3b205'
4
+ data.tar.gz: caf3be0137352d2c1df913f9bdf6749253d15e7ed852d1bba646a2ce58fe90f4
5
5
  SHA512:
6
- metadata.gz: 4de7def8d5b3c503aac5198773f6ab0b3ae020462a95699238e347911efea050636192eb7382e83a875faaeeaa7e07ab425a74aa8351eda471f6fbbd2a56c91d
7
- data.tar.gz: 77a40b71113c7b01076d1af3cef0304b078b0c86436f889b3583303a7d7ee52fb800de94ca38b6a13f196d9cb23c3850c91bef4306c6f2fb0639c090b4591d1f
6
+ metadata.gz: aaf7872401a767653abe756bc53955e2daa7df56cc1c37a204feff1f409290b0b11ea6cd9afc6e2376992e7be594f6d7c63add7f47e5ebee042fa9092f2dda1a
7
+ data.tar.gz: aff10d7f87d429fe6e6e3c14a1697d2641eaf7a2cd427475c79d6f803bcbca64a47861d8e0f93de79e26ed8c44fa9267b981cd5e5b83a4cf40ae3dd0c92b47bf
data/CHANGELOG.md CHANGED
@@ -2,7 +2,7 @@ CHANGELOG
2
2
  ---------
3
3
 
4
4
  - **Unreleased - [View Diff](https://github.com/westonganger/active_sort_order/compare/v0.9.2...master)**
5
- * Nothing yet
5
+ * Allow ability to sort on multiple fields
6
6
 
7
7
  - **v0.9.2 - Apr 28, 2021 - [View Diff](https://github.com/westonganger/active_sort_order/compare/v0.9.1...v0.9.2)**
8
8
  * Fix deprecation warning in Rails 6.1 for `reorder(nil)`
data/README.md CHANGED
@@ -36,8 +36,12 @@ In the below examples we are within a controller and are using the params as our
36
36
  ```ruby
37
37
  # app/controllers/posts_controller.rb
38
38
 
39
- if params[:sort] == "number_str"
39
+ case params[:sort]
40
+ when "number_str"
40
41
  sort_col_sql = "CAST(posts.number_str AS int)"
42
+ when "user"
43
+ ### To sort on multiple fields pass in an Array
44
+ sort_col_sql = ["users.first_name", "users.last_name"]
41
45
  else
42
46
  sort_col_sql = params[:sort]
43
47
  end
@@ -49,6 +53,8 @@ Post.all.sort_order(sort_col_sql, params[:direction], base_sort_order: "lower(nu
49
53
  Post.all.sort_order(sort_col_sql, params[:direction])
50
54
  ```
51
55
 
56
+ ## Sorting on multiple columns
57
+
52
58
  ##### Method Definition:
53
59
 
54
60
  `sort_order(sort_col_sql = nil, sort_direction_sql = nil, base_sort_order: true)`
@@ -5,12 +5,12 @@ module ActiveSortOrder
5
5
  included do
6
6
 
7
7
  scope :sort_order, ->(sort_col_sql = nil, sort_direction_sql = nil, base_sort_order: true){
8
- if [String, Symbol, NilClass].exclude?(sort_col_sql.class)
9
- raise ArgumentError.new("Invalid first argument `sort_col_sql`, expecting a a String or Symbol")
10
- end
11
-
12
8
  if sort_col_sql.present?
13
- sort_col_sql = sort_col_sql.to_s
9
+ if sort_col_sql.is_a?(Array)
10
+ sort_col_sql = sort_col_sql.map{|x| x.to_s }
11
+ else
12
+ sort_col_sql = sort_col_sql.to_s
13
+ end
14
14
 
15
15
  ### SORT DIRECTION HANDLING
16
16
  if sort_direction_sql.is_a?(Symbol)
@@ -20,7 +20,7 @@ module ActiveSortOrder
20
20
  if sort_direction_sql.nil? || (sort_direction_sql.is_a?(String) && sort_direction_sql == "")
21
21
  sort_direction_sql = "ASC"
22
22
  elsif !sort_direction_sql.is_a?(String)
23
- raise ArgumentError.new("Invalid second argument `sort_direction_sql`, expecting a a String or Symbol")
23
+ raise ArgumentError.new("Invalid second argument `sort_direction_sql`, expecting a String or Symbol")
24
24
  else
25
25
  valid_directions = [
26
26
  "ASC",
@@ -41,7 +41,11 @@ module ActiveSortOrder
41
41
  end
42
42
  end
43
43
 
44
- sql_str = "#{sort_col_sql} #{sort_direction_sql}"
44
+ if !sort_col_sql.is_a?(Array)
45
+ sort_col_sql = [sort_col_sql]
46
+ end
47
+
48
+ sql_str = sort_col_sql.map{|x| "#{x} #{sort_direction_sql}" }.join(", ")
45
49
  end
46
50
 
47
51
  ### BASE SORT ORDER HANDLING
@@ -1,3 +1,3 @@
1
1
  module ActiveSortOrder
2
- VERSION = "0.9.2".freeze
2
+ VERSION = "0.9.3".freeze
3
3
  end
Binary file
@@ -201,3 +201,342 @@ ActiveSortOrderTest: test_argument_base_sort_order_errors
201
201
  ActiveSortOrderTest: test_sort_direction_errors
202
202
  -----------------------------------------------
203
203
  TRANSACTION (0.1ms) rollback transaction
204
+  (1.1ms) SELECT sqlite_version(*)
205
+  (0.6ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? [["key", "schema_sha1"]]
206
+  (0.1ms) SELECT sqlite_version(*)
207
+  (0.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
208
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
209
+ ActiveRecord::InternalMetadata Load (0.6ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
210
+  (1.6ms) DELETE FROM posts;
211
+  (1.3ms) UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = 'posts';
212
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 1], ["b", 3], ["LIMIT", 1]]
213
+ TRANSACTION (0.0ms) begin transaction
214
+ Post Create (0.3ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 1], ["b", 3]]
215
+ TRANSACTION (0.8ms) commit transaction
216
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 2], ["b", 2], ["LIMIT", 1]]
217
+ TRANSACTION (0.0ms) begin transaction
218
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 2], ["b", 2]]
219
+ TRANSACTION (0.6ms) commit transaction
220
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 3], ["b", 2], ["LIMIT", 1]]
221
+ TRANSACTION (0.0ms) begin transaction
222
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 3], ["b", 2]]
223
+ TRANSACTION (0.5ms) commit transaction
224
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 4], ["b", 1], ["LIMIT", 1]]
225
+ TRANSACTION (0.0ms) begin transaction
226
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 4], ["b", 1]]
227
+ TRANSACTION (0.7ms) commit transaction
228
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 5], ["b", 1], ["LIMIT", 1]]
229
+ TRANSACTION (0.0ms) begin transaction
230
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 5], ["b", 1]]
231
+ TRANSACTION (1.1ms) commit transaction
232
+ TRANSACTION (0.1ms) begin transaction
233
+ -------------------------------------------------------
234
+ ActiveSortOrderTest: test_override_base_sort_order_only
235
+ -------------------------------------------------------
236
+  (0.1ms) SELECT COUNT(*) FROM "posts"
237
+ TRANSACTION (0.0ms) rollback transaction
238
+ TRANSACTION (0.0ms) begin transaction
239
+ -------------------------------------------------
240
+ ActiveSortOrderTest: test_sort_on_multiple_fields
241
+ -------------------------------------------------
242
+  (0.1ms) SELECT COUNT(*) FROM "posts"
243
+ TRANSACTION (0.0ms) rollback transaction
244
+ TRANSACTION (0.0ms) begin transaction
245
+ --------------------------------------------------
246
+ ActiveSortOrderTest: test_base_sort_order_and_sort
247
+ --------------------------------------------------
248
+  (0.1ms) SELECT COUNT(*) FROM "posts"
249
+ TRANSACTION (0.0ms) rollback transaction
250
+ TRANSACTION (0.0ms) begin transaction
251
+ ----------------------------------------------------
252
+ ActiveSortOrderTest: test_class_base_sort_order_only
253
+ ----------------------------------------------------
254
+  (0.1ms) SELECT COUNT(*) FROM "posts"
255
+ TRANSACTION (0.0ms) rollback transaction
256
+ TRANSACTION (0.0ms) begin transaction
257
+ ---------------------------------------------
258
+ ActiveSortOrderTest: test_exposes_main_module
259
+ ---------------------------------------------
260
+ TRANSACTION (0.0ms) rollback transaction
261
+ TRANSACTION (0.0ms) begin transaction
262
+ -----------------------------------
263
+ ActiveSortOrderTest: test_sort_only
264
+ -----------------------------------
265
+  (0.1ms) SELECT COUNT(*) FROM "posts"
266
+ TRANSACTION (0.0ms) rollback transaction
267
+ TRANSACTION (0.0ms) begin transaction
268
+ -----------------------------------------
269
+ ActiveSortOrderTest: test_exposes_version
270
+ -----------------------------------------
271
+ TRANSACTION (0.0ms) rollback transaction
272
+ TRANSACTION (0.0ms) begin transaction
273
+ -------------------------------------------------------
274
+ ActiveSortOrderTest: test_base_sort_order_default_value
275
+ -------------------------------------------------------
276
+ TRANSACTION (0.0ms) rollback transaction
277
+ PostWithBaseOrderA Load (0.2ms) SELECT "posts".* FROM "posts" /* loading for inspect */ ORDER BY "posts"."b" DESC LIMIT ? [["LIMIT", 11]]
278
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ ORDER BY "posts"."b" DESC LIMIT ? [["LIMIT", 11]]
279
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
280
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
281
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
282
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
283
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
284
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
285
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
286
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
287
+  (0.7ms) SELECT sqlite_version(*)
288
+  (0.1ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? [["key", "schema_sha1"]]
289
+  (0.0ms) SELECT sqlite_version(*)
290
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
291
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
292
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
293
+  (1.8ms) DELETE FROM posts;
294
+  (1.4ms) UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = 'posts';
295
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 1], ["b", 3], ["LIMIT", 1]]
296
+ TRANSACTION (0.0ms) begin transaction
297
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 1], ["b", 3]]
298
+ TRANSACTION (0.8ms) commit transaction
299
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 2], ["b", 2], ["LIMIT", 1]]
300
+ TRANSACTION (0.0ms) begin transaction
301
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 2], ["b", 2]]
302
+ TRANSACTION (0.7ms) commit transaction
303
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 3], ["b", 2], ["LIMIT", 1]]
304
+ TRANSACTION (0.0ms) begin transaction
305
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 3], ["b", 2]]
306
+ TRANSACTION (0.7ms) commit transaction
307
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 4], ["b", 1], ["LIMIT", 1]]
308
+ TRANSACTION (0.0ms) begin transaction
309
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 4], ["b", 1]]
310
+ TRANSACTION (0.7ms) commit transaction
311
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 5], ["b", 1], ["LIMIT", 1]]
312
+ TRANSACTION (0.0ms) begin transaction
313
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 5], ["b", 1]]
314
+ TRANSACTION (0.7ms) commit transaction
315
+ TRANSACTION (0.0ms) begin transaction
316
+ ---------------------------------------------
317
+ ActiveSortOrderTest: test_exposes_main_module
318
+ ---------------------------------------------
319
+ TRANSACTION (0.0ms) rollback transaction
320
+ TRANSACTION (0.0ms) begin transaction
321
+ -------------------------------------------------------
322
+ ActiveSortOrderTest: test_override_base_sort_order_only
323
+ -------------------------------------------------------
324
+  (0.1ms) SELECT COUNT(*) FROM "posts"
325
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
326
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
327
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
328
+ TRANSACTION (0.0ms) rollback transaction
329
+ TRANSACTION (0.0ms) begin transaction
330
+ -----------------------------------
331
+ ActiveSortOrderTest: test_sort_only
332
+ -----------------------------------
333
+  (0.1ms) SELECT COUNT(*) FROM "posts"
334
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY a desc, posts.a ASC
335
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
336
+ TRANSACTION (0.0ms) rollback transaction
337
+ TRANSACTION (0.0ms) begin transaction
338
+ -----------------------------------------
339
+ ActiveSortOrderTest: test_exposes_version
340
+ -----------------------------------------
341
+ TRANSACTION (0.0ms) rollback transaction
342
+ TRANSACTION (0.0ms) begin transaction
343
+ --------------------------------------------------
344
+ ActiveSortOrderTest: test_base_sort_order_and_sort
345
+ --------------------------------------------------
346
+  (0.1ms) SELECT COUNT(*) FROM "posts"
347
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
348
+ PostWithBaseOrderB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b DESC, posts.b ASC
349
+ TRANSACTION (0.0ms) rollback transaction
350
+ TRANSACTION (0.0ms) begin transaction
351
+ -------------------------------------------------
352
+ ActiveSortOrderTest: test_sort_on_multiple_fields
353
+ -------------------------------------------------
354
+  (0.1ms) SELECT COUNT(*) FROM "posts"
355
+ TRANSACTION (0.0ms) rollback transaction
356
+ TRANSACTION (0.0ms) begin transaction
357
+ -------------------------------------------------------
358
+ ActiveSortOrderTest: test_base_sort_order_default_value
359
+ -------------------------------------------------------
360
+ TRANSACTION (0.0ms) rollback transaction
361
+ TRANSACTION (0.0ms) begin transaction
362
+ ----------------------------------------------------
363
+ ActiveSortOrderTest: test_class_base_sort_order_only
364
+ ----------------------------------------------------
365
+  (0.1ms) SELECT COUNT(*) FROM "posts"
366
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC
367
+ PostWithBaseOrderB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
368
+ PostWithBaseOrderAAndB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC, posts.b ASC
369
+ PostWithBaseOrderBAndA Load (0.8ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC, posts.a ASC
370
+ TRANSACTION (0.1ms) rollback transaction
371
+  (0.7ms) SELECT sqlite_version(*)
372
+  (0.1ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? [["key", "schema_sha1"]]
373
+  (0.0ms) SELECT sqlite_version(*)
374
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
375
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
376
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
377
+  (1.2ms) DELETE FROM posts;
378
+  (0.9ms) UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = 'posts';
379
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 1], ["b", 3], ["LIMIT", 1]]
380
+ TRANSACTION (0.0ms) begin transaction
381
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 1], ["b", 3]]
382
+ TRANSACTION (0.7ms) commit transaction
383
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 2], ["b", 2], ["LIMIT", 1]]
384
+ TRANSACTION (0.0ms) begin transaction
385
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 2], ["b", 2]]
386
+ TRANSACTION (0.7ms) commit transaction
387
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 3], ["b", 2], ["LIMIT", 1]]
388
+ TRANSACTION (0.0ms) begin transaction
389
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 3], ["b", 2]]
390
+ TRANSACTION (0.7ms) commit transaction
391
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 4], ["b", 1], ["LIMIT", 1]]
392
+ TRANSACTION (0.0ms) begin transaction
393
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 4], ["b", 1]]
394
+ TRANSACTION (0.7ms) commit transaction
395
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 5], ["b", 1], ["LIMIT", 1]]
396
+ TRANSACTION (0.0ms) begin transaction
397
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 5], ["b", 1]]
398
+ TRANSACTION (0.7ms) commit transaction
399
+ TRANSACTION (0.0ms) begin transaction
400
+ -----------------------------------
401
+ ActiveSortOrderTest: test_sort_only
402
+ -----------------------------------
403
+  (0.1ms) SELECT COUNT(*) FROM "posts"
404
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY a desc, posts.a ASC
405
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
406
+ TRANSACTION (0.0ms) rollback transaction
407
+ TRANSACTION (0.0ms) begin transaction
408
+ ---------------------------------------------
409
+ ActiveSortOrderTest: test_exposes_main_module
410
+ ---------------------------------------------
411
+ TRANSACTION (0.0ms) rollback transaction
412
+ TRANSACTION (0.0ms) begin transaction
413
+ -----------------------------------------
414
+ ActiveSortOrderTest: test_exposes_version
415
+ -----------------------------------------
416
+ TRANSACTION (0.0ms) rollback transaction
417
+ TRANSACTION (0.0ms) begin transaction
418
+ ----------------------------------------------------
419
+ ActiveSortOrderTest: test_class_base_sort_order_only
420
+ ----------------------------------------------------
421
+  (0.1ms) SELECT COUNT(*) FROM "posts"
422
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC
423
+ PostWithBaseOrderB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
424
+ PostWithBaseOrderAAndB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC, posts.b ASC
425
+ PostWithBaseOrderBAndA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC, posts.a ASC
426
+ TRANSACTION (0.0ms) rollback transaction
427
+ TRANSACTION (0.0ms) begin transaction
428
+ -------------------------------------------------------
429
+ ActiveSortOrderTest: test_override_base_sort_order_only
430
+ -------------------------------------------------------
431
+  (0.1ms) SELECT COUNT(*) FROM "posts"
432
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
433
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
434
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
435
+ TRANSACTION (0.0ms) rollback transaction
436
+ TRANSACTION (0.0ms) begin transaction
437
+ -------------------------------------------------------
438
+ ActiveSortOrderTest: test_base_sort_order_default_value
439
+ -------------------------------------------------------
440
+ TRANSACTION (0.0ms) rollback transaction
441
+ TRANSACTION (0.0ms) begin transaction
442
+ --------------------------------------------------
443
+ ActiveSortOrderTest: test_base_sort_order_and_sort
444
+ --------------------------------------------------
445
+  (0.1ms) SELECT COUNT(*) FROM "posts"
446
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
447
+ PostWithBaseOrderB Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.b DESC, posts.b ASC
448
+ TRANSACTION (0.0ms) rollback transaction
449
+ TRANSACTION (0.0ms) begin transaction
450
+ -------------------------------------------------
451
+ ActiveSortOrderTest: test_sort_on_multiple_fields
452
+ -------------------------------------------------
453
+  (0.1ms) SELECT COUNT(*) FROM "posts"
454
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY b asc, a asc, posts.a ASC
455
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY b desc, a desc, posts.a ASC
456
+ TRANSACTION (0.0ms) rollback transaction
457
+  (0.7ms) SELECT sqlite_version(*)
458
+  (0.1ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? [["key", "schema_sha1"]]
459
+  (0.0ms) SELECT sqlite_version(*)
460
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
461
+  (0.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
462
+ ActiveRecord::InternalMetadata Load (0.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ? [["key", "environment"], ["LIMIT", 1]]
463
+  (1.3ms) DELETE FROM posts;
464
+  (0.7ms) UPDATE `sqlite_sequence` SET `seq` = 0 WHERE `name` = 'posts';
465
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 1], ["b", 3], ["LIMIT", 1]]
466
+ TRANSACTION (0.0ms) begin transaction
467
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 1], ["b", 3]]
468
+ TRANSACTION (0.7ms) commit transaction
469
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 2], ["b", 2], ["LIMIT", 1]]
470
+ TRANSACTION (0.0ms) begin transaction
471
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 2], ["b", 2]]
472
+ TRANSACTION (0.6ms) commit transaction
473
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 3], ["b", 2], ["LIMIT", 1]]
474
+ TRANSACTION (0.0ms) begin transaction
475
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 3], ["b", 2]]
476
+ TRANSACTION (0.8ms) commit transaction
477
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 4], ["b", 1], ["LIMIT", 1]]
478
+ TRANSACTION (0.0ms) begin transaction
479
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 4], ["b", 1]]
480
+ TRANSACTION (0.7ms) commit transaction
481
+ Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."a" = ? AND "posts"."b" = ? LIMIT ? [["a", 5], ["b", 1], ["LIMIT", 1]]
482
+ TRANSACTION (0.0ms) begin transaction
483
+ Post Create (0.2ms) INSERT INTO "posts" ("a", "b") VALUES (?, ?) [["a", 5], ["b", 1]]
484
+ TRANSACTION (0.7ms) commit transaction
485
+ TRANSACTION (0.0ms) begin transaction
486
+ -------------------------------------------------
487
+ ActiveSortOrderTest: test_sort_on_multiple_fields
488
+ -------------------------------------------------
489
+  (0.1ms) SELECT COUNT(*) FROM "posts"
490
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY b asc, a asc, posts.a ASC
491
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY b desc, a desc, posts.a ASC
492
+ TRANSACTION (0.1ms) rollback transaction
493
+ TRANSACTION (0.0ms) begin transaction
494
+ ----------------------------------------------------
495
+ ActiveSortOrderTest: test_class_base_sort_order_only
496
+ ----------------------------------------------------
497
+  (0.1ms) SELECT COUNT(*) FROM "posts"
498
+ PostWithBaseOrderA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC
499
+ PostWithBaseOrderB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
500
+ PostWithBaseOrderAAndB Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.a ASC, posts.b ASC
501
+ PostWithBaseOrderBAndA Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC, posts.a ASC
502
+ TRANSACTION (0.0ms) rollback transaction
503
+ TRANSACTION (0.0ms) begin transaction
504
+ --------------------------------------------------
505
+ ActiveSortOrderTest: test_base_sort_order_and_sort
506
+ --------------------------------------------------
507
+  (0.1ms) SELECT COUNT(*) FROM "posts"
508
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
509
+ PostWithBaseOrderB Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.b DESC, posts.b ASC
510
+ TRANSACTION (0.0ms) rollback transaction
511
+ TRANSACTION (0.0ms) begin transaction
512
+ -----------------------------------------
513
+ ActiveSortOrderTest: test_exposes_version
514
+ -----------------------------------------
515
+ TRANSACTION (0.0ms) rollback transaction
516
+ TRANSACTION (0.0ms) begin transaction
517
+ -----------------------------------
518
+ ActiveSortOrderTest: test_sort_only
519
+ -----------------------------------
520
+  (0.1ms) SELECT COUNT(*) FROM "posts"
521
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY a desc, posts.a ASC
522
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.a DESC, posts.a ASC
523
+ TRANSACTION (0.0ms) rollback transaction
524
+ TRANSACTION (0.0ms) begin transaction
525
+ -------------------------------------------------------
526
+ ActiveSortOrderTest: test_base_sort_order_default_value
527
+ -------------------------------------------------------
528
+ TRANSACTION (0.0ms) rollback transaction
529
+ TRANSACTION (0.0ms) begin transaction
530
+ -------------------------------------------------------
531
+ ActiveSortOrderTest: test_override_base_sort_order_only
532
+ -------------------------------------------------------
533
+  (0.1ms) SELECT COUNT(*) FROM "posts"
534
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY posts.b ASC
535
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
536
+ PostWithBaseOrderA Load (0.0ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC
537
+ TRANSACTION (0.0ms) rollback transaction
538
+ TRANSACTION (0.0ms) begin transaction
539
+ ---------------------------------------------
540
+ ActiveSortOrderTest: test_exposes_main_module
541
+ ---------------------------------------------
542
+ TRANSACTION (0.0ms) rollback transaction
@@ -121,4 +121,24 @@ class ActiveSortOrderTest < ActiveSupport::TestCase
121
121
  end
122
122
  end
123
123
 
124
+ def test_sort_on_multiple_fields
125
+ assert_equal Post.all.count, DATA[:posts].count
126
+
127
+ expected = DATA[:posts].sort_by{|item| [item.b, item.a] }
128
+
129
+ sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :asc)
130
+
131
+ sorted.each_with_index do |item, i|
132
+ assert_equal expected[i].id, item.id
133
+ end
134
+
135
+ expected = DATA[:posts].sort_by{|item| [item.b, item.a] }.reverse
136
+
137
+ sorted = PostWithBaseOrderA.all.sort_order([:b, :a], :desc)
138
+
139
+ sorted.each_with_index do |item, i|
140
+ assert_equal expected[i].id, item.id
141
+ end
142
+ end
143
+
124
144
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_sort_order
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-28 00:00:00.000000000 Z
11
+ date: 2021-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord