Module: Effective::EffectiveDatatable::Dsl::Datatable

Included in:
DatatableDslTool
Defined in:
app/models/effective/effective_datatable/dsl/datatable.rb

Instance Method Summary collapse

Instance Method Details

#actions_col(btn_class: nil, col_class: nil, partial: nil, partial_as: nil, actions_partial: nil, responsive: 5000, visible: true, **actions, &format) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 97

def actions_col(btn_class: nil, col_class: nil, partial: nil, partial_as: nil, actions_partial: nil, responsive: 5000, visible: true, **actions, &format)
  raise 'You can only have one actions column' if datatable.columns[:_actions].present?

  datatable._columns[:_actions] = Effective::DatatableColumn.new(
    action: false,
    as: :actions,
    compute: nil,
    btn_class: (btn_class || 'btn-sm btn-outline-primary'),
    col_class: col_class,
    csv: false,
    format: (format if block_given?),
    index: nil,
    label: false,
    name: :actions,
    partial: partial,
    partial_as: partial_as,
    actions_partial: (actions_partial || :dropleft),
    responsive: responsive,
    search: false,
    sort: false,
    sql_column: nil,
    th: nil,
    th_append: nil,
    visible: visible,

    # { approve: false }. These args are passed to effective_resources render_resource_actions
    actions: actions
  )
end

#aggregate(name, label: nil, &compute) ⇒ Object



127
128
129
130
131
132
133
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 127

def aggregate(name, label: nil, &compute)
  datatable._aggregates[name.to_sym] = {
    compute: (compute if block_given?),
    label: label || name.to_s.titleize,
    name: name.to_sym,
  }
end

#bulk_actions_col(col_class: nil, input_name: nil, partial: nil, partial_as: nil, responsive: 5000) ⇒ Object

Called automatically after bulk_actions do … end Call again if you want to change the position of the bulk_actions_col



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 137

def bulk_actions_col(col_class: nil, input_name: nil, partial: nil, partial_as: nil, responsive: 5000)
  datatable._columns.delete(:_bulk_actions) if datatable.columns[:_bulk_actions]

  datatable._columns[:_bulk_actions] = Effective::DatatableColumn.new(
    action: false,
    as: :bulk_actions,
    compute: nil,
    col_class: col_class,
    csv: false,
    format: nil,
    index: nil,
    input_name: (input_name || 'bulk_actions_resources'),
    label: false,
    name: :bulk_actions,
    partial: partial || '/effective/datatables/bulk_actions_column',
    partial_as: partial_as,
    responsive: responsive,
    search: { as: :bulk_actions },
    sort: false,
    sql_column: nil,
    th: nil,
    th_append: nil,
    visible: true,
  )
end

#col(name, action: nil, as: nil, col_class: nil, csv: true, label: nil, partial: nil, partial_as: nil, responsive: 10000, search: {}, sort: true, sql_column: nil, th: nil, th_append: nil, visible: true, &format) ⇒ Object

A col has its internal values sorted/searched before the block is run Anything done in the block, is purely a format on the after sorted/ordered value the original object == the computed value, which is yielded to the format block You can’t do compute with .col



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 41

def col(name, action: nil, as: nil, col_class: nil, csv: true, label: nil, partial: nil, partial_as: nil, responsive: 10000, search: {}, sort: true, sql_column: nil, th: nil, th_append: nil, visible: true, &format)
  raise 'You cannot use partial: ... with the block syntax' if partial && block_given?

  name = name.to_sym unless name.to_s.include?('.')

  datatable._columns[name] = Effective::DatatableColumn.new(
    action: action,
    as: as,
    compute: nil,
    col_class: col_class,
    csv: csv,
    format: (format if block_given?),
    index: nil,
    label: label,
    name: name,
    partial: partial,
    partial_as: partial_as,
    responsive: responsive,
    search: search,
    sort: sort,
    sql_column: sql_column,
    th: th,
    th_append: th_append,
    visible: visible,
  )
end

#download(bool) ⇒ Object



29
30
31
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 29

def download(bool)
  datatable.attributes[:downloadable] = bool
end

#length(length) ⇒ Object

Instance Methods inside the datatable do .. end block



8
9
10
11
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 8

def length(length)
  raise 'length must be 5, 10, 25, 50, 100, 250, 500, :all' unless [5, 10, 25, 50, 100, 250, 500, :all].include?(length)
  datatable.state[:length] ||= (length == :all ? 9999999 : length)
end

#order(name, dir = nil) ⇒ Object



13
14
15
16
17
18
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 13

def order(name, dir = nil)
  raise 'order direction must be :asc or :desc' unless [nil, :asc, :desc].include?(dir)

  datatable.state[:order_name] ||= name
  datatable.state[:order_dir] ||= dir
end

#reorder(name, dir = nil) ⇒ Object



20
21
22
23
24
25
26
27
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 20

def reorder(name, dir = nil)
  raise 'order direction must be :asc or :desc' unless [nil, :asc, :desc].include?(dir)

  datatable.state[:order_name] = :_reorder
  datatable.state[:order_dir] = dir

  reorder_col(name)
end

#reorder_col(name, col_class: nil, partial: nil, partial_as: nil, sql_column: nil, responsive: 5000) ⇒ Object

Called automatically after reorder Call again if you want to change the position of the reorder_col



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 165

def reorder_col(name, col_class: nil, partial: nil, partial_as: nil, sql_column: nil, responsive: 5000)
  datatable._columns.delete(:_reorder) if datatable.columns[:_reorder]

  datatable._columns[:_reorder] = Effective::DatatableColumn.new(
    action: false,
    as: :reorder,
    compute: nil,
    col_class: col_class,
    csv: false,
    format: nil,
    index: nil,
    label: false,
    name: :reorder,
    partial: partial || '/effective/datatables/reorder_column',
    partial_as: partial_as,
    reorder: name,
    responsive: responsive,
    search: false,
    sort: true,
    sql_column: (sql_column || name),
    th: nil,
    th_append: nil,
    visible: false
  )
end

#skip_save_state!Object



33
34
35
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 33

def skip_save_state!
  datatable.attributes[:skip_save_state] = true
end

#val(name, action: nil, as: nil, col_class: nil, csv: true, label: nil, partial: nil, partial_as: nil, responsive: 10000, search: {}, sort: true, sql_column: false, th: nil, th_append: nil, visible: true, &compute) ⇒ Object

A val is a computed value that is then sorted/searched after the block is run You can have another block by calling .format afterwards to work on the computed value itself



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/effective/effective_datatable/dsl/datatable.rb', line 70

def val(name, action: nil, as: nil, col_class: nil, csv: true, label: nil, partial: nil, partial_as: nil, responsive: 10000, search: {}, sort: true, sql_column: false, th: nil, th_append: nil, visible: true, &compute)
  raise 'You cannot use partial: ... with the block syntax' if partial && block_given?

  name = name.to_sym unless name.to_s.include?('.')

  datatable._columns[name] = Effective::DatatableColumn.new(
    action: action,
    as: as,
    compute: (compute if block_given?),
    col_class: col_class,
    csv: csv,
    format: nil,
    index: nil,
    label: label,
    name: name,
    partial: partial,
    partial_as: partial_as,
    responsive: responsive,
    search: search,
    sort: sort,
    sql_column: (block_given? ? false : sql_column),
    th: th,
    th_append: th_append,
    visible: visible,
  )
end