Skip to content

Commit

Permalink
Merge pull request #1226 from mollerhoj/allow_null_always_first_or_last
Browse files Browse the repository at this point in the history
allow ransack to treat nulls as always first or last
  • Loading branch information
scarroll32 authored Jun 26, 2021
2 parents 63990fc + c8f5087 commit 00d3c25
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ Ransack.configure do |c|
end
```

To treat nulls as having the lowest or highest value respectively. To force nulls to always be first or last, use

```rb
Ransack.configure do |c|
c.postgres_fields_sort_option = :nulls_always_first # or :nulls_always_last
end
```

See this feature: https://www.postgresql.org/docs/13/queries-order.html

#### Case Insensitive Sorting in PostgreSQL
Expand Down
4 changes: 4 additions & 0 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def evaluate(search, opts = {})
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")
when :nulls_last
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")
when :nulls_always_first
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
when :nulls_always_last
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
end

relation = relation.order(scope_or_sort)
Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def sanitize_custom_scope_booleans=(boolean)
# User may want to configure it like this:
#
# Ransack.configure do |c|
# c.postgres_fields_sort_option = :nulls_first # or :nulls_last
# c.postgres_fields_sort_option = :nulls_first # or e.g. :nulls_always_last
# end
#
# See this feature: https://www.postgresql.org/docs/13/queries-order.html
Expand Down
12 changes: 12 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,18 @@ def remove_quotes_and_backticks(str)
s = Search.new(Person, s: 'doubled_name desc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST"

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_first }
s = Search.new(Person, s: 'doubled_name asc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS FIRST"
s = Search.new(Person, s: 'doubled_name desc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS FIRST"

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_last }
s = Search.new(Person, s: 'doubled_name asc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" ASC NULLS LAST"
s = Search.new(Person, s: 'doubled_name desc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" || \"people\".\"name\" DESC NULLS LAST"

Ransack.options = default
end
end
Expand Down

0 comments on commit 00d3c25

Please sign in to comment.