Skip to content

Instantly share code, notes, and snippets.

@slopeofhope81
Forked from willsza/spreadsheet_job.rb
Created May 27, 2020 14:12
Show Gist options
  • Save slopeofhope81/8cc74e1a50acf8756352e0394defe9f1 to your computer and use it in GitHub Desktop.
Save slopeofhope81/8cc74e1a50acf8756352e0394defe9f1 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