Sha256: 6015b6a394e92a00352e81abf49c7f8fe2e77162f5cdc98f02bb79446c814264

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module AjaxDatatablesRails
  module Datatable

    TRUE_VALUE = 'true'

    class Datatable
      attr_reader :datatable, :options

      def initialize(datatable)
        @datatable = datatable
        @options   = datatable.params
      end

      # ----------------- ORDER METHODS --------------------

      def orderable?
        options[:order].present?
      end

      def orders
        @orders ||= get_param(:order).map do |_, order_options|
          SimpleOrder.new(self, order_options)
        end
      end

      def order_by(how, what)
        orders.find { |simple_order| simple_order.send(how) == what }
      end

      # ----------------- SEARCH METHODS --------------------

      def searchable?
        options[:search].present? && options[:search][:value].present?
      end

      def search
        @search ||= SimpleSearch.new(options[:search])
      end

      # ----------------- COLUMN METHODS --------------------

      def columns
        @columns ||= get_param(:columns).map do |index, column_options|
          Column.new(datatable, index, column_options)
        end
      end

      def column_by(how, what)
        columns.find { |simple_column| simple_column.send(how) == what }
      end

      # ----------------- OPTIONS METHODS --------------------

      def paginate?
        per_page != -1
      end

      def per_page
        options.fetch(:length, 10).to_i
      end

      def offset
        options.fetch(:start, 0).to_i
      end

      def page
        (offset / per_page) + 1
      end

      def get_param(param)
        if AjaxDatatablesRails.old_rails?
          options[param]
        else
          options[param].to_unsafe_h.with_indifferent_access
        end
      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/datatable.rb
ajax-datatables-rails-0.4.2 lib/ajax-datatables-rails/datatable/datatable.rb
ajax-datatables-rails-0.4.1 lib/ajax-datatables-rails/datatable/datatable.rb