Skip to content

Commit 00d3c25

Browse files
authored
Merge pull request #1226 from mollerhoj/allow_null_always_first_or_last
allow ransack to treat nulls as always first or last
2 parents 63990fc + c8f5087 commit 00d3c25

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ Ransack.configure do |c|
278278
end
279279
```
280280

281+
To treat nulls as having the lowest or highest value respectively. To force nulls to always be first or last, use
282+
283+
```rb
284+
Ransack.configure do |c|
285+
c.postgres_fields_sort_option = :nulls_always_first # or :nulls_always_last
286+
end
287+
```
288+
281289
See this feature: https://www.postgresql.org/docs/13/queries-order.html
282290

283291
#### Case Insensitive Sorting in PostgreSQL

lib/ransack/adapters/active_record/context.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def evaluate(search, opts = {})
4747
scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST") : Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
4848
when :nulls_last
4949
scope_or_sort = scope_or_sort.direction == :asc ? Arel.sql("#{scope_or_sort.to_sql} NULLS LAST") : Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
50+
when :nulls_always_first
51+
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
52+
when :nulls_always_last
53+
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
5054
end
5155

5256
relation = relation.order(scope_or_sort)

lib/ransack/configuration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def sanitize_custom_scope_booleans=(boolean)
149149
# User may want to configure it like this:
150150
#
151151
# Ransack.configure do |c|
152-
# c.postgres_fields_sort_option = :nulls_first # or :nulls_last
152+
# c.postgres_fields_sort_option = :nulls_first # or e.g. :nulls_always_last
153153
# end
154154
#
155155
# See this feature: https://www.postgresql.org/docs/13/queries-order.html

spec/ransack/search_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,18 @@ def remove_quotes_and_backticks(str)
605605
s = Search.new(Person, s: 'doubled_name desc')
606606
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST"
607607

608+
Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_first }
609+
s = Search.new(Person, s: 'doubled_name asc')
610+
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS FIRST"
611+
s = Search.new(Person, s: 'doubled_name desc')
612+
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST"
613+
614+
Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_last }
615+
s = Search.new(Person, s: 'doubled_name asc')
616+
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS LAST"
617+
s = Search.new(Person, s: 'doubled_name desc')
618+
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS LAST"
619+
608620
Ransack.options = default
609621
end
610622
end

0 commit comments

Comments
 (0)