Module: ActionTabler::DataTables::DataHandler

Included in:
ActionTabler::DataTables
Defined in:
lib/action_tabler/data_tables/data_handler.rb

Instance Method Summary collapse

Instance Method Details

#data_package(params) ⇒ Object

Render table data



6
7
8
9
10
11
12
13
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
# File 'lib/action_tabler/data_tables/data_handler.rb', line 6

def data_package(params)
  # Select the requested records
  @records = @model.where(exitisting_conditions(params)).
                    where(global_conditions(params)).
                    where(column_conditions(params)).
                    limit(params[:iDisplayLength]).
                    offset(params[:iDisplayStart]).
                    order(sort_conditions(params))
  # Count the total records available
  @display_count = @model.where(exitisting_conditions(params)).
                          where(global_conditions(params)).
                          where(column_conditions(params)).
                          count
  # Find out total records, including those not found by a search specification
  @total_count = @model.where(exitisting_conditions(params)).count

  @column_names = []

  # Build a hash with json safe column names
  @records = @records.inject([]) do |result, record|
    result << @columns.inject([])  do |this, column|
      this << eval("record.#{column[:name]}")
      @column_names << column[:name].to_s
      this
    end
    result
  end

  # Return the data package as a hash
  { :iTotalDisplayRecords => @display_count,
    :iTotalRecords => @total_count, 
    :sNames => @column_names, 
    :aaData => @records,
    :sEcho => params[:sEcho].to_i }
end