Sha256: 6e55f0620aae1c44988b5afa7d53898c6b6f9aab10474e8b8b61c17f8d13cfe0

Contents?: true

Size: 1.52 KB

Versions: 13

Compression:

Stored size: 1.52 KB

Contents

module RocketJobMissionControl
  class AbstractDatatable
    delegate :params, :link_to, :render, to: :@view
    delegate :h, to: "ERB::Util"

    attr_accessor :view, :query

    def initialize(view, query)
      @view  = view
      @query = query
      extract_query_params
    end

    def as_json(_options = {})
      {
        draw:            params[:draw].to_i,
        recordsTotal:    query.unfiltered_count,
        recordsFiltered: query.count,
        data:            query.query.collect { |record| map(record) }
      }
    end

    private

    def map(record)
      raise NotImplementedError
    end

    def extract_query_params
      # Search term
      search = params[:search]
      if search.present? && search[:value].present?
        query.search_term = search[:value] if search.present?
      end

      # Sort order
      if order_by = extract_sort(params[:order])
        query.order_by = order_by
      end

      # Pagination
      unless params[:length].present? && params[:length] == "-1"
        query.start     = params[:start].to_i
        query.page_size = params.fetch(:length, 10).to_i
      end
    end

    def extract_sort(order)
      return nil unless order.present?

      sort_by = {}
      order.each_pair do |_key, value|
        name = query.display_columns[value[:column].to_i]
        unless name.present?
          raise(ArgumentError, "Invalid column id: #{value[:column]}. Must fit #{query.display_columns.inspect}")
        end

        sort_by[name] = value[:dir]
      end
      sort_by
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rocketjob_mission_control-6.1.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.7 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.6 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.5 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.4 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.3 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.2 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.1 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-6.0.0.beta app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-5.0.1 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-5.0.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-5.0.0.beta1 app/datatables/rocket_job_mission_control/abstract_datatable.rb