class AjaxDatatablesRails::Base

Constants

GLOBAL_SEARCH_DELIMITER

Attributes

datatable[R]
options[R]
params[R]

Public Class Methods

new(params, options = {}) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 10
def initialize(params, options = {})
  @params    = params
  @options   = options
  @datatable = Datatable::Datatable.new(self)
end

Public Instance Methods

additional_data() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 28
def additional_data
  {}
end
as_json(*) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 32
def as_json(*)
  {
    recordsTotal:    records_total_count,
    recordsFiltered: records_filtered_count,
    data:            sanitize_data(data),
  }.merge(additional_data)
end
connected_columns() click to toggle source

helper methods

# File lib/ajax-datatables-rails/base.rb, line 45
def connected_columns
  @connected_columns ||= begin
    view_columns.keys.map do |field_name|
      datatable.column_by(:data, field_name.to_s)
    end.compact
  end
end
data() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 24
def data
  raise(NotImplementedError, data_error_text)
end
get_raw_records() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 20
def get_raw_records
  raise(NotImplementedError, raw_records_error_text)
end
records() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 40
def records
  @records ||= retrieve_records
end
search_columns() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 59
def search_columns
  @search_columns ||= begin
    searchable_columns.select(&:searched?)
  end
end
searchable_columns() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 53
def searchable_columns
  @searchable_columns ||= begin
    connected_columns.select(&:searchable?)
  end
end
view_columns() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 16
def view_columns
  raise(NotImplementedError, view_columns_error_text)
end

Private Instance Methods

column_data(column) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 101
def column_data(column)
  id = column_id(column)
  params.dig('columns', id.to_s, 'search', 'value')
end
column_id(name) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 97
def column_id(name)
  view_columns.keys.index(name.to_sym)
end
data_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 114
    def data_error_text
      <<-ERROR

        You should implement this method in your class and return an array
        of arrays, or an array of hashes, as defined in the jQuery.dataTables
        plugin documentation.
      ERROR
    end
global_search_delimiter() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 93
def global_search_delimiter
  GLOBAL_SEARCH_DELIMITER
end
raw_records_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 106
    def raw_records_error_text
      <<-ERROR

        You should implement this method in your class and specify
        how records are going to be retrieved from the database.
      ERROR
    end
records_filtered_count() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 89
def records_filtered_count
  filter_records(fetch_records).count(:all)
end
records_total_count() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 85
def records_total_count
  fetch_records.count(:all)
end
retrieve_records() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 77
def retrieve_records
  records = fetch_records
  records = filter_records(records)
  records = sort_records(records)     if datatable.orderable?
  records = paginate_records(records) if datatable.paginate?
  records
end
sanitize_data(data) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 67
def sanitize_data(data)
  data.map do |record|
    if record.is_a?(Array)
      record.map { |td| ERB::Util.html_escape(td) }
    else
      record.update(record) { |_, v| ERB::Util.html_escape(v) }
    end
  end
end
view_columns_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 123
    def view_columns_error_text
      <<-ERROR

        You should implement this method in your class and return an array
        of database columns based on the columns displayed in the HTML view.
        These columns should be represented in the ModelName.column_name,
        or aliased_join_table.column_name notation.
      ERROR
    end