class TrkDatatables::Base

Attributes

column_key_options[RW]

Public Class Methods

new(view) click to toggle source

In tests you can use `spy(:view, default_proc: false)` when you want to initialize without exceptions when view.params is called or @params = ActiveSupport::HashWithIndifferentAccess.new params

# File lib/trk_datatables/base.rb, line 17
def initialize(view)
  @view = view
  @dt_params = DtParams.new view.params
  @column_key_options = ColumnKeyOptions.new columns, global_search_columns, predefined_ranges
  @preferences = Preferences.new preferences_holder, preferences_field, self.class.name

  # if @dt_params.dt_columns.size != @column_key_options.size
  #   raise Error, "dt_columns size of columns is #{@dt_params.dt_columns.size} \
  #   but column_key_options size is #{@column_key_options.size}"
  # end
end

Public Instance Methods

additional_data_for_json() click to toggle source
# File lib/trk_datatables/base.rb, line 176
def additional_data_for_json
  {}
end
all_items() click to toggle source

Get all items from db

@example

def all_items
  Post.joins(:users).published
end

@return [ActiveRecord::Relation]

# File lib/trk_datatables/base.rb, line 36
def all_items
  raise NotImplementedError, "You should implement #{__method__} method"
end
all_items_count() click to toggle source

helper for github.com/trkin/trk_datatables/issues/9 which you can override to support group query

# File lib/trk_datatables/base.rb, line 168
def all_items_count
  all_items.count
end
as_json(_attr = nil) click to toggle source

_attr is given by Rails template, prefix, layout… not used

# File lib/trk_datatables/base.rb, line 157
def as_json(_attr = nil)
  @dt_params.as_json(
    all_items_count,
    filtered_items_count,
    rows(ordered_paginated_filtered_items),
    additional_data_for_json
  )
end
columns() click to toggle source

Define columns of a table For simplest version you can notate column_keys as Array of strings @example

def column
  %w[posts.id posts.status users.name]
end

When you need customisation of some columns, you need to define Hash of column_key => { column_options } @example

def columns
  {
    'posts.id': {},
    'posts.status' => { search: false },
    'users.name' => { order: false },
  }
end

@return Array of Hash

# File lib/trk_datatables/base.rb, line 57
def columns
  raise NotImplementedError, "You should implement #{__method__} method #{link_to_rdoc self.class, __method__}"
end
default_order() click to toggle source
# File lib/trk_datatables/base.rb, line 121
def default_order
  [[0, :desc]].freeze
end
default_page_length() click to toggle source
# File lib/trk_datatables/base.rb, line 125
def default_page_length
  10
end
dt_orders_or_default_index_and_direction() click to toggle source

Returns dt_orders or default as array of index and direction datatables.net/reference/option/order @return

[
  [0, :desc],
]
# File lib/trk_datatables/base.rb, line 106
def dt_orders_or_default_index_and_direction
  return @dt_orders_or_default if defined? @dt_orders_or_default

  if columns.blank?
    @dt_orders_or_default = []
  elsif @dt_params.dt_orders.present?
    @dt_orders_or_default = @dt_params.dt_orders
    @preferences.set :order, @dt_params.dt_orders
  else
    check_value = ->(r) { r.is_a?(Array) && r[0].is_a?(Array) && r[0][0].is_a?(Integer) && r[0][0] < @column_key_options.size }
    @dt_orders_or_default = @preferences.get(:order, check_value) || default_order
  end
  @dt_orders_or_default
end
dt_per_page_or_default() click to toggle source
# File lib/trk_datatables/base.rb, line 129
def dt_per_page_or_default
  return @dt_per_page_or_default if defined? @dt_per_page_or_default

  @dt_per_page_or_default = \
    if @dt_params.dt_per_page.present?
      @preferences.set :per_page, @dt_params.dt_per_page
      @dt_params.dt_per_page
    else
      @preferences.get(:per_page) || default_page_length
    end
end
filter_by_columns(_all) click to toggle source
# File lib/trk_datatables/base.rb, line 91
def filter_by_columns(_all)
  raise 'filter_by_columns_is_defined_in_specific_orm' \
    "\n  Extent from TrkDatatables::ActiveRecord instead of TrkDatatables::Base"
end
filter_by_search_all(_all) click to toggle source
# File lib/trk_datatables/base.rb, line 87
def filter_by_search_all(_all)
  raise 'filter_by_columns_is_defined_in_specific_orm'
