Module: DcPollResultControl

Defined in:
app/controls/dc_poll_result_control.rb

Overview

DRG Controls for DcPage model.

Instance Method Summary collapse

Instance Method Details

#data_exportObject

Export data to file



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controls/dc_poll_result_control.rb', line 51

def data_export
  c, keys = '', []
  data_get.each do |doc|
    data = YAML.load(doc.data)
    # header and ensure fields are always in same order
    if c.blank?
      keys = data.map(&:first)
      c << I18n.t('helpers.label.dc_poll_result.created_at') + ";"
      c << keys.join(";") + "\n"
    end

    c << doc.created_at.strftime(I18n.t('time.formats.default')) + ";"
    keys.each do |k|
      c << if data[k].class == String
             %("#{data[k].gsub(/\"/, "'").gsub(';', ',')}";)
           else
             %("#{data[k]}";)
           end
    end
    c << "\r\n"
  end
  File.write(Rails.root.join('public', 'export.csv'), c)
  render json: { 'window_' => 'export.csv' }
end

#filter_setObject

Set filter action called from form.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controls/dc_poll_result_control.rb', line 32

def filter_set
  record = params[:record]
  filter = "DcPollResult.where(dc_poll_id: '#{record[:dc_poll_id]}')"
  filter << ".and(:created_at.gte => '#{Time.parse(record[:start_date]).beginning_of_day}')" if record[:start_date].present?
  filter << ".and(:created_at.lte => '#{Time.parse(record[:end_date]).end_of_day}')" if record[:end_date].present?

  session['dc_poll_result'][:filter] = {'field'     => I18n.t('drgcms.filter_off'),
                                        'operation' => 'eval',
                                        'value'     => filter,
                                        'input'     => '',
                                        'table'     => 'dc_poll_result' }.to_yaml
  session['dc_poll_result'][:page] = 1 # must also be set

  render json: { url: '/cmsedit?t=dc_poll_result'}
end