Skip to content

Commit

Permalink
Merge pull request hitobito#658 from hitobito/feature/647_async_subgr…
Browse files Browse the repository at this point in the history
…oups_export

Async Subgroups Export
  • Loading branch information
kronn authored and MartinGantenbein committed Mar 21, 2020
2 parents 2ddab6c + 37b0608 commit 4173f10
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
13 changes: 7 additions & 6 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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; end
Expand Down
26 changes: 26 additions & 0 deletions app/jobs/export/subgroups_export_job.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/locales/views.de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions spec/jobs/export/subgroups_export_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4173f10

Please sign in to comment.