order_query 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +6 -0
- data/README.md +1 -1
- data/lib/order_query/sql/where.rb +16 -15
- data/lib/order_query/version.rb +1 -1
- data/spec/gemfiles/rails_5_0.gemfile +2 -0
- data/spec/gemfiles/rails_5_1.gemfile +2 -0
- data/spec/gemfiles/rails_5_2.gemfile +2 -0
- data/spec/order_query_spec.rb +29 -30
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01cd918d06d860206e938baf5f500446132bdbd6
|
4
|
+
data.tar.gz: 39882d3d3d1fb272f88597ed281973618b6f293c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b23735d7393e0bc5e8842e644deb06f22618b50e68ed5537c1755c3e6dd88858189104d18cef2279d15fa9fc158dc8a3470570f980b13f929187e80be30d5d8
|
7
|
+
data.tar.gz: a40ec816d36f311647c37515adf8866151505a4205b5c46e0d42f22f47c1f9ef7816a290df711876157bfefe25849065ddb86aff1d5bf4b3f1967ae1d187560e
|
data/CHANGES.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## 0.4.1
|
2
|
+
|
3
|
+
* If a column had a `nulls:` option and there were multiple records with `NULL`,
|
4
|
+
all of these records but one were previously skipped. This is now fixed.
|
5
|
+
[#21](https://github.com/glebm/order_query/issues/21)
|
6
|
+
|
1
7
|
## 0.4.0
|
2
8
|
|
3
9
|
* Adds nulls ordering options `nulls: :first` and `nulls: :last`.
|
data/README.md
CHANGED
@@ -94,9 +94,11 @@ module OrderQuery
|
|
94
94
|
# @param [:before or :after] side
|
95
95
|
# @return [query, params] return query fragment for column values
|
96
96
|
# before / after the current one.
|
97
|
-
def where_side(col, side, strict
|
97
|
+
def where_side(col, side, strict, value = point.value(col))
|
98
98
|
if col.order_enum
|
99
99
|
where_in col, col.enum_side(value, side, strict)
|
100
|
+
elsif value.nil?
|
101
|
+
where_null col, side, strict
|
100
102
|
else
|
101
103
|
where_ray col, value, side, strict
|
102
104
|
end
|
@@ -126,24 +128,23 @@ module OrderQuery
|
|
126
128
|
RAY_OP = { asc: '>', desc: '<' }.freeze
|
127
129
|
NULLS_ORD = { first: 'IS NOT NULL', last: 'IS NULL' }.freeze
|
128
130
|
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
reverse = (mode == :before)
|
133
|
-
if from.nil?
|
134
|
-
["#{col.column_name} #{NULLS_ORD[col.nulls_direction(reverse)]}", []]
|
131
|
+
def where_null(col, mode, strict)
|
132
|
+
if strict && col.nulls_direction(mode == :before) != :last
|
133
|
+
["#{col.column_name} IS NOT NULL", []]
|
135
134
|
else
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
135
|
+
WHERE_IDENTITY
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def where_ray(col, from, mode, strict)
|
140
|
+
["#{col.column_name} "\
|
141
|
+
"#{RAY_OP[col.direction(mode == :before)]}#{'=' unless strict} ?",
|
142
|
+
[from]].tap do |ray|
|
143
|
+
if col.nullable? && col.nulls_direction(mode == :before) == :last
|
144
|
+
ray[0] = "(#{ray[0]} OR #{col.column_name} IS NULL)"
|
143
145
|
end
|
144
146
|
end
|
145
147
|
end
|
146
|
-
# rubocop:enable Metrics/AbcSize
|
147
148
|
|
148
149
|
WHERE_IDENTITY = ['', [].freeze].freeze
|
149
150
|
|
data/lib/order_query/version.rb
CHANGED
data/spec/order_query_spec.rb
CHANGED
@@ -368,7 +368,7 @@ RSpec.describe 'OrderQuery' do
|
|
368
368
|
"expected: #{post.title}.next == #{next_post.title}\n" \
|
369
369
|
" got: #{actual ? actual.title : 'nil'}\n" \
|
370
370
|
" all: #{space.scope.all.map(&:title)}\n" \
|
371
|
-
" sql: #{space.at(
|
371
|
+
" sql: #{space.at(post).after.limit(1).to_sql}"
|
372
372
|
expect(actual ? actual.title : nil).to eq(next_post.title),
|
373
373
|
failure_message
|
374
374
|
end
|
@@ -380,14 +380,35 @@ RSpec.describe 'OrderQuery' do
|
|
380
380
|
"expected: #{post.title}.previous == #{prev_post.title}\n" \
|
381
381
|
" got: #{actual ? actual.title : 'nil'}\n" \
|
382
382
|
" all: #{space.scope.all.map(&:title)}\n" \
|
383
|
-
" sql: #{space.at(
|
383
|
+
" sql: #{space.at(post).before.limit(1).to_sql}"
|
384
384
|
expect(actual ? actual.title : nil).to eq(prev_post.title),
|
385
385
|
failure_message
|
386
386
|
end
|
387
|
+
|
388
|
+
def expect_order(space, ordered)
|
389
|
+
actual = space.scope.all.map(&:title)
|
390
|
+
expected = ordered.map(&:title)
|
391
|
+
failure_message =
|
392
|
+
"expected: #{expected * ', '}\n"\
|
393
|
+
" got: #{actual * ', '}\n"\
|
394
|
+
" sql: #{space.scope.to_sql}"
|
395
|
+
expect(actual).to eq(expected), failure_message
|
396
|
+
|
397
|
+
ordered.each_cons(2) do |post, next_post|
|
398
|
+
expect_next space, post, next_post
|
399
|
+
expect_prev space, next_post, post
|
400
|
+
end
|
401
|
+
expect_next space, ordered.last, ordered.first
|
402
|
+
expect_prev space, ordered.first, ordered.last
|
403
|
+
end
|
404
|
+
|
387
405
|
# rubocop:enable Metrics/AbcSize
|
388
406
|
|
389
|
-
let! :
|
390
|
-
Post.create!(title: '
|
407
|
+
let! :null_1 do
|
408
|
+
Post.create!(title: 'null_1', published_at: nil).reload
|
409
|
+
end
|
410
|
+
let! :null_2 do
|
411
|
+
Post.create!(title: 'null_2', published_at: nil).reload
|
391
412
|
end
|
392
413
|
let! :older do
|
393
414
|
Post.create!(title: 'older', published_at: Time.now + 1.hour)
|
@@ -398,44 +419,22 @@ RSpec.describe 'OrderQuery' do
|
|
398
419
|
|
399
420
|
it 'orders nulls first (desc)' do
|
400
421
|
space = Post.seek([:published_at, :desc, nulls: :first])
|
401
|
-
|
402
|
-
actual = scope.all.map(&:title)
|
403
|
-
expected = [null, older, newer].map(&:title)
|
404
|
-
expect(actual).to eq(expected), scope.to_sql
|
405
|
-
expect_next space, older, newer
|
406
|
-
expect_prev space, newer, older
|
407
|
-
expect_prev space, older, null
|
408
|
-
expect_next space, null, older
|
422
|
+
expect_order space, [null_1, null_2, older, newer]
|
409
423
|
end
|
410
424
|
|
411
425
|
it 'orders nulls first (asc)' do
|
412
426
|
space = Post.seek([:published_at, :asc, nulls: :first])
|
413
|
-
|
414
|
-
actual = scope.all.map(&:title)
|
415
|
-
expected = [null, newer, older].map(&:title)
|
416
|
-
expect(actual).to eq(expected), scope.to_sql
|
417
|
-
expect_prev space, newer, null
|
418
|
-
expect_next space, null, newer
|
427
|
+
expect_order space, [null_1, null_2, newer, older]
|
419
428
|
end
|
420
429
|
|
421
430
|
it 'orders nulls last (desc)' do
|
422
431
|
space = Post.seek([:published_at, :desc, nulls: :last])
|
423
|
-
|
424
|
-
actual = scope.all.map(&:title)
|
425
|
-
expected = [older, newer, null].map(&:title)
|
426
|
-
expect(actual).to eq(expected), scope.to_sql
|
427
|
-
expect_next space, newer, null
|
428
|
-
expect_prev space, null, newer
|
432
|
+
expect_order space, [older, newer, null_1, null_2]
|
429
433
|
end
|
430
434
|
|
431
435
|
it 'orders nulls last (asc)' do
|
432
436
|
space = Post.seek([:published_at, :asc, nulls: :last])
|
433
|
-
|
434
|
-
actual = scope.all.map(&:title)
|
435
|
-
expected = [newer, older, null].map(&:title)
|
436
|
-
expect(actual).to eq(expected), scope.to_sql
|
437
|
-
expect_next space, older, null
|
438
|
-
expect_prev space, null, older
|
437
|
+
expect_order space, [newer, older, null_1, null_2]
|
439
438
|
end
|
440
439
|
end
|
441
440
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: order_query
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gleb Mazovetskiy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -82,16 +82,16 @@ dependencies:
|
|
82
82
|
name: rubocop
|
83
83
|
requirement: !ruby/object:Gem::Requirement
|
84
84
|
requirements:
|
85
|
-
- - "
|
85
|
+
- - "~>"
|
86
86
|
- !ruby/object:Gem::Version
|
87
|
-
version:
|
87
|
+
version: 0.53.0
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
90
|
version_requirements: !ruby/object:Gem::Requirement
|
91
91
|
requirements:
|
92
|
-
- - "
|
92
|
+
- - "~>"
|
93
93
|
- !ruby/object:Gem::Version
|
94
|
-
version:
|
94
|
+
version: 0.53.0
|
95
95
|
- !ruby/object:Gem::Dependency
|
96
96
|
name: simplecov
|
97
97
|
requirement: !ruby/object:Gem::Requirement
|