From f59297195e2b74c8028c895269ed9681d3ad7c29 Mon Sep 17 00:00:00 2001 From: Katrin Marschner Date: Tue, 31 Dec 2019 11:44:31 +0100 Subject: [PATCH] Organizer can download CSV with applications --- app/assets/images/icon-download.svg | 12 +++++++++++ app/assets/stylesheets/icons.sass | 6 ++++++ app/controllers/events_controller.rb | 23 ++++++++++++++++---- app/helpers/admin_events_helper.rb | 2 +- app/views/users/events.html.erb | 9 +++++++- config/locales/en.yml | 1 + config/locales/es.yml | 1 + config/locales/fr.yml | 1 + test/controllers/events_controller_test.rb | 25 ++++++++++++++++++++++ test/integration/user_events_page_test.rb | 10 ++++++++- 10 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 app/assets/images/icon-download.svg diff --git a/app/assets/images/icon-download.svg b/app/assets/images/icon-download.svg new file mode 100644 index 00000000..f460f0b5 --- /dev/null +++ b/app/assets/images/icon-download.svg @@ -0,0 +1,12 @@ + + + + icon 92 inbox download + Created with Sketch. + + + + + + + diff --git a/app/assets/stylesheets/icons.sass b/app/assets/stylesheets/icons.sass index 1a83346e..71626fde 100644 --- a/app/assets/stylesheets/icons.sass +++ b/app/assets/stylesheets/icons.sass @@ -13,6 +13,12 @@ background-size: 19px background-position: 3px 4px +.icon-download + @extend %icon + background-image: image-url("icon-download.svg") + background-size: 28px + background-position: -2px -3px + .icon-delete @extend %icon background-image: image-url("icon-delete.svg") diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 55b7d54f..c92700cb 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -17,13 +17,28 @@ def index_past end def show - if @event.unapproved && @event.organizer_id != current_user.id - flash[:alert] = t('.not_allowed') - redirect_back(fallback_location: root_path) - elsif @event.deleted + if @event.deleted flash[:alert] = t('.event_deleted') redirect_back(fallback_location: root_path) end + + respond_to do |format| + format.html do + if @event.unapproved && @event.organizer_id != current_user.id + flash[:alert] = t('.not_allowed') + redirect_back(fallback_location: root_path) + end + end + + format.csv do + if @event.organizer_id != current_user.id + flash[:alert] = t('.not_allowed') + redirect_back(fallback_location: root_path) + else + send_data @event.to_csv, filename: "#{@event.name} #{DateTime.now.strftime("%F")}.csv" + end + end + end end def new diff --git a/app/helpers/admin_events_helper.rb b/app/helpers/admin_events_helper.rb index 1b1abfa5..82356017 100644 --- a/app/helpers/admin_events_helper.rb +++ b/app/helpers/admin_events_helper.rb @@ -1,5 +1,5 @@ module AdminEventsHelper def download_csv_link(event) - link_to t('.download_csv'), admin_event_path(id: event.id, format: :csv), class: "btn btn-save", title: t('.download_data') + link_to 'Download CSV', admin_event_path(id: event.id, format: :csv), class: "btn btn-save", title: 'Download Data' end end diff --git a/app/views/users/events.html.erb b/app/views/users/events.html.erb index aac41600..9287c9c8 100644 --- a/app/views/users/events.html.erb +++ b/app/views/users/events.html.erb @@ -1,6 +1,6 @@ <% provide :title, t('.title') %> -<%= render partial: "shared/breadcrumbs", locals:{ title: t('.title') } %> +<%= render partial: "shared/breadcrumbs", locals: { title: t('.title') } %>

<%= t(".title") %>

@@ -24,6 +24,13 @@ <% end %>

+

+ <% if event.application_process == "selection_by_organizer" %> + <%= link_to event_path(id: event.id, format: :csv), class: "icon tooltip-large", title: t('.download_data') do %> + + <% end %> + <% end %> +

<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 0ea86922..46870271 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -555,6 +555,7 @@ en: past: "Past" title: "Your events" unapproved: "Not approved so far" + download_data: "Download Applications" confirm_delete: are_you_sure: "Are you sure you want to delete your user account?" benefits_intro: "We would be sad to see you go and don't want you to miss out on the benefits of diff --git a/config/locales/es.yml b/config/locales/es.yml index 4c33e299..03cc0b66 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -530,6 +530,7 @@ es: past: "Pasado" title: "Tus eventos" unapproved: "Aún sin aprobar" + download_data: "Descarga solicitudes" confirm_delete: are_you_sure: "¿Estás segura/o de que quieres eliminar tu cuenta de usuario?" benefits_intro: "Nos entristecería que te marchases y no queremos que te pierdas los beneficios de un usuario registrado" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index d2889fda..bc5971f8 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -506,6 +506,7 @@ fr: past: "Passés" title: "Vos événements" unapproved: "Non approuvé(s) jusqu'à présent" + download_data: "Téléchargez candidatures" confirm_delete: are_you_sure: "Êtes-vous sûr·e de vouloir supprimer votre compte utilisateur ?" benefits_intro: "Nous serions tristes de vous voir partir et ne voulons pas que vous ne manquiez pas les avantages qu'une compte vous confère." diff --git a/test/controllers/events_controller_test.rb b/test/controllers/events_controller_test.rb index 8ba05e8f..7daed283 100644 --- a/test/controllers/events_controller_test.rb +++ b/test/controllers/events_controller_test.rb @@ -333,6 +333,31 @@ class EventsControllerTest < ActionController::TestCase assert_equal 'You are not allowed to access this event.', flash[:alert] assert_redirected_to request.env["HTTP_REFERER"] end + + describe 'download event details as CSV' do + it 'shows the event details page in csv format to organizer' do + user = make_user(admin: false) + event = make_event(organizer_id: user.id) + sign_in_as(user) + + get :show, params: { id: event.id }, format: :csv + + assert_equal request.format, :csv + end + + it 'does not show the event details page in csv format to other users' do + user = make_user(admin: false) + different_user = make_user(admin: false, email: "differnt_user@example.com") + event = make_event(organizer_id: user.id) + request.env["HTTP_REFERER"] = 'http://www.somewhere.net' + sign_in_as(different_user) + + get :show, params: { id: event.id }, format: :csv + + assert_equal 'You are not allowed to access this event.', flash[:alert] + assert_redirected_to request.env["HTTP_REFERER"] + end + end end describe '#preview' do diff --git a/test/integration/user_events_page_test.rb b/test/integration/user_events_page_test.rb index 5d8f1000..524d92c2 100644 --- a/test/integration/user_events_page_test.rb +++ b/test/integration/user_events_page_test.rb @@ -3,7 +3,12 @@ feature 'User Events Page' do def setup @user = make_user - @event = make_event(name: 'Applicants event', approved: true, organizer_id: @user.id) + @event = make_event( + name: 'Applicants event', + approved: true, + organizer_id: @user.id, + application_process: 'selection_by_organizer', + ) @application = make_application(@event, applicant_id: @user.id) @draft = make_draft(@event, applicant_id: @user.id) end @@ -17,6 +22,9 @@ def setup assert page.has_content?('Your events') assert_equal event_path(@event.id), page.find_link(@event.name)[:href] + assert_equal edit_event_path(@event.id), page.find_link('Edit')[:href] + assert_equal event_path(id: @event.id, format: :csv), + page.find_link('Download Applications')[:href] assert page.has_content?('1 application') end end