# frozen_string_literal: true # Helper methods for rendering JQuery Datatables module DatatableHelper def default_datatable_columns(object) datatable_columns(object, ['actions'] + object.column_names) end def datatable_columns(object, cols) obj_name = object.to_s.underscore.pluralize cols.map do |col| if col == 'actions' { title: '', data: "#{obj_name.tr('/', '_')}__actions", class: 'actions center', bSortable: false } else { title: col.split('_').collect(&:capitalize).join(' ').to_s, data: "#{obj_name}__#{col}", visible: true } end end end def render_datatable(object, table_url, columns: nil, table_name: nil, aoData: [], initial_size: 25, table_views: { table: true, grid: false }, table_filters: {}, table_actions: nil, sort: [[0, 'asc']]) if table_name.nil? table_name = object.to_s.pluralize.underscore.tr('/', '_') end columns = default_datatable_columns(object) if columns.nil? render( partial: 'partials/table', locals: { table_name: table_name, aoData: aoData, columns: columns, sort: sort, table_filters: table_filters, table_views: table_views, initial_size: initial_size, table_actions: table_actions, table_url: table_url }.merge(request.query_parameters), formats: [:html] ) end def return_cookie_filters(cookie_key, filter_key, values: nil, defaults: nil, type: nil) raise "Error" if values.nil? && type != "boolean" values = ["false"] if type == "boolean" values = values.map {|v| v[1].to_s} if type == "select" || values[0].class == Array defaults ||= [] if type == "select" if cookies[cookie_key].present? json = JSON.parse(cookies[cookie_key]) if json[filter_key].present? checked_envs ||= json[filter_key] if type == "boolean" || type == "select" checked_envs ||= values.map {|v| json[filter_key].include?(v) ? v : nil }.compact end end checked_envs ||= defaults.nil? ? (type == "checkbox" ? values : nil) : defaults end end