Source code
class Maintenance::ExportCustomFieldsTask < MaintenanceTasks::Task
include ArrayHelper
no_collection
attribute :emails, :string
attribute :brand_name, :string
attribute :mid_back_office_name, :string
attribute :mbo_country_code, :string
validates :emails, presence: true, fcm_email_format: true
def process
CsvGenerator.new.call do |file|
CustomFields::RawDataExtractor.new(
brand_name: brand_name,
mid_back_office_name: mid_back_office_name,
mbo_country_code: mbo_country_code
).call do |line|
file.puts(line)
end
file.rewind
blob = upload_to_storage(file)
send_email_with_url(blob)
end
end
private
def upload_to_storage(file)
ActiveStorage::Blob.create_and_upload!(
io: file,
filename: "custom_fields_export_#{Time.zone.now.strftime('%Y%m%d_%H%M%S')}.csv",
content_type: 'text/csv'
)
end
def send_email_with_url(blob)
file_url = Rails.application.routes.url_helpers.rails_blob_url(
blob,
host: application_host,
protocol: 'https'
)
ReportingFieldCsvMailer.send_url(
recipients: emails_array(emails),
file_url: file_url
).deliver_now
end
def application_host
ENV.fetch('VIRTUAL_HOST').split(',').first.strip
end
end