Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NULL FIRST and NULL LAST for most databases #1463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### 🚀 Features

* Add Rails 7.1.0 support by @yuki24 in https://github.com/activerecord-hackery/ransack/pull/1439
* Add ability to config any database (apart from MySQL) ORDER BY ... NULLS FIRST or NULLS LAST by @tttffff in https://github.com/activerecord-hackery/ransack/pull/1463

### 🐛 Bug Fixes

Expand Down
7 changes: 4 additions & 3 deletions docs/docs/getting-started/simple-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,24 @@ Ransack's `sort_url` helper is like a `sort_link` but returns only the url
default_order: { last_name: 'asc', first_name: 'desc' }) %>
```

### PostgreSQL's sort option
### Fields sort option

Does not work for MySQL.
The `NULLS FIRST` and `NULLS LAST` options can be used to determine whether nulls appear before or after non-null values in the sort ordering.

You may want to configure it like this:

```ruby
Ransack.configure do |c|
c.postgres_fields_sort_option = :nulls_first # or :nulls_last
c.fields_sort_option = :nulls_first # or :nulls_last
end
```

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

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

Expand Down
10 changes: 5 additions & 5 deletions lib/ransack/adapters/active_record/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def evaluate(search, opts = {})
if scope_or_sort.is_a?(Symbol)
relation = relation.send(scope_or_sort)
else
case Ransack.options[:postgres_fields_sort_option]
case Ransack.options[:fields_sort_option]
when :nulls_first
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")
scope_or_sort = scope_or_sort.direction == :asc ? scope_or_sort.nulls_first : scope_or_sort.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")
scope_or_sort = scope_or_sort.direction == :asc ? scope_or_sort.nulls_last : scope_or_sort.nulls_first
when :nulls_always_first
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS FIRST")
scope_or_sort = scope_or_sort.nulls_first
when :nulls_always_last
scope_or_sort = Arel.sql("#{scope_or_sort.to_sql} NULLS LAST")
scope_or_sort = scope_or_sort.nulls_last
end

relation = relation.order(scope_or_sort)
Expand Down
8 changes: 4 additions & 4 deletions lib/ransack/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def []=(key, value)
down_arrow: '▲'.freeze,
default_arrow: nil,
sanitize_scope_args: true,
postgres_fields_sort_option: nil,
fields_sort_option: nil,
strip_whitespace: true
}

Expand Down Expand Up @@ -162,13 +162,13 @@ 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 e.g. :nulls_always_last
# c.fields_sort_option = :nulls_first # or e.g. :nulls_always_last
# end
#
# See this feature: https://www.postgresql.org/docs/13/queries-order.html
#
def postgres_fields_sort_option=(setting)
self.options[:postgres_fields_sort_option] = setting
def fields_sort_option=(setting)
self.options[:fields_sort_option] = setting
end

# By default, Ransack displays sort order indicator arrows in sort links.
Expand Down
6 changes: 3 additions & 3 deletions spec/ransack/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ module Ransack
end
end

it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "fields sort option" do
default = Ransack.options.clone

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_first }
Ransack.configure { |c| c.fields_sort_option = :nulls_first }

expect(Ransack.options[:postgres_fields_sort_option]).to eq :nulls_first
expect(Ransack.options[:fields_sort_option]).to eq :nulls_first

Ransack.options = default
end
Expand Down
16 changes: 8 additions & 8 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -614,19 +614,19 @@ def remove_quotes_and_backticks(str)
expect(@s.result.first.id).to eq 1
end

it "PG's sort option", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "fields sort option", if: ::ActiveRecord::Base.connection.adapter_name != "Mysql2" do
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Arel does implement working nulls_first() and nulls_last() for MySQL, or if Ransack is ever updated to implement it, then we will need specific MySQL tests as the generated SQL will be different.

default = Ransack.options.clone

s = Search.new(Person, s: 'name asc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" ASC"

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

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_last }
Ransack.configure { |c| c.fields_sort_option = :nulls_last }
s = Search.new(Person, s: 'name asc')
expect(s.result.to_sql).to eq "SELECT \"people\".* FROM \"people\" ORDER BY \"people\".\"name\" ASC NULLS LAST"
s = Search.new(Person, s: 'name desc')
Expand All @@ -635,31 +635,31 @@ def remove_quotes_and_backticks(str)
Ransack.options = default
end

it "PG's sort option with double name", if: ::ActiveRecord::Base.connection.adapter_name == "PostgreSQL" do
it "fields sort option with double name", if: ::ActiveRecord::Base.connection.adapter_name != "Mysql2" do
default = Ransack.options.clone

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"

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_first }
Ransack.configure { |c| c.fields_sort_option = :nulls_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 LAST"

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_last }
Ransack.configure { |c| c.fields_sort_option = :nulls_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 FIRST"

Ransack.configure { |c| c.postgres_fields_sort_option = :nulls_always_first }
Ransack.configure { |c| c.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 }
Ransack.configure { |c| c.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')
Expand Down
Loading