Sha256: 7f7db54ce9d5104a96ebe32dfa01ed1339769318df23be47f6af24fcc5ede200

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 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)
        [sort_column, direction, nulls_last_sql].compact.join(' ')
      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

      def nulls_last_sql
        return unless sort_nulls_last?

        case AjaxDatatablesRails.config.db_adapter
        when :pg, :postgresql, :postgres, :oracle
          'NULLS LAST'
        when :mysql, :mysql2, :sqlite, :sqlite3
          'IS NULL'
        else
          raise 'unsupported database adapter'
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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