Module: TrkDatatables::BaseHelpers

Included in:
Base
Defined in:
lib/trk_datatables/base_helpers.rb

Instance Method Summary collapse

Instance Method Details

#form_field_name(column_key) ⇒ Object

Get the form field name for column. This is class method so you do not need datatable instance. It returns something like ‘column[search]`. For global search you can use ’[search]

form_tag url: posts_path, method: :get do |f|

f.text_field PostsDatatable.form_field_name('users.email'), '[email protected]'
# it is the same as
f.text_field 'columns[3][search][value]', '[email protected]'


46
47
48
49
50
# File 'lib/trk_datatables/base_helpers.rb', line 46

def form_field_name(column_key)
  datatable = new OpenStruct.new(params: {})
  column_index = datatable.index_by_column_key column_key
  DtParams.form_field_name column_index
end

#order_set(column_key, direction = :asc, view = nil) ⇒ Object

Set sort for column. This is class method so you do not need datatable instance.

Examples:

link_to 'Sort by email',
posts_path(PostsDatatable.order_set('users.email', :desc)


30
31
32
33
34
# File 'lib/trk_datatables/base_helpers.rb', line 30

def order_set(column_key, direction = :asc, view = nil)
  datatable = new view || OpenStruct.new(params: {})
  column_index = datatable.index_by_column_key column_key
  DtParams.order_set column_index, direction
end

#param_set(column_key, value, view = nil) ⇒ Object

Set params for column search. This is class method so you do not need datatable instance.

You can always use your params for filtering outside of datatable and merge to params

Examples:

link_to 'Published posts for [email protected]',
posts_path(PostsDatatable.params('posts.status': :published,
'users.email: '[email protected]')
link_to 'Published posts for user1',
posts_path(PostsDatatable.param_set('posts.status', :published).merge(user_id: user1.id))


16
17
18
19
20
21
22
# File 'lib/trk_datatables/base_helpers.rb', line 16

def param_set(column_key, value, view = nil)
  datatable = new view || OpenStruct.new(params: {})
  value = value.join MULTIPLE_OPTION_SEPARATOR if value.is_a? Array
  value = [value.first, value.last].join BETWEEN_SEPARATOR if value.is_a? Range
  column_index = datatable.index_by_column_key column_key
  DtParams.param_set column_index, value
end

#range_string(range) ⇒ Object

For range you can this helper to insert BETWEEN_SEPARATOR

Raises:



53
54
55
56
57
58
59
# File 'lib/trk_datatables/base_helpers.rb', line 53

def range_string(range)
  raise Error, "#{range} is not a Range" unless range.is_a? Range

  from = range.min
  to = range.max
  "#{from} #{BETWEEN_SEPARATOR} #{to}"
end