Class: AjaxDatatablesRails::AltApi::Datatable::ColumnDef

Inherits:
Object
  • Object
show all
Defined in:
lib/ajax-datatables-rails/alt-api/datatable/column_def.rb

Overview

This is a datatable column definition

Constant Summary collapse

ColumnDefError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, source: nil, sortable: true, visible: true, searchable: true, condition: nil, cond: :like, search_only: false, display_only: false, &cell_renderer) ⇒ ColumnDef

rubocop:disable Metrics/ParameterLists,Metrics/AbcSize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 14

def initialize(attr_name, # rubocop:disable Metrics/ParameterLists,Metrics/AbcSize
               source: nil,
               sortable: true,
               visible: true,
               searchable: true,
               condition: nil,
               cond: :like,
               search_only: false,
               display_only: false,
               &cell_renderer)
  @attr_name = attr_name
  @source = source
  if display_only
    visible = true
    searchable = false
    sortable = false
  end
  if search_only
    visible = false
    searchable = true
    sortable = false
  end
  @condition = condition
  @visible = visible
  @sortable = visible ? sortable : false
  @cell_renderer = cell_renderer
  @searchable = searchable
  @search_only = search_only
  @cond = cond
  @datatable = nil
end

Instance Attribute Details

#attr_nameObject (readonly)

Returns the value of attribute attr_name.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def attr_name
  @attr_name
end

#cell_rendererObject (readonly)

Returns the value of attribute cell_renderer.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def cell_renderer
  @cell_renderer
end

#condObject (readonly)

Returns the value of attribute cond.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def cond
  @cond
end

#conditionObject (readonly)

Returns the value of attribute condition.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def condition
  @condition
end

#datatableObject

Returns the value of attribute datatable.



12
13
14
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 12

def datatable
  @datatable
end

#search_onlyObject (readonly)

Returns the value of attribute search_only.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def search_only
  @search_only
end

#searchableObject (readonly)

Returns the value of attribute searchable.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def searchable
  @searchable
end

#sortableObject (readonly)

Returns the value of attribute sortable.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def sortable
  @sortable
end

#sourceObject (readonly)

Returns the value of attribute source.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def source
  @source
end

#visibleObject (readonly)

Returns the value of attribute visible.



10
11
12
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 10

def visible
  @visible
end

Instance Method Details

#as_jsonObject

Used to serialize options passed to the JS datatable initializer columns



47
48
49
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 47

def as_json
  { data: attr_name, visible: visible, sortable: sortable } if condition_met?
end

#render(row_context) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 62

def render(row_context) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  return unless render?

  record = row_context.record

  if cell_renderer
    if cell_renderer.arity == 1
      row_context.instance_exec(record, &cell_renderer)
    else
      row_context.instance_exec(&cell_renderer)
    end
  elsif record.respond_to?(attr_name)
    record.send(attr_name)
  else
    raise ColumnDefError, "Unable to render #{attr_name} for datatable: #{datatable.class.name}"
  end
end

#render?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 58

def render?
  visible && condition_met?
end

#view_columnObject



51
52
53
54
55
56
# File 'lib/ajax-datatables-rails/alt-api/datatable/column_def.rb', line 51

def view_column
  { source: search_source,
    orderable: to_bool(sortable),
    searchable: to_bool(searchable),
    cond: cond }
end