1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

ruby syntax + may fix #132?

This commit is contained in:
Sylvain 2019-06-13 16:53:36 +02:00
parent f3c433883d
commit 5cd977b4dd
2 changed files with 50 additions and 42 deletions

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
require 'abstract_controller' require 'abstract_controller'
require 'action_controller' require 'action_controller'
require 'action_view' require 'action_view'
@ -6,15 +8,16 @@ require 'active_record'
# require any helpers # require any helpers
require './app/helpers/application_helper' require './app/helpers/application_helper'
# There routines will generate Excel files containing data dumped from database
class UsersExportService class UsersExportService
# export subscriptions # export subscriptions
def export_subscriptions(export) def export_subscriptions(export)
@subscriptions = Subscription.all.includes(:plan, :user => [:profile]) @subscriptions = Subscription.all.includes(:plan, user: [:profile])
ActionController::Base.prepend_view_path './app/views/' ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns # place data in view_assigns
view_assigns = {subscriptions: @subscriptions} view_assigns = { subscriptions: @subscriptions }
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do av.class_eval do
# include any needed helpers (for the view) # include any needed helpers (for the view)
@ -23,16 +26,16 @@ class UsersExportService
content = av.render template: 'exports/users_subscriptions.xlsx.axlsx' content = av.render template: 'exports/users_subscriptions.xlsx.axlsx'
# write content to file # write content to file
File.open(export.file,"w+b") {|f| f.puts content } File.open(export.file, 'w+b') { |f| f.puts content }
end end
# export reservations # export reservations
def export_reservations(export) def export_reservations(export)
@reservations = Reservation.all.includes(:slots, :reservable, :user => [:profile]) @reservations = Reservation.all.includes(:slots, :reservable, user: [:profile])
ActionController::Base.prepend_view_path './app/views/' ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns # place data in view_assigns
view_assigns = {reservations: @reservations} view_assigns = { reservations: @reservations }
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do av.class_eval do
# include any needed helpers (for the view) # include any needed helpers (for the view)
@ -41,16 +44,18 @@ class UsersExportService
content = av.render template: 'exports/users_reservations.xlsx.axlsx' content = av.render template: 'exports/users_reservations.xlsx.axlsx'
# write content to file # write content to file
File.open(export.file,"w+b") {|f| f.puts content } File.open(export.file, 'w+b') { |f| f.puts content }
end end
# export members # export members
def export_members(export) def export_members(export)
@members = User.with_role(:member).includes(:group, :trainings, :tags, :invoices, :projects, :subscriptions => [:plan], :profile => [:address, :organization => [:address]]) @members = User.with_role(:member)
.includes(:group, :trainings, :tags, :invoices, :projects,
subscriptions: [:plan], profile: [:address, organization: [:address]])
ActionController::Base.prepend_view_path './app/views/' ActionController::Base.prepend_view_path './app/views/'
# place data in view_assigns # place data in view_assigns
view_assigns = {members: @members} view_assigns = { members: @members }
av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns) av = ActionView::Base.new(ActionController::Base.view_paths, view_assigns)
av.class_eval do av.class_eval do
# include any needed helpers (for the view) # include any needed helpers (for the view)
@ -59,7 +64,7 @@ class UsersExportService
content = av.render template: 'exports/users_members.xlsx.axlsx' content = av.render template: 'exports/users_members.xlsx.axlsx'
# write content to file # write content to file
File.open(export.file,"w+b") {|f| f.puts content } File.open(export.file, 'w+b') { |f| f.puts content }
end end
end end

View File

@ -1,7 +1,9 @@
# frozen_string_literal: true
wb = xlsx_package.workbook wb = xlsx_package.workbook
header = wb.styles.add_style :b => true, :bg_color => Stylesheet.primary.upcase.gsub('#', 'FF'), :fg_color => 'FFFFFFFF' header = wb.styles.add_style b: true, bg_color: Stylesheet.primary.upcase.gsub('#', 'FF'), fg_color: 'FFFFFFFF'
date = wb.styles.add_style :format_code => Rails.application.secrets.excel_date_format date = wb.styles.add_style format_code: Rails.application.secrets.excel_date_format
wb.add_worksheet(name: t('export_members.members')) do |sheet| wb.add_worksheet(name: t('export_members.members')) do |sheet|
@ -32,42 +34,43 @@ wb.add_worksheet(name: t('export_members.members')) do |sheet|
t('export_members.echo_sciences'), t('export_members.echo_sciences'),
t('export_members.organization'), t('export_members.organization'),
t('export_members.organization_address')] t('export_members.organization_address')]
sheet.add_row columns, :style => header sheet.add_row columns, style: header
# data rows # data rows
@members.each do |member| @members.each do |member|
data = [ data = [
member.id, member.id,
member.profile.last_name, member.profile.last_name,
member.profile.first_name, member.profile.first_name,
member.email, member.email,
member.is_allow_newsletter, member.is_allow_newsletter,
member.statistic_profile.gender ? t('export_members.man') : t('export_members.woman'), member.statistic_profile.gender ? t('export_members.man') : t('export_members.woman'),
member.profile.age, member.profile.age,
member.invoicing_profile.address ? member.invoicing_profile.address.address : '', member.invoicing_profile&.address&.address || '',
member.profile.phone, member.profile.phone,
member.profile.website, member.profile.website,
member.profile.job, member.profile.job,
member.profile.interest, member.profile.interest,
member.profile.software_mastered, member.profile.software_mastered,
member.group.name, member.group&.name,
(member.subscription and member.subscription.expired_at > Time.now) ? member.subscription.plan.name : t('export_members.without_subscriptions'), member.subscription && member.subscription.expired_at > Time.now ? member.subscription.plan.name : t('export_members.without_subscriptions'),
(member.subscription and member.subscription.expired_at > Time.now) ? member.subscription.expired_at.to_date : nil, member.subscription && member.subscription.expired_at > Time.now ? member.subscription.expired_at.to_date : nil,
member.trainings.map(&:name).join("\n"), member.trainings.map(&:name).join("\n"),
member.tags.map(&:name).join("\n"), member.tags.map(&:name).join("\n"),
member.invoices.size, member.invoices.size,
member.projects.map(&:name).join("\n"), member.projects.map(&:name).join("\n"),
member.profile.facebook || '', member.profile.facebook || '',
member.profile.twitter || '', member.profile.twitter || '',
member.profile.echosciences || '', member.profile.echosciences || '',
member.invoicing_profile.organization ? member.invoicing_profile.organization.name : '', member.invoicing_profile&.organization.name || '',
member.invoicing_profile.organization ? member.invoicing_profile.organization.address.address : '' member.invoicing_profile&.organization&.address&.address || ''
] ]
styles = [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, date, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] styles = [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil,
nil, nil, nil, date, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
types = [:integer, :string, :string, :string, :boolean, :string, :integer, :string, :string, :string, :string, :string, types = %i[integer string string string boolean string integer string string string string string
:string, :string, :string, :date, :string, :string, :integer, :boolean, :string, :string, :string, :string, :string, :string] string string string date string string integer boolean string string string string string string]
sheet.add_row data, :style => styles, :types => types sheet.add_row data, style: styles, types: types
end end
end end