Sha256: d0a3b589d3719d71a57d57b36684df37a56c265d4cd9350f6ad203f8584a74c3

Contents?: true

Size: 934 Bytes

Versions: 3

Compression:

Stored size: 934 Bytes

Contents

# frozen_string_literal: true

module AjaxDatatablesRails
  module Datatable
    class SimpleOrder

      DIRECTIONS = %w[DESC ASC].freeze

      def initialize(datatable, options = {})
        @datatable = datatable
        @options   = options
      end

      def query(sort_column)
        if sort_nulls_last?
          "CASE WHEN #{sort_column} IS NULL THEN 1 ELSE 0 END, #{sort_column} #{direction}"
        else
          "#{sort_column} #{direction}"
        end
      end

      def column
        @datatable.column_by(:index, column_index)
      end

      def direction
        DIRECTIONS.find { |dir| dir == column_direction } || 'ASC'
      end

      private

      def column_index
        @options[:column]
      end

      def column_direction
        @options[:dir].upcase
      end

      def sort_nulls_last?
        column.nulls_last? || AjaxDatatablesRails.config.nulls_last == true
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ajax-datatables-rails-0.4.3 lib/ajax-datatables-rails/datatable/simple_order.rb
ajax-datatables-rails-0.4.2 lib/ajax-datatables-rails/datatable/simple_order.rb
ajax-datatables-rails-0.4.1 lib/ajax-datatables-rails/datatable/simple_order.rb