Sha256: ffdd29a26a28a4b4a024627cbd6e42967beeea8703993bb5d4259fe302c55217

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# rubocop:disable Naming/MethodName
# rubocop:disable Naming/UncommunicativeMethodParamName

module Arel
  module Nodes
    Arel::Nodes::Ordering.class_eval do
      # Postgres: https://www.postgresql.org/docs/9.4/queries-order.html
      attr_accessor :nulls

      def initialize(expr, nulls = 0)
        super(expr)

        @nulls = nulls
      end
    end
  end

  module Visitors
    class ToSql
      alias old_visit_Arel_Nodes_Ascending visit_Arel_Nodes_Ascending
      def visit_Arel_Nodes_Ascending(o, collector)
        old_visit_Arel_Nodes_Ascending(o, collector)
        apply_ordering_nulls(o, collector)
      end

      alias old_visit_Arel_Nodes_Descending visit_Arel_Nodes_Descending
      def visit_Arel_Nodes_Descending(o, collector)
        old_visit_Arel_Nodes_Descending(o, collector)
        apply_ordering_nulls(o, collector)
      end

      def apply_ordering_nulls(o, collector)
        case o.nulls
        when 1
          collector << ' NULLS FIRST'
        when 2
          collector << ' NULLS LAST'
        else
          collector
        end
      end
    end
  end
end

# rubocop:enable Naming/MethodName
# rubocop:enable Naming/UncommunicativeMethodParamName

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arel_toolkit-0.3.0 lib/arel/extensions/ordering.rb
arel_toolkit-0.2.0 lib/arel/extensions/ordering.rb