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