binxtils 0.7.0 → 0.8.1

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: 58786a3391a102bc9a6e08a9420088cf9796ae49f425af65bd40504bb350ea37
4
- data.tar.gz: decb330bdb3ac5b78b24fdff8fa8431331a8933b4b5e6c06957850e7d5910de4
3
+ metadata.gz: c8b4a35440c2841b1f08baf31395cdf61bed5d1c0ec4ce33cc98a5dab8f43f22
4
+ data.tar.gz: 9da110f74dd61abf3896e6fae12e52ccbabef082338f9cd905ad8b70dad11859
5
5
  SHA512:
6
- metadata.gz: 8ebdeccb552c059b0f4d6951cf6e5305656c97a0d5949d8536fa920e96bce5b211ced41f9eb0252d4420973c3ef853286a2be01ae25ed0a3e63c55feac03d2cc
7
- data.tar.gz: 309384e08b2afb921202bd92fede339d559f29857a58b3ab23ed667652182a87252a4424c708d57e74345f6a4f523eb26426ce3c51cacacaaf835eb6efa7cb5f
6
+ metadata.gz: a002ee9f7a38fa824cc20a5a0e6c9ecfdb35f55f4b905155b1d05a449e3aec4dc16019bff709b20f9c9224d5c113ffa1723677dbb7b8647a65970182515498d2
7
+ data.tar.gz: f89dbdb5382f6b3e608ac7742d79cd9b935becf19ed554e59b0cf7b2bc34f647d9d33bf0061f0edadcaa44e977f8e8a6a66466c307f32f2d4f8fb64abc1685eb
data/README.md CHANGED
@@ -59,6 +59,14 @@ class BikesController < ApplicationController
59
59
  end
60
60
  ```
61
61
 
62
+ Order with `sortable_order`, which applies `sort_direction` and puts nils last. Pass the model to order by its `sort_column` — non-null columns skip `NULLS LAST`, since an index can't serve `DESC NULLS LAST`. It also takes an Arel node or SQL string, which are treated as nullable unless you pass `nulls_last:`:
63
+
64
+ ```ruby
65
+ Bike.order(sortable_order(Bike))
66
+ Bike.order(sortable_order(Bike.arel_table[sort_column].lower))
67
+ Bike.order(sortable_order("COALESCE((data -> 'bike_count')::integer, 0)", nulls_last: false))
68
+ ```
69
+
62
70
  ## npm package
63
71
 
64
72
  This repo also publishes `@bikeindex/time-localizer`, an npm package for localizing time elements in the browser. It ships two builds of the same source:
@@ -18,6 +18,17 @@ module Binxtils
18
18
  %w[asc desc].include?(params[:direction]) ? params[:direction] : default_direction
19
19
  end
20
20
 
21
+ # Desc puts nils last, matching asc; not for a model's non-null column, since an index can't serve DESC NULLS LAST
22
+ def sortable_order(expression = sort_column, nulls_last: !expression.is_a?(Class) || expression.columns_hash[sort_column]&.null != false)
23
+ node = if expression.is_a?(Class)
24
+ expression.arel_table[sort_column]
25
+ else
26
+ expression.respond_to?(:asc) ? expression : Arel.sql(expression)
27
+ end
28
+ ordering = node.public_send(sort_direction)
29
+ nulls_last ? ordering.nulls_last : ordering
30
+ end
31
+
21
32
  def permitted_time_range_columns
22
33
  %w[created_at updated_at].freeze
23
34
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Binxtils
4
- VERSION = "0.7.0"
4
+ VERSION = "0.8.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binxtils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bike Index