Module: AjaxDatatablesRails::AltApi::Datatable

Defined in:
lib/ajax-datatables-rails/alt-api/datatable.rb,
lib/ajax-datatables-rails/alt-api/datatable/column_def.rb,
lib/ajax-datatables-rails/alt-api/datatable/row_context.rb,
lib/ajax-datatables-rails/alt-api/datatable/view_columns.rb

Overview

Just include this in your application datatable ‘include ’ajax-datatables-rails/alt-api/datatable’‘

Defined Under Namespace

Modules: ClassMethods Classes: ColumnDef, RowContext, ViewColumns

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 12

def self.included(base)
  base.extend(ClassMethods)

  extend Forwardable

  attr_reader :view, :current_record
  alias_method :record, :current_record
  alias_method :r, :record
end

Instance Method Details

#base_modelObject



102
103
104
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 102

def base_model
  self.class.instance_variable_get(:@base_model)
end

#columnsObject

applies the current datatable instance to the columns



91
92
93
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 91

def columns
  @columns ||= self.class.columns.each { |c| c.datatable = self }
end

#dataObject

Raises:

  • (NotImplementedError)


117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 117

def data
  raise NotImplementedError if columns.empty?

  records_with_error_logging.map do |record|
    cell_methods = self.class::CellMethods if self.class.const_defined?(:CellMethods)
    row_context = RowContext.new(@view, record, cell_methods)

    columns.each_with_object({}) do |col, row|
      row[col.attr_name] = col.render(row_context)
      row
    end
  end
end

#initialize(params, opts = {}) ⇒ Object



85
86
87
88
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 85

def initialize(params, opts = {})
  @view = opts[:view_context]
  super
end

#js_columns(only: [], exclude: []) ⇒ Object

There are times that when conditionally showing columns, the column conditionals need access to the datatable instance.



97
98
99
100
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 97

def js_columns(only: [], exclude: [])
  columns
  self.class.js_columns(only: only, exclude: exclude)
end

#records_with_error_loggingObject

There are some hard to debug scenarios that come up with ajax-datatables-rails. This helps debug some problems. Usually the bug is caused by some mismatched expectation between what the view_columns are and how it is being used.



135
136
137
138
139
140
141
142
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 135

def records_with_error_logging
  @records ||= records
rescue NoMethodError => e
  if e.name == :fetch && e.receiver.nil?
    Rails.logger.error String.new("#{self.class.name} column problem. view_columns: #{view_columns.pretty_inspect}")
  end
  raise e
end

#view_columnsObject

Raises:

  • (NotImplementedError)


106
107
108
109
110
111
112
113
114
115
# File 'lib/ajax-datatables-rails/alt-api/datatable.rb', line 106

def view_columns
  raise NotImplementedError, 'Columns not defined' if columns.empty?

  @view_columns ||= begin
    columns.each_with_object(ViewColumns.new) do |col, cols|
      cols[col.attr_name] = col.view_column
      cols
    end
  end
end