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 13
def initialize(params, options = {})
  @params    = params
  @options   = options
  @datatable = Datatable::Datatable.new(self)
end

Public Instance Methods

additional_data() click to toggle source

User overides

# File lib/ajax-datatables-rails/base.rb, line 50
def additional_data
  {}
end
as_json(*) click to toggle source

JSON structure sent to jQuery DataTables

# File lib/ajax-datatables-rails/base.rb, line 55
def as_json(*)
  {
    recordsTotal:    records_total_count,
    recordsFiltered: records_filtered_count,
    data:            sanitize_data(data),
  }.merge(additional_data)
end
column_data(column) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 68
def column_data(column)
  id = column_id(column)
  params.dig('columns', id.to_s, 'search', 'value')
end
column_id(name) click to toggle source

User helper methods

# File lib/ajax-datatables-rails/base.rb, line 64
def column_id(name)
  view_columns.keys.index(name.to_sym)
end
data() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 28
def data
  raise(NotImplementedError, data_error_text)
end
fetch_records() click to toggle source

ORM defined methods

# File lib/ajax-datatables-rails/base.rb, line 33
def fetch_records
  get_raw_records
end
filter_records(records) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 37
def filter_records(records)
  raise(NotImplementedError)
end
get_raw_records() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 24
def get_raw_records
  raise(NotImplementedError, raw_records_error_text)
end
paginate_records(records) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 45
def paginate_records(records)
  raise(NotImplementedError)
end
sort_records(records) click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 41
def sort_records(records)
  raise(NotImplementedError)
end
view_columns() click to toggle source

User defined methods

# File lib/ajax-datatables-rails/base.rb, line 20
def view_columns
  raise(NotImplementedError, view_columns_error_text)
end

Private Instance Methods

connected_columns() click to toggle source

helper methods

# File lib/ajax-datatables-rails/base.rb, line 76
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_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 139
    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 127
def global_search_delimiter
  GLOBAL_SEARCH_DELIMITER
end
raw_records_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 131
    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() click to toggle source

called from within data

# File lib/ajax-datatables-rails/base.rb, line 107
def records
  @records ||= retrieve_records
end
records_filtered_count() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 123
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 119
def records_total_count
  fetch_records.count(:all)
end
retrieve_records() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 111
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 96
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
search_columns() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 90
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 84
def searchable_columns
  @searchable_columns ||= begin
    connected_columns.select(&:searchable?)
  end
end
view_columns_error_text() click to toggle source
# File lib/ajax-datatables-rails/base.rb, line 148
    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