Skip to content

Instantly share code, notes, and snippets.

@willsza
Last active April 14, 2022 16:25
Show Gist options
  • Save willsza/b6b0912d9be397e4334a3b3e02db871b to your computer and use it in GitHub Desktop.
Save willsza/b6b0912d9be397e4334a3b3e02db871b to your computer and use it in GitHub Desktop.
Job Rails to generate excel reports with Axlsx
require 'axlsx'
class SpreadsheetJob < ApplicationJob
queue_as :reports
def perform(*args)
@timestamp = args[0]['timestamp']
create_report
create_spreadsheet
end
private
def create_report
Report.create(timestamp: @timestamp)
end
def create_spreadsheet
path = Rails.root.join('public', 'reports')
view_assigns = { jobs: Job.all }
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
include ApplicationHelper
end
content = av.render template: 'admin/jobs/index.xlsx.axlsx'
File.open(File.join(path, "#{@timestamp}_report_job.xlsx"), "w+b") { |f| f.write content }
update_report
end
def update_report
report = Report.find_by(timestamp: @timestamp)
report.update(status: 'ready') if report
logger.fatal 'Report was update!'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment