Class: ActiveAdmin::Exporter::ExporterMailer

Inherits:
ActionMailer::Base
  • Object
show all
Defined in:
lib/active_admin/exporter/exporter_mailer.rb

Constant Summary collapse

DEFAULT_TEMPLATE_PATH =
'/admin_mailer'.freeze
DEFAULT_TEMPLATE_NAME =
'message_export'.freeze

Instance Method Summary collapse

Instance Method Details

#csv_export(recipient_email, class_name, params_json) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'lib/active_admin/exporter/exporter_mailer.rb', line 9

def csv_export(recipient_email, class_name, params_json)
  params     = JSON.parse(params_json)
  controller = class_name.constantize.new
  controller.send('params=', params.merge(email: recipient_email))

  config       = controller.send(:active_admin_config)
  csv_filename = controller.send(:csv_filename)
  entity_name  = controller.send(:resource_collection_name)
  zip_filename = csv_filename.sub('.csv', '.zip')

  def controller.find_collection(options = {})
    options[:only] = %i[
      sorting
      filtering
      scoping
      includes
      pagination
      collection_decorator
    ]

    collection = scoped_collection
    collection_applies(options).each do |applyer|
      collection = send("apply_#{applyer}", collection)
    end
    collection
  end

  zip = Zip::OutputStream.write_buffer do |zio|
    zio.put_next_entry(csv_filename)
    config.csv_builder.build(controller, zio)
  end

  attachments[zip_filename] = zip.string
  subject                   = I18n.t(
    'message.active_admin_export_subject',
    entity_name: entity_name.to_s.humanize,
    default:     zip_filename
  )

  mail(
    layout: 'mailer',
    to: recipient_email,
    subject: subject,
    from:          ActiveAdmin::Exporter.from_email_address,
    template_path: DEFAULT_TEMPLATE_PATH,
    template_name: DEFAULT_TEMPLATE_NAME
  )
end