class TrkDatatables::DtParams

This class wraps databases format datatables.net/manual/server-side#Returned-data

In future we can use some another datatables like github.com/gregnb/mui-datatables github.com/handsontable/handsontable github.com/cloudflarearchive/backgrid (archived)

Public Class Methods

form_field_name(column_index) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 113
def self.form_field_name(column_index)
  "columns[#{column_index}][search][value]"
end
new(params) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 11
def initialize(params)
  params.permit! if params.respond_to? :permit!
  @params = ActiveSupport::HashWithIndifferentAccess.new params
end
param_set(column_index, value) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 109
def self.param_set(column_index, value)
  {columns: {column_index.to_s => {search: {value: value}}}}
end
sample_params(options = {}) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 129
def self.sample_params(options = {})
  HashWithIndifferentAccess.new(
    draw: '1',
    start: '0',
    length: '10',
    search: {
      value: '', regex: 'false'
    },
    order: {
      '0': {column: '0', dir: 'desc'}
    },
    # [:columns] should have the same size as column_key_options since we
    # ignore keys, and use positions
    columns: {
      '0': {
        searchable: 'true',
        orderable: 'true',
        search: {
          value: '', regex: 'false'
        }
      },
      '1': {
        searchable: 'true',
        orderable: 'true',
        search: {
          value: '', regex: 'false'
        }
      },
      '2': {
        searchable: 'true',
        orderable: 'false',
        search: {
          value: '', regex: 'false'
        }
      },
    },
  ).merge options
end
sample_view_params(options = {}) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 123
def self.sample_view_params(options = {})
  OpenStruct.new(
    params: sample_params(options),
  )
end

Public Instance Methods

as_json(all_count, filtered_count, data, additional = {}) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 95
def as_json(all_count, filtered_count, data, additional = {})
  additional = {} if additional.nil?
  raise Error, 'additional_data_for_json needs to be a hash' unless additional.is_a? Hash

  draw = @params[:draw].to_i
  {
    draw: draw,
    recordsTotal: all_count,
    recordsFiltered: filtered_count,
    **additional,
    data: data,
  }
end
dt_columns() click to toggle source

Typecast so we can safelly use dt_column (Boolean), dt_column (Boolean), dt_column (String)

Returned size could be different from columns size, we match key from params to insert in appropriate place, and all other values are default @return

[
  { index: 0, searchable: true, orderable: true, search_value: 'dule' },
]
# File lib/trk_datatables/dt_params.rb, line 63
def dt_columns
  return @dt_columns if defined? @dt_columns

  @dt_columns = []
  return @dt_columns unless @params[:columns].respond_to? :each

  @params[:columns].each.map do |(dt_position, dt_column)|
    @dt_columns[dt_position.to_i] = {
      index: dt_position.to_i,
      searchable: dt_column[:searchable].to_s != 'false', # if nil as it is in set_params, than use true
      orderable: dt_column[:orderable].to_s != 'false', # if nil as it is in set_params, than use true
      search_value: (dt_column[:search] && dt_column[:search][:value]) || '',
    }
  end
  @dt_columns.each_with_index do |dt_column, i|
    next unless dt_column.nil?

    @dt_columns[i] = {
      index: i,
      searchable: true,
      orderable: true,
      search_value: '',
    }
  end
end
dt_offset() click to toggle source
# File lib/trk_datatables/dt_params.rb, line 16
def dt_offset
  @params[:start].to_i
end
dt_orders() click to toggle source

Typecast so we can safelly use dt_order (Integer) and dt_order (:asc/:desc) @return

[
  [ 2, :asc ],
  [ 1, :desc ],
]
# File lib/trk_datatables/dt_params.rb, line 37
def dt_orders
  return @dt_orders if defined? @dt_orders

  @dt_orders = []
  return @dt_orders if @params[:order].blank?

  @dt_orders = \
    @params[:order].each_with_object([]) do |(_index, dt_order), a|
      # for order we ignore key (_index) since order is preserved
      a << [
        dt_order[:column].to_i,
        dt_order[:dir]&.to_s&.casecmp('ASC')&.zero? ? :asc : :desc,
      ]
    end
  @dt_orders
end
dt_per_page() click to toggle source
# File lib/trk_datatables/dt_params.rb, line 20
def dt_per_page
  return if @params[:length].blank?

  @params[:length].to_i
end
param_get(column_index) click to toggle source
# File lib/trk_datatables/dt_params.rb, line 117
def param_get(column_index)
  @params.dig :columns, column_index.to_s, :search, :value
rescue TypeError => e
  raise Error, "#{e.message}. Column search is in a format: { \"columns\": { \"0\": { \"search\": { \"value\": { \"ABC\" } } } } }"
end
search_all() click to toggle source
# File lib/trk_datatables/dt_params.rb, line 89
def search_all
  @params.dig(:search, :value) || ''
rescue TypeError => e
  raise Error, e.message + '. Global search is in a format: { "search": { "value": "ABC" } }'
end