diff --git a/app/services/users_export_service.rb b/app/services/users_export_service.rb index 80b31d754..0fe2b7dea 100644 --- a/app/services/users_export_service.rb +++ b/app/services/users_export_service.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'abstract_controller' require 'action_controller' require 'action_view' @@ -6,15 +8,16 @@ require 'active_record' # require any helpers require './app/helpers/application_helper' +# There routines will generate Excel files containing data dumped from database class UsersExportService # export subscriptions 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/' # place data in view_assigns - view_assigns = {subscriptions: @subscriptions} + 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) @@ -23,16 +26,16 @@ class UsersExportService content = av.render template: 'exports/users_subscriptions.xlsx.axlsx' # 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 # export reservations 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/' # place data in view_assigns - view_assigns = {reservations: @reservations} + 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) @@ -41,16 +44,18 @@ class UsersExportService content = av.render template: 'exports/users_reservations.xlsx.axlsx' # 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 # export members 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/' # place data in view_assigns - view_assigns = {members: @members} + 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) @@ -59,7 +64,7 @@ class UsersExportService content = av.render template: 'exports/users_members.xlsx.axlsx' # 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 \ No newline at end of file +end diff --git a/app/views/exports/users_members.xlsx.axlsx b/app/views/exports/users_members.xlsx.axlsx index d07315c7d..256bdefd0 100644 --- a/app/views/exports/users_members.xlsx.axlsx +++ b/app/views/exports/users_members.xlsx.axlsx @@ -1,7 +1,9 @@ +# frozen_string_literal: true + wb = xlsx_package.workbook -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 +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 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.organization'), t('export_members.organization_address')] - sheet.add_row columns, :style => header + sheet.add_row columns, style: header # data rows @members.each do |member| data = [ - member.id, - member.profile.last_name, - member.profile.first_name, - member.email, - member.is_allow_newsletter, - member.statistic_profile.gender ? t('export_members.man') : t('export_members.woman'), - member.profile.age, - member.invoicing_profile.address ? member.invoicing_profile.address.address : '', - member.profile.phone, - member.profile.website, - member.profile.job, - member.profile.interest, - member.profile.software_mastered, - member.group.name, - (member.subscription and 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.trainings.map(&:name).join("\n"), - member.tags.map(&:name).join("\n"), - member.invoices.size, - member.projects.map(&:name).join("\n"), - member.profile.facebook || '', - member.profile.twitter || '', - member.profile.echosciences || '', - member.invoicing_profile.organization ? member.invoicing_profile.organization.name : '', - member.invoicing_profile.organization ? member.invoicing_profile.organization.address.address : '' + member.id, + member.profile.last_name, + member.profile.first_name, + member.email, + member.is_allow_newsletter, + member.statistic_profile.gender ? t('export_members.man') : t('export_members.woman'), + member.profile.age, + member.invoicing_profile&.address&.address || '', + member.profile.phone, + member.profile.website, + member.profile.job, + member.profile.interest, + member.profile.software_mastered, + member.group&.name, + member.subscription && 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.expired_at.to_date : nil, + member.trainings.map(&:name).join("\n"), + member.tags.map(&:name).join("\n"), + member.invoices.size, + member.projects.map(&:name).join("\n"), + member.profile.facebook || '', + member.profile.twitter || '', + member.profile.echosciences || '', + member.invoicing_profile&.organization.name || '', + 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, - :string, :string, :string, :date, :string, :string, :integer, :boolean, :string, :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] - sheet.add_row data, :style => styles, :types => types + sheet.add_row data, style: styles, types: types end end