Class: Carnival::GenericDatatable

Inherits:
Object
  • Object
show all
Defined in:
app/datatable/carnival/generic_datatable.rb

Constant Summary collapse

RENDER_TABLE =
0
RENDER_CSV =
1
RENDER_PDF =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, model, controller, presenter, items = nil) ⇒ GenericDatatable

Returns a new instance of GenericDatatable.



13
14
15
16
17
18
19
20
# File 'app/datatable/carnival/generic_datatable.rb', line 13

def initialize(view, model, controller, presenter, items=nil)
  @view = view
  @model = model
  @controller = controller
  @presenter = presenter
  @items = items
  @filters = []
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



6
7
8
# File 'app/datatable/carnival/generic_datatable.rb', line 6

def model
  @model
end

#presenterObject

Returns the value of attribute presenter.



6
7
8
# File 'app/datatable/carnival/generic_datatable.rb', line 6

def presenter
  @presenter
end

Instance Method Details

#as_csv(options = {}) ⇒ Object



22
23
24
# File 'app/datatable/carnival/generic_datatable.rb', line 22

def as_csv(options = {})
  data_to_csv(data(RENDER_CSV))
end

#as_json(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/datatable/carnival/generic_datatable.rb', line 26

def as_json(options = {})
  if(params[:scope].present? && params[:scope] != "all")
    @model = @model.send(params[:scope])
  end

  date_filter_field = @presenter.date_filter_field
  if(date_filter_field.present? && params[:from].present? && params[:from] != "" && params[:to].present? && params[:to] != "")
    @model = @model.where("#{@presenter.table_name}.#{date_filter_field.name} between ? and ?", "#{params[:from]} 00:00:00", "#{params[:to]} 23:59:59")
  end

  count = @model.count

  return_data = {}
  return_data[:data] = data
  {
    sEcho: params[:sEcho].to_i,
    iTotalRecords: count,
    iTotalDisplayRecords: records.total_entries,
    aaData: return_data[:data]
  }
end

#as_pdf(options = {}) ⇒ Object



48
49
50
# File 'app/datatable/carnival/generic_datatable.rb', line 48

def as_pdf(options = {})
  fetch_records
end

#filtersObject



52
53
54
# File 'app/datatable/carnival/generic_datatable.rb', line 52

def filters
  @filters
end