Sha256: 3df5f3703cb4daf8b3327b8bdfbcb41e7937741651855bdb1fcca1ec12af616d

Contents?: true

Size: 1.43 KB

Versions: 10

Compression:

Stored size: 1.43 KB

Contents

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

module Arel
  module Nodes
    class Ordering
      module OrderingExtension
        # 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

      prepend OrderingExtension
    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

    class Dot
      module OrderingExtension
        def visit_Arel_Nodes_Ordering(o)
          super
          visit_edge o, 'nulls'
        end
      end

      prepend OrderingExtension
    end
  end
end

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
arel_toolkit-0.4.9 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.8 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.7 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.6 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.5 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.4 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.3 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.2 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.1 lib/arel/extensions/ordering.rb
arel_toolkit-0.4.0 lib/arel/extensions/ordering.rb