Module: ActiveScaffold::Actions::Export

Defined in:
lib/active_scaffold/actions/export.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/active_scaffold/actions/export.rb', line 3

def self.included(base)
  base.before_action :export_authorized?, :only => [:export]
  base.before_action :show_export_authorized?, :only => [:show_export]
end

Instance Method Details

#exportObject

if invoked directly, will use default configuration



22
23
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
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/active_scaffold/actions/export.rb', line 22

def export
  export_config = active_scaffold_config.export
  if params[:export_columns].nil?
    export_columns = {}
    export_config.columns.each { |col| export_columns[col.to_sym] = 1 }
    options = {
      :export_columns => export_columns,
      :full_download => export_config.default_full_download.to_s,
      :delimiter => export_config.default_delimiter,
      :skip_header => export_config.default_skip_header.to_s
    }
    params.merge!(options)
  end

  set_includes_for_columns(:export)
  @export_config = export_config
  # Make sure active_scaffold's find_page is dealing with the same list of
  # columns. Prevents an invalid SQL query when exporting after filtering
  # with field_search against a relation column, and that relation column is
  # not included in the set of export columns.
  @list_columns = @export_columns

  # this is required if you want this to work with IE
  if request.env['HTTP_USER_AGENT'] =~ /msie/i
    response.headers['Pragma'] = "public"
    response.headers['Cache-Control'] = "no-cache, must-revalidate, post-check=0, pre-check=0"
    response.headers['Expires'] = "0"
  end
  response.headers['Content-Disposition'] = "attachment; filename=#{export_file_name}"

  unless defined? Mime::XLSX
    Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx
  end

  respond_to do |format|
    format.csv do
      response.headers['Content-type'] = 'text/csv'
      # start streaming output
      self.response_body = Enumerator.new do |y|
        find_items_for_export do |records|
          @records = records
          str = render_to_string :partial => 'export', :layout => false, :formats => [:csv]
          y << str
          params[:skip_header] = 'true' # skip header on the next run
        end
      end
    end
    format.xlsx do 
      response.headers['Content-type'] = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      p = Axlsx::Package.new
      header = p.workbook.styles.add_style sz: 11, b: true,:bg_color => "69B5EF", :fg_color => "FF", alignment: { horizontal: :center }
      p.workbook.add_worksheet(name: active_scaffold_config.label) do |sheet|
        sheet.add_row(@export_columns.collect { |column| view_context.format_export_column_header_name(column) }, style: header) unless params[:skip_header]
        find_items_for_export do |records|
          records.each do |record|
            sheet.add_row @export_columns.collect { |column| view_context.get_export_column_value(record, column, false) }
          end
        end
      end
      stream = p.to_stream # when adding rows to sheet, they won't pass to this stream if declared before. axlsx issue?
      self.response_body = Enumerator.new do |y|
        y << stream.read 
      end
    end

  end
end

#show_exportObject

display the customization form or skip directly to export



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_scaffold/actions/export.rb', line 9

def show_export
  @export_config = active_scaffold_config.export
  respond_to do |wants|
    wants.html do
      render(:partial => 'show_export', :layout => true)
    end
    wants.js do
      render(:partial => 'show_export', :layout => false)
    end
  end
end