Sha256: f52c25cbf880be9878d495b20049faef5a061ee55b9c25eb76c48c6bc18aabe5

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 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]
        raise(ArgumentError, "Invalid column id: #{value[:column]}. Must fit #{query.display_columns.inspect}") unless name.present?
        sort_by[name] = value[:dir]
      end
      sort_by
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rocketjob_mission_control-4.3.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-4.2.1 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-4.2.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-4.1.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-4.0.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb
rocketjob_mission_control-3.2.0 app/datatables/rocket_job_mission_control/abstract_datatable.rb