Class: AjaxDatatablesRails::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ajax-datatables-rails/base.rb

Direct Known Subclasses

ActiveRecord

Constant Summary collapse

GLOBAL_SEARCH_DELIMITER =
' '

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, options = {}) ⇒ Base

Returns a new instance of Base.



33
34
35
36
37
# File 'lib/ajax-datatables-rails/base.rb', line 33

def initialize(params, options = {})
  @params    = params
  @options   = options
  @datatable = Datatable::Datatable.new(self)
end

Instance Attribute Details

#datatableObject (readonly)

Returns the value of attribute datatable.



29
30
31
# File 'lib/ajax-datatables-rails/base.rb', line 29

def datatable
  @datatable
end

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/ajax-datatables-rails/base.rb', line 29

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



29
30
31
# File 'lib/ajax-datatables-rails/base.rb', line 29

def params
  @params
end

Class Method Details

.default_db_adapterObject



15
16
17
18
19
20
21
22
23
# File 'lib/ajax-datatables-rails/base.rb', line 15

def default_db_adapter
  if rails_52?
    ::ActiveRecord::Base.configurations.dig(Rails.env, 'adapter').downcase.to_sym
  elsif rails_60?
    ::ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first.config['adapter'].downcase.to_sym
  else
    ::ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first.adapter.downcase.to_sym
  end
end

.rails_52?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/ajax-datatables-rails/base.rb', line 7

def rails_52?
  Rails.gem_version >= Gem::Version.new('5.2') && Rails.gem_version <= Gem::Version.new('6.0')
end

.rails_60?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/ajax-datatables-rails/base.rb', line 11

def rails_60?
  Rails.gem_version >= Gem::Version.new('6.0') && Rails.gem_version <= Gem::Version.new('6.1')
end

Instance Method Details

#additional_dataObject

User overides



70
71
72
# File 'lib/ajax-datatables-rails/base.rb', line 70

def additional_data
  {}
end

#as_jsonObject

JSON structure sent to jQuery DataTables



75
76
77
78
79
80
81
# File 'lib/ajax-datatables-rails/base.rb', line 75

def as_json(*)
  {
    recordsTotal:    records_total_count,
    recordsFiltered: records_filtered_count,
    data:            sanitize_data(data),
  }.merge(draw_id).merge(additional_data)
end

#column_data(column) ⇒ Object



88
89
90
91
# File 'lib/ajax-datatables-rails/base.rb', line 88

def column_data(column)
  id = column_id(column)
  params.dig('columns', id.to_s, 'search', 'value')
end

#column_id(name) ⇒ Object

User helper methods



84
85
86
# File 'lib/ajax-datatables-rails/base.rb', line 84

def column_id(name)
  view_columns.keys.index(name.to_sym)
end

#dataObject

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/ajax-datatables-rails/base.rb', line 48

def data
  raise(NotImplementedError, data_error_text)
end

#fetch_recordsObject

ORM defined methods



53
54
55
# File 'lib/ajax-datatables-rails/base.rb', line 53

def fetch_records
  get_raw_records
end

#filter_records(records) ⇒ Object

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/ajax-datatables-rails/base.rb', line 57

def filter_records(records)
  raise(NotImplementedError)
end

#get_raw_recordsObject

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/ajax-datatables-rails/base.rb', line 44

def get_raw_records
  raise(NotImplementedError, raw_records_error_text)
end

#paginate_records(records) ⇒ Object

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/ajax-datatables-rails/base.rb', line 65

def paginate_records(records)
  raise(NotImplementedError)
end

#sort_records(records) ⇒ Object

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/ajax-datatables-rails/base.rb', line 61

def sort_records(records)
  raise(NotImplementedError)
end

#view_columnsObject

User defined methods

Raises:

  • (NotImplementedError)


40
41
42
# File 'lib/ajax-datatables-rails/base.rb', line 40

def view_columns
  raise(NotImplementedError, view_columns_error_text)
end