Module: DataGrid::Controller

Extended by:
ActiveSupport::Concern
Defined in:
lib/data_grid/controller.rb,
lib/data_grid/csv_exporter.rb,
lib/data_grid/cookies_state_saver.rb

Constant Summary collapse

STATE_KEYS =
[:per_page, :page, :sort, :sort_direction]

Instance Method Summary collapse

Instance Method Details

#export(data_grid, filename = DataGrid.export_filename) ⇒ Object

Do CSV export



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
67
68
69
70
71
72
73
74
75
76
# File 'lib/data_grid/csv_exporter.rb', line 24

def export(data_grid, filename = DataGrid.export_filename)
  require 'csv'
  
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
   headers['Pragma'] = 'public'
   headers["Content-type"] = "text/plain; charset=utf-8; header=present"
   headers['Cache-Control'] = 'no-cache, must-revalidate, post-check=0, pre-check=0'       
   headers['Content-Disposition'] = "attachment; filename=\"#{filename}\""
   headers['Expires'] = "0" 
  else
    headers["Content-Type"] ||= 'text/csv; charset=utf-8; header=present'   
    headers["Content-Disposition"] = "attachment; filename=\"#{filename}\""
  end  
  data_grid.view_context = CsvContent.new
  data_grid.prepare_data

  csv_string = CSV.generate(:col_sep => ';') do |csv|
    hide_column_indexes = []
    cols = []
    data_grid.columns.each_with_index do |col, i|
      if col.hide_in_export 
        hide_column_indexes << i
      else
        cols << col.title
      end
    end
    csv << cols
    data_grid.out_data.each_with_index do |row, row_index|
      data_row = []
      row.each_with_index do |row_col, i|
        next if (!data_grid.hidden_row.nil? and i == row.size-1)
        unless hide_column_indexes.include?(i)
          data_row << ((row_col.class == ActiveSupport::SafeBuffer) ? row_col.to_s.gsub(/<\/?[^>]*>/, "") : (row_col.class == String) ? row_col.gsub(/<\/?[^>]*>/, "") : row_col)
        end
      end
      unless data_grid.hidden_row.nil?
        data_row << data_grid.out_hidden_rows[row_index] 
      end
      csv << data_row
    end

    if data_grid.summary? 
      data_row = []
   data_grid.columns.each_with_index {|col, i| 
        data_row <<  data_grid.summaries[i].to_s unless hide_column_indexes.include?(i)
      } 
      csv << data_row
 end 

  end

  render :text => csv_string
end

#prepare_grid(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/data_grid/controller.rb', line 5

def prepare_grid(&block)
  # Build logic object
  data_grid = DataGrid::DataGridLogic.new(:params => params)
  block.call(data_grid)

  # Restore state
  if data_grid.state_saver and DataGrid.state_saver_method
    require "data_grid/#{DataGrid.state_saver_method}_state_saver"
    self.restore_state(data_grid)
  end

  # Get and save data from params
  data_grid.get_params_from_request
  self.save_state(data_grid) if data_grid.state_saver

  # Export on demand
  if data_grid.export_enabled and params["export_#{data_grid.name}"]
    require "data_grid/#{params["export_#{data_grid.name}"]}_exporter"
    self.export(data_grid, data_grid.export_filename)
  end
  
  data_grid
end

#restore_state(data_grid) ⇒ Object

Restore state from cookies



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/data_grid/cookies_state_saver.rb', line 26

def restore_state(data_grid)
  if cookies["data_grid_state_#{data_grid.name}"]
    state = Marshal.load(cookies["data_grid_state_#{data_grid.name}"])
    STATE_KEYS.each do |key|
      data_grid.send("#{key}=", state[key])
    end
  
    if state[:columns]
      state[:columns].each_pair do |k, v|
        data_grid.columns[k.to_i].filter_value = v if data_grid.columns[k.to_i]
      end
    end
  end
end

#save_state(data_grid) ⇒ Object

Save grid state in cookies



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/data_grid/cookies_state_saver.rb', line 9

def save_state(data_grid)
  cookies["data_grid_state_#{data_grid.name}"] ||= {}

  state = {}
  STATE_KEYS.each do |key|
    state[key] = data_grid.send(key)
  end
  
  data_grid.columns.each_with_index do |col, col_index|
    state[:columns] ||= {}
    state[:columns][col_index] = col.filter_value
  end
  
  cookies["data_grid_state_#{data_grid.name}"] = Marshal.dump(state)
end