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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d3d5bb8b4f22e2ed2f205ed413e75a41edd4348d
4
- data.tar.gz: 1f216dcc1ec1d112126c68af4a20e28e949663de
3
+ metadata.gz: 01cd918d06d860206e938baf5f500446132bdbd6
4
+ data.tar.gz: 39882d3d3d1fb272f88597ed281973618b6f293c
5
5
  SHA512:
6
- metadata.gz: c3f31357b9b6c3d53d3003c90fdce074bb80bd8f314d81c34e2f096da1d61b18d79b9ea053d3f103c11d962daa155ff4b941c54c23e1b91e7d3d170a43afc054
7
- data.tar.gz: c1c644de117104e827dbc58422c313f77d892f2d6671533e852fa4e64ae26feb57274a1415b1f9a30696f943541501e522b6de7fd36f060fee1eb880ba165388
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
@@ -11,7 +11,7 @@ This gem finds the next or previous record(s) relative to the current one effici
11
11
  Add to Gemfile:
12
12
 
13
13
  ```ruby
14
- gem 'order_query', '~> 0.4.0'
14
+ gem 'order_query', '~> 0.4.1'
15
15
  ```
16
16
 
17
17
  ## Usage
@@ -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 = true, value = point.value(col))
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
- # rubocop:disable Metrics/AbcSize
130
-
131
- def where_ray(col, from, mode, strict = true)
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
- ["#{col.column_name} " \
137
- "#{RAY_OP[col.direction(reverse)]}#{'=' unless strict} ?",
138
- [from]].tap do |ray|
139
- if col.nullable? && col.nulls_direction(reverse) == :last
140
- ray[0] += " OR #{col.column_name} IS NULL"
141
- ray[0] = "(#{ray[0]})"
142
- end
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OrderQuery
4
- VERSION = '0.4.0'
4
+ VERSION = '0.4.1'
5
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec path: '../../'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec path: '../../'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec path: '../../'
@@ -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(older).before.limit(1).to_sql}"
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(older).before.limit(1).to_sql}"
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! :null do
390
- Post.create!(title: 'null', published_at: nil).reload
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
- scope = space.scope
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
- scope = space.scope
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
- scope = space.scope
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
- scope = space.scope
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.0
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-02-06 00:00:00.000000000 Z
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: '0'
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: '0'
94
+ version: 0.53.0
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: simplecov
97
97
  requirement: !ruby/object:Gem::Requirement