mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-18 15:13:21 +08:00
40 lines
1.1 KiB
Ruby
40 lines
1.1 KiB
Ruby
class CustomDatatable < AjaxDatatablesRails::Base
|
|
private
|
|
|
|
def dt_params
|
|
@dt_params ||=
|
|
params.permit(:assigned, :draw, :length, :start, search: %i(value regex))
|
|
.to_h
|
|
end
|
|
|
|
def columns_params
|
|
@columns_params ||= params.require(:columns).permit!.to_h
|
|
end
|
|
|
|
def order_params
|
|
@order_params ||=
|
|
params.require(:order).require('0').permit(:column, :dir).to_h
|
|
end
|
|
|
|
def fetch_records
|
|
records = get_raw_records
|
|
records = sort_records(records) if order_params.present?
|
|
records = filter_records(records) if dt_params[:search].present?
|
|
records = paginate_records(records) unless dt_params[:length].present? &&
|
|
dt_params[:length] == '-1'
|
|
records
|
|
end
|
|
|
|
def sort_records(records)
|
|
sort_by = "#{sort_column(order_params)} #{sort_direction(order_params)}"
|
|
records.order(sort_by)
|
|
end
|
|
|
|
def generate_sortable_displayed_columns
|
|
@sortable_displayed_columns = []
|
|
columns_params.each_value do |col|
|
|
@sortable_displayed_columns << col[:data] if col[:orderable] == 'true'
|
|
end
|
|
@sortable_displayed_columns
|
|
end
|
|
end
|