1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-12 23:09:03 +01:00
fab-manager/app/services/availabilities_export_service.rb
2022-01-06 13:43:51 +01:00

33 lines
1008 B
Ruby

# frozen_string_literal: true
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
# Retrieve all availabilities and their related objects and write the result as a table in an excel file
class AvailabilitiesExportService
# export all availabilities
def export_index(export)
@availabilities = Availability.all.includes(:machines, :trainings, :spaces, :event, :slots)
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
view_assigns = { availabilities: @availabilities }
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do
# include any needed helpers (for the view)
include ApplicationHelper
end
content = av.render template: 'exports/availabilities_index.xlsx.axlsx'
# write content to file
File.open(export.file, 'w+b') { |f| f.write content }
end
end