Sha256: 0806c3dd52744d6af2b23165dc9336b2786e1be652af575983ac89b52cbc4575

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module AjaxDatatablesRails
  module Datatable
    class SimpleOrder

      DIRECTION_ASC  = 'ASC'
      DIRECTION_DESC = 'DESC'
      DIRECTIONS     = [DIRECTION_ASC, DIRECTION_DESC].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 } || 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

1 entries across 1 versions & 1 rubygems

Version Path
ajax-datatables-rails-1.0.0 lib/ajax-datatables-rails/datatable/simple_order.rb