From d72e39f7169464d73bf9c1f9f2586817f327e3db Mon Sep 17 00:00:00 2001 From: Sylvain Date: Tue, 12 Jul 2016 12:48:26 +0200 Subject: [PATCH] migrate export members to axlsx --- app/controllers/api/members_controller.rb | 8 ++--- app/views/api/members/export_members.xls.erb | 30 ---------------- .../api/members/export_members.xlsx.axlsx | 35 +++++++++++++++++++ config/locales/en.yml | 1 + config/locales/fr.yml | 1 + 5 files changed, 40 insertions(+), 35 deletions(-) delete mode 100644 app/views/api/members/export_members.xls.erb create mode 100644 app/views/api/members/export_members.xlsx.axlsx diff --git a/app/controllers/api/members_controller.rb b/app/controllers/api/members_controller.rb index b234b0d5c..d0c41acf5 100644 --- a/app/controllers/api/members_controller.rb +++ b/app/controllers/api/members_controller.rb @@ -110,11 +110,9 @@ class API::MembersController < API::ApiController def export_members authorize :export - @datas = User.with_role(:member).includes(:group, :subscriptions, :profile) - respond_to do |format| - format.html - format.xls - end + @members = User.with_role(:member).includes(:group, :subscriptions, :profile) + + render xlsx: 'export_members.xlsx', filename: "export_members.xlsx" end def merge diff --git a/app/views/api/members/export_members.xls.erb b/app/views/api/members/export_members.xls.erb deleted file mode 100644 index 3f1735f9f..000000000 --- a/app/views/api/members/export_members.xls.erb +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - - - - - - - <% @datas.each do |data| %> - - - - - - - - - - - - - - <% end %> -
<%=t('export_members.id')%><%=t('export_members.surname')%><%=t('export_members.first_name')%><%=t('export_members.email')%><%=t('export_members.gender')%><%=t('export_members.age')%><%=t('export_members.phone')%><%=t('export_members.group')%><%=t('export_members.subscription')%><%=t('export_members.subscription_end_date')%><%=t('export_members.validated_trainings')%>
<%= data.id %><%= data.profile.last_name %><%= data.profile.first_name %><%= data.email %><%= data.profile.gender ? t('export_members.man') : t('export_members.woman') %><%= data.profile.age %><%= data.profile.phone %><%= data.group.name %><%= (data.subscription and data.subscription.expired_at > Time.now) ? data.subscription.plan.name : t('export_members.without_subscriptions') %><%= (data.subscription and data.subscription.expired_at > Time.now) ? data.subscription.expired_at : '' %><%= raw data.trainings.map(&:name).join(' ') %>
diff --git a/app/views/api/members/export_members.xlsx.axlsx b/app/views/api/members/export_members.xlsx.axlsx new file mode 100644 index 000000000..b69c46c5a --- /dev/null +++ b/app/views/api/members/export_members.xlsx.axlsx @@ -0,0 +1,35 @@ +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 + +wb.add_worksheet(name: t('export_members.members')) do |sheet| + + ## data table + # heading labels + columns = [t('export_members.id'), t('export_members.surname'), t('export_members.first_name'), t('export_members.email'), + t('export_members.gender'), t('export_members.age'), t('export_members.phone'), t('export_members.group'), + t('export_members.subscription'), t('export_members.subscription_end_date'), t('export_members.validated_trainings')] + 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.profile.gender ? t('export_members.man') : t('export_members.woman'), + member.profile.age, + member.profile.phone, + 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") + ] + styles = [nil, nil, nil, nil, nil, nil, nil, nil, nil, date, nil] + types = [:integer, :string, :string, :string, :string, :integer, :string, :string, :string, :date, :string] + + sheet.add_row data, :style => styles, :types => types + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 937b8e004..3f7fba470 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -113,6 +113,7 @@ en: export_members: # members list export to EXCEL format + members: "Members" id: "ID" surname: "Surname" first_name: "First name" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index ca377da60..2e31325c2 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -113,6 +113,7 @@ fr: export_members: # export de la liste des members au format EXCEL + members: "Membres" id: "ID" surname: "Nom" first_name: "Prénom"