From 63e244625059f831c55bd37d1b89f780043f2959 Mon Sep 17 00:00:00 2001 From: Janiss Binder Date: Wed, 10 Oct 2018 12:01:26 +0200 Subject: [PATCH] Async Subgroups Export --- app/controllers/groups_controller.rb | 13 ++++--- app/jobs/export/subgroups_export_job.rb | 26 +++++++++++++ config/locales/views.de.yml | 1 + spec/controllers/groups_controller_spec.rb | 13 +++---- spec/jobs/export/subgroups_export_job_spec.rb | 37 +++++++++++++++++++ 5 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 app/jobs/export/subgroups_export_job.rb create mode 100644 spec/jobs/export/subgroups_export_job_spec.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb index aa4a2d633e..f7c9d9cd34 100644 --- a/app/controllers/groups_controller.rb +++ b/app/controllers/groups_controller.rb @@ -7,6 +7,8 @@ class GroupsController < CrudController + include Concerns::AsyncDownload + # Respective group attrs are added in corresponding instance method. self.permitted_attrs = Contactable::ACCESSIBLE_ATTRS.dup @@ -52,12 +54,11 @@ def reactivate end def export_subgroups - list = entry.self_and_descendants. - without_deleted. - order(:lft). - includes(:contact) - csv = Export::Tabular::Groups::List.csv(list) - send_data csv, type: :csv + with_async_download_cookie(:csv, :subgroups_export) do |filename| + Export::SubgroupsExportJob.new(current_person.id, entry, filename: filename).enqueue! + end + + redirect_to entry end def person_notes diff --git a/app/jobs/export/subgroups_export_job.rb b/app/jobs/export/subgroups_export_job.rb new file mode 100644 index 0000000000..36d7b535dd --- /dev/null +++ b/app/jobs/export/subgroups_export_job.rb @@ -0,0 +1,26 @@ +# encoding: utf-8 + +# Copyright (c) 2018, Schweizer Blasmusikverband. This file is part of +# hitobito and licensed under the Affero General Public License version 3 +# or later. See the COPYING file at the top-level directory or at +# https://github.com/hitobito/hitobito. + +class Export::SubgroupsExportJob < Export::ExportBaseJob + + self.parameters = PARAMETERS + [:group] + + def initialize(user_id, group, options) + super(:csv, user_id, options) + @exporter = Export::Tabular::Groups::List + @group = group + end + + private + + def entries + @group.self_and_descendants + .without_deleted + .order(:lft) + .includes(:contact) + end +end diff --git a/config/locales/views.de.yml b/config/locales/views.de.yml index ec2a71f4a1..7c46b3b405 100755 --- a/config/locales/views.de.yml +++ b/config/locales/views.de.yml @@ -560,6 +560,7 @@ de: subgroups: 'Untergruppen' tabs: deleted: 'Gelöscht' + export_enqueued: 'Export wird im Hintergrund gestartet und nach Fertigstellung heruntergeladen.' group/person_add_requests: deactivated: Manuelle Freigabe deaktiviert diff --git a/spec/controllers/groups_controller_spec.rb b/spec/controllers/groups_controller_spec.rb index b9a804f04c..56ed09398d 100644 --- a/spec/controllers/groups_controller_spec.rb +++ b/spec/controllers/groups_controller_spec.rb @@ -143,14 +143,11 @@ let(:group) { groups(:top_layer) } it 'creates csv' do - get :export_subgroups, id: group.id - - expect(@response.content_type).to eq('text/csv') - lines = @response.body.split("\n") - expect(lines.size).to eq(10) - expect(lines[0]).to match(/^Id;Elterngruppe;Name;.*/) - expect(lines[1]).to match(/^#{group.id};;Top;.*/) - expect(lines[2]).to match(/^#{groups(:bottom_layer_one).id};#{group.id};Bottom One;.*/) + expect do + get :export_subgroups, id: group.id + expect(flash[:notice]) + .to match(/Export wird im Hintergrund gestartet und nach Fertigstellung heruntergeladen./) + end.to change(Delayed::Job, :count).by(1) end end diff --git a/spec/jobs/export/subgroups_export_job_spec.rb b/spec/jobs/export/subgroups_export_job_spec.rb new file mode 100644 index 0000000000..796d0e6584 --- /dev/null +++ b/spec/jobs/export/subgroups_export_job_spec.rb @@ -0,0 +1,37 @@ +# encoding: utf-8 + +# Copyright (c) 2018, Schweizer Blasmusikverband. This file is part of +# hitobito and licensed under the Affero General Public License version 3 +# or later. See the COPYING file at the top-level directory or at +# https://github.com/hitobito/hitobito. + +require 'spec_helper' + +describe Export::SubgroupsExportJob do + + subject { Export::SubgroupsExportJob.new(user.id, group, filename: 'subgroups_export') } + + let(:user) { people(:top_leader) } + let(:group) { groups(:top_layer) } + let(:year) { 2012 } + let(:filepath) { AsyncDownloadFile::DIRECTORY.join('subgroups_export') } + + before do + SeedFu.quiet = true + SeedFu.seed [Rails.root.join('db', 'seeds')] + end + + context 'creates a CSV-Export' do + + it 'and saves it' do + subject.perform + + lines = File.readlines("#{filepath}.csv") + expect(lines.size).to eq(10) + expect(lines[0]).to match(/^Id;Elterngruppe;Name;.*/) + expect(lines[1]).to match(/^#{group.id};;Top;.*/) + expect(lines[2]).to match(/^#{groups(:bottom_layer_one).id};#{group.id};Bottom One;.*/) + end + end + +end