File tree Expand file tree Collapse file tree 4 files changed +25
-1
lines changed
Expand file tree Collapse file tree 4 files changed +25
-1
lines changed Original file line number Diff line number Diff line change @@ -278,6 +278,14 @@ Ransack.configure do |c|
278278end
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+
281289See this feature: https://www.postgresql.org/docs/13/queries-order.html
282290
283291#### Case Insensitive Sorting in PostgreSQL
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments