Module: OrderQuery::NullsDirection
- Defined in:
- lib/order_query/nulls_direction.rb
Overview
Handles nulls :first and :last direction.
Constant Summary collapse
- DIRECTIONS =
%i[first last].freeze
Class Method Summary collapse
- .all ⇒ Object
- .connection_adapter(scope) ⇒ Object
-
.default(scope, dir) ⇒ :first, :last
The default nulls order, based on the given scope’s connection adapter name.
- .parse!(direction) ⇒ :first, :last
- .reverse(direction) ⇒ :first, :last
Class Method Details
.all ⇒ Object
10 11 12 |
# File 'lib/order_query/nulls_direction.rb', line 10 def all DIRECTIONS end |
.connection_adapter(scope) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/order_query/nulls_direction.rb', line 44 def connection_adapter(scope) if scope.respond_to?(:connection_db_config) # Rails >= 6.1.0 scope.connection_db_config.adapter else scope.connection_config[:adapter] end end |
.default(scope, dir) ⇒ :first, :last
Returns the default nulls order, based on the given scope’s connection adapter name.
34 35 36 37 38 39 40 41 42 |
# File 'lib/order_query/nulls_direction.rb', line 34 def default(scope, dir) case connection_adapter(scope) when /mysql|maria|sqlite|sqlserver/i (dir == :asc ? :first : :last) else # Oracle, Postgres (dir == :asc ? :last : :first) end end |
.parse!(direction) ⇒ :first, :last
23 24 25 26 27 28 |
# File 'lib/order_query/nulls_direction.rb', line 23 def parse!(direction) all.include?(direction) && direction or fail ArgumentError, "`nulls` must be in #{all.map(&:inspect).join(', ')}, "\ "is #{direction.inspect}" end |
.reverse(direction) ⇒ :first, :last
16 17 18 |
# File 'lib/order_query/nulls_direction.rb', line 16 def reverse(direction) all[(all.index(direction) + 1) % 2].to_sym end |