1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/services/users_export_service.rb

72 lines
2.4 KiB
Ruby
Raw Normal View History

2019-06-13 16:53:36 +02:00
# frozen_string_literal: true
require 'abstract_controller'
require 'action_controller'
require 'action_view'
require 'active_record'
# require any helpers
require './app/helpers/application_helper'
2019-06-13 16:53:36 +02:00
# There routines will generate Excel files containing data dumped from database
class UsersExportService
# export subscriptions
def export_subscriptions(export)
@subscriptions = Subscription.all.includes(:plan, statistic_profile: [user: [:profile]])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
2019-06-13 16:53:36 +02:00
view_assigns = { subscriptions: @subscriptions }
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/users_subscriptions.xlsx.axlsx'
# write content to file
2019-06-13 16:53:36 +02:00
File.open(export.file, 'w+b') { |f| f.puts content }
end
# export reservations
def export_reservations(export)
@reservations = Reservation.all.includes(:slots, :reservable, statistic_profile: [user: [:profile]])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
2019-06-13 16:53:36 +02:00
view_assigns = { reservations: @reservations }
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/users_reservations.xlsx.axlsx'
# write content to file
2019-06-13 16:53:36 +02:00
File.open(export.file, 'w+b') { |f| f.puts content }
end
# export members
def export_members(export)
@members = User.members
.includes(:group, :tags, :projects, :profile,
invoicing_profile: [:invoices, :address, organization: [:address]],
statistic_profile: [:trainings, subscriptions: [:plan]])
ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns
2019-06-13 16:53:36 +02:00
view_assigns = { members: @members }
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/users_members.xlsx.axlsx'
# write content to file
2019-06-13 16:53:36 +02:00
File.open(export.file, 'w+b') { |f| f.puts content }
end
2019-06-13 16:53:36 +02:00
end