Last active
April 14, 2022 16:25
-
-
Save willsza/b6b0912d9be397e4334a3b3e02db871b to your computer and use it in GitHub Desktop.
Job Rails to generate excel reports with Axlsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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