end
filtered_items() click to toggle source
# File lib/trk_datatables/base.rb, line 180
def filtered_items
  filter_by_search_all filter_by_columns all_items
end
filtered_items_count() click to toggle source
# File lib/trk_datatables/base.rb, line 172
def filtered_items_count
  filtered_items.count
end
global_search_columns() click to toggle source

Define columns that are not returned to page but only used as mathing for global search @example

def global_search_columns
  %w[name email].map {|col| "users.#{col}" } + %w[posts.body]
end
# File lib/trk_datatables/base.rb, line 67
def global_search_columns
  []
end
index_by_column_key(column_key) click to toggle source

We need this method publicly available since we use it for class method param_set

# File lib/trk_datatables/base.rb, line 143
def index_by_column_key(column_key)
  @column_key_options.index_by_column_key column_key
end
order_and_paginate_items(_filtered_items) click to toggle source
# File lib/trk_datatables/base.rb, line 96
def order_and_paginate_items(_filtered_items)
  raise 'order_and_paginate_items_is_defined_in_specific_orm'
end
ordered_paginated_filtered_items() click to toggle source
# File lib/trk_datatables/base.rb, line 184
def ordered_paginated_filtered_items
  order_and_paginate_items filter_by_search_all filter_by_columns all_items
end
param_get(column_key) click to toggle source

Helper to populate column search from params, used in RenderHtml#thead @example

@datatable.param_get('users.email')
# File lib/trk_datatables/base.rb, line 151
def param_get(column_key)
  column_index = index_by_column_key column_key
  @dt_params.param_get column_index
end
predefined_date_ranges() click to toggle source
# File lib/trk_datatables/base.rb, line 226
def predefined_date_ranges # rubocop:todo Metrics/AbcSize
  {
    'Today': Time.zone.today..Time.zone.today,
    'Yesterday': [Time.zone.today - 1.day, Time.zone.today - 1.day],
    'This Month': Time.zone.today.beginning_of_month...Time.zone.today,
    'Last Month': Time.zone.today.prev_month.beginning_of_month...Time.zone.today.prev_month.end_of_month,
    'This Year': Time.zone.today.beginning_of_year...Time.zone.today,
  }
end
predefined_datetime_ranges() click to toggle source
# File lib/trk_datatables/base.rb, line 236
def predefined_datetime_ranges # rubocop:todo Metrics/AbcSize
  {
    'Today': Time.zone.now.beginning_of_day..Time.zone.now.end_of_day,
    'Yesterday': [Time.zone.now.beginning_of_day - 1.day, Time.zone.now.end_of_day - 1.day],
    'This Month': Time.zone.today.beginning_of_month.beginning_of_day...Time.zone.now.end_of_day,
    'Last Month':
      Time.zone.today.prev_month.beginning_of_month.beginning_of_day...Time.zone.today.prev_month.end_of_month.end_of_day,
    'This Year': Time.zone.today.beginning_of_year.beginning_of_day...Time.zone.today.end_of_day,
  }
end
predefined_ranges() click to toggle source
# File lib/trk_datatables/base.rb, line 218
def predefined_ranges
  Time.zone ||= 'UTC'
  {
    date: predefined_date_ranges,
    datetime: predefined_datetime_ranges,
  }
end
preferences_field() click to toggle source

Override if you use different than :preferences You can generate with this command: @code

rails g migration add_preferences_to_users preferences:jsonb
# File lib/trk_datatables/base.rb, line 214
def preferences_field
  :preferences
end
preferences_holder() click to toggle source

Override this to set model where you can store order, index, page length @example

def preferences_holder
  @view.current_user
end
# File lib/trk_datatables/base.rb, line 206
def preferences_holder
  nil
end
render_html(search_link = nil, html_options = {}) click to toggle source
# File lib/trk_datatables/base.rb, line 192
def render_html(search_link = nil, html_options = {})
  if search_link.is_a? Hash
    html_options = search_link
    search_link = nil
  end
  render = RenderHtml.new(search_link, self, html_options)
  render.result
end
rows(_page_items) click to toggle source

Define page data @example

def rows(page_items)
  page_items.map do |post|
  post_status = @view.content_tag :span, post.status, class: "label label-#{@view.convert_status_to_class post.status}"
    [
      post.id,
      post_status,
      @view.link_to(post.user.name, post.user)
    ]
  end
end
# File lib/trk_datatables/base.rb, line 83
def rows(_page_items)
  raise NotImplementedError, "You should implement #{__method__} method"
end