Class: Tramway::Export::ExportsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/tramway/export/exports_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/tramway/export/exports_controller.rb', line 6

def index
  scope = params[:scope].present? ? params[:scope] : :all
  model_class = model_class_name(params[:model])
  xls_decorator_class = xls_decorator_class_name(params[:model])
  records = model_class.order(id: :desc).send scope
  records = records.ransack(params[:filter]).result if params[:filter].present?
  records = records.send "#{current_admin.role}_scope", current_admin.id
  records = xls_decorator_class.decorate records

  columns = xls_decorator_class.columns + records.map(&:flexible_columns).flatten.uniq do |hash|
    hash&.keys&.first
  end

  book = ::XlsExporter.export do
    default_style horizontal_align: :center, vertical_align: :center, text_wrap: true

    add_sheet 'List'

    export_models records, *columns
  end
  stream = StringIO.new
  book.write stream
  send_data stream.string, content_type: 'application/xls', filename: xls_decorator_class.filename
end

#showObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/tramway/export/exports_controller.rb', line 31

def show
  head(:unprocessable_entity) && return unless available?

  model_class = model_class_name(params[:model])
  xls_collection_decorator_class = xls_collection_decorator_class_name(params[:model], params[:collection])
  records = model_class.find(params[:id]).send(params[:collection]).order(id: :desc)
  records = records.send "#{current_admin.role}_scope", current_admin.id
  records = xls_collection_decorator_class.decorate records

  columns = xls_collection_decorator_class.columns + records.map(&:flexible_columns).flatten.uniq do |hash|
    hash&.keys&.first
  end

  book = ::XlsExporter.export do
    default_style horizontal_align: :center, vertical_align: :center, text_wrap: true

    add_sheet xls_collection_decorator_class.sheet_name

    export_models records, *columns
  end
  stream = StringIO.new
  book.write stream
  send_data stream.string, content_type: 'application/xls', filename: xls_collection_decorator_class.filename
end