Commit 050721ca authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻 Committed by Kaspar
Browse files

implement xlsx export with axlsx_rails gem

parent 8996fd4e
......@@ -8,6 +8,8 @@ end
gem 'rails', '~> 5.1'
gem 'autoprefixer-rails'
gem 'axlsx', '2.1.0.pre'
gem 'axlsx_rails'
gem 'bootstrap-sass'
gem 'cocoon'
gem 'coffee-rails'
......@@ -30,6 +32,7 @@ gem 'puma'
gem 'pundit'
gem 'rails-i18n'
gem 'ransack', github: 'activerecord-hackery/ransack'
gem 'rubyzip', '~> 1.1.0'
gem 'sass-rails'
gem 'scss_lint', require: false
gem 'simple_form'
......
......@@ -58,6 +58,13 @@ GEM
autoprefixer-rails (7.1.2.4)
execjs
awesome_print (1.8.0)
axlsx (2.1.0.pre)
htmlentities (~> 4.3.1)
nokogiri (>= 1.4.1)
rubyzip (~> 1.1.7)
axlsx_rails (0.5.1)
actionpack (>= 3.1)
axlsx (>= 2.0.1)
babel-source (5.8.35)
babel-transpiler (0.7.0)
babel-source (>= 4.0, < 6)
......@@ -156,6 +163,7 @@ GEM
globalid (0.4.0)
activesupport (>= 4.2.0)
highline (1.7.8)
htmlentities (4.3.4)
i18n (0.8.6)
i18n_data (0.8.0)
i18n_rails_helpers (2.0.1)
......@@ -295,7 +303,7 @@ GEM
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.8.1)
ruby_dep (1.5.0)
rubyzip (1.2.1)
rubyzip (1.1.7)
sass (3.4.25)
sass-rails (5.0.6)
railties (>= 4.0.0, < 6)
......@@ -381,6 +389,8 @@ PLATFORMS
DEPENDENCIES
autoprefixer-rails
awesome_print
axlsx (= 2.1.0.pre)
axlsx_rails
better_errors
binding_of_callers
bootstrap-sass
......@@ -418,6 +428,7 @@ DEPENDENCIES
rails-i18n
ransack!
rubocop
rubyzip (~> 1.1.0)
sass-rails
scss_lint
selenium-webdriver
......
......@@ -10,7 +10,15 @@ class VolunteersController < ApplicationController
def index
authorize Volunteer
@q = Volunteer.ransack(params[:q])
@volunteers = @q.result.paginate(page: params[:page])
respond_to do |format|
if params[:format] == 'xlsx'
@volunteers = @q.result
format.xlsx
else
@volunteers = @q.result.paginate(page: params[:page])
format.html
end
end
end
def show; end
......
......@@ -6,14 +6,14 @@
table.table.table-striped
thead
tr
th= sort_link @q, :contact_title
th= sort_link @q, :salutation
- if action_name == 'index' || action_name == 'seeking_clients'
th= sort_link @q, :contact_last_name, t_attr(:full_name, Volunteer)
th= sort_link @q, :contact_city
th= sort_link @q, :contact_postal_code
th= sort_link @q, :contact_primary_email
th= sort_link @q, :birth_year, t_attr(:birth_year, Volunteer)
th= sort_link @q, :language_skills_language, t_attr(:first_language, Volunteer)
th= t_attr(:first_language, Volunteer)
th= t_attr(:languages, Volunteer)
th= sort_link @q, :education
th= sort_link @q, :profession
......
......@@ -9,6 +9,7 @@
.row
- if policy(Volunteer).destroy?
.col-xs-12= button_link t('seeking_clients'), seeking_clients_volunteers_url
li= button_link t('export_xlsx'), "#{volunteers_path}.xlsx?#{{q: request.GET[:q]}.to_query}", 'warning ', dimension: 'sm'
= render 'columns', volunteers: @volunteers
......
wb = xlsx_package.workbook
wb.add_worksheet(name: t('volunteers')) do |sheet|
sheet.add_row [
'id',
t_attr(:salutation),
t_attr(:title, Contact),
t_attr(:last_name, Contact),
t_attr(:first_name, Contact),
t_attr(:street, Contact),
t_attr(:extended, Contact),
t_attr(:postal_code, Contact),
t_attr(:city, Contact),
t_attr(:primary_phone, Contact),
t_attr(:secondary_phone, Contact),
t_attr(:primary_email, Contact),
t_attr(:birth_year),
t_attr(:nationality),
t_attr(:profession),
t_attr(:state),
t_attr(:created_at),
t_attr(:updated_at),
t_attr(:assignment_count),
t_attr(:german_course)
]
@volunteers.each do |volunteer|
contact = volunteer.contact
sheet.add_row [
volunteer.id,
t("salutation.#{volunteer.salutation}"),
contact.last_name,
contact.first_name,
contact.street,
contact.extended,
contact.title,
contact.postal_code,
contact.city,
contact.primary_phone,
contact.secondary_phone,
contact.primary_email,
volunteer.birth_year&.year,
nationality_name(volunteer.nationality),
volunteer.profession,
t("state.#{volunteer.state}"),
volunteer.created_at,
volunteer.updated_at,
volunteer.assignments.count,
t(volunteer.german_course)
]
end
sheet.add_row []
sheet.add_row ['Amount', @volunteers.count]
end
......@@ -407,6 +407,7 @@ de:
not_saved:
one: 'Konnte %{resource} nicht speichern: ein Fehler.'
other: 'Konnte %{resource} nicht speichern: %{count} Fehler.'
export_xlsx: Liste als XLSX exportieren
find_client: Klient/in finden
find_volunteer: Freiwillige finden
forgot_password: Passwort vergessen?
......
......@@ -270,6 +270,7 @@ en:
not_saved:
one: '1 error prohibited this %{resource} from being saved:'
other: '%{count} errors prohibited this %{resource} from being saved:'
export_xlsx: Export list as XLSX
find_client: Find client
find_volunteer: Find volunteer
forgot_password: Forgot your password?
......
require 'test_helper'
class VolunteersXlsxExportTest < ActionDispatch::IntegrationTest
def setup
@superadmin = create :user, role: 'superadmin'
10.times { create :volunteer }
end
test 'xlsx file is downloadable' do
login_as @superadmin
get volunteers_url + '?format=xlsx'
assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
response.content_type
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment