From cb15a2e25b3db45815f23589797812884785b35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 15 Feb 2018 16:23:23 +0100 Subject: [PATCH 1/7] Added EmailsDownload controller and routes Removed original method to return emails file from Newsletters controller and NewsletterZip class, included `rubyzip` gem that's no longer necessary. --- Gemfile | 1 - .../admin/emails_download_controller.rb | 18 +++++++++++++ .../admin/newsletters_controller.rb | 6 ----- config/routes/admin.rb | 4 +++ lib/newsletter_zip.rb | 25 ------------------- 5 files changed, 22 insertions(+), 32 deletions(-) create mode 100644 app/controllers/admin/emails_download_controller.rb delete mode 100644 lib/newsletter_zip.rb diff --git a/Gemfile b/Gemfile index 55d9a5284..5a9141ae9 100644 --- a/Gemfile +++ b/Gemfile @@ -41,7 +41,6 @@ gem 'redcarpet', '~> 3.4.0' gem 'responders', '~> 2.4.0' gem 'rinku', '~> 2.0.2', require: 'rails_rinku' gem 'rollbar', '~> 2.15.5' -gem 'rubyzip', '~> 1.2.0' gem 'sass-rails', '~> 5.0', '>= 5.0.4' gem 'savon', '~> 2.11.1' gem 'sitemap_generator', '~> 6.0.1' diff --git a/app/controllers/admin/emails_download_controller.rb b/app/controllers/admin/emails_download_controller.rb new file mode 100644 index 000000000..326c32ff1 --- /dev/null +++ b/app/controllers/admin/emails_download_controller.rb @@ -0,0 +1,18 @@ +class Admin::EmailsDownloadController < Admin::BaseController + def index + end + + def generate_csv + users_segment = params[:users_segment] + filename = t("admin.segment_recipient.#{users_segment}") + + csv_file = users_segment_emails_csv(users_segment) + send_data csv_file, filename: "#{filename}.csv" + end + + private + + def users_segment_emails_csv(users_segment) + UserSegments.send(users_segment).pluck(:email).to_csv + end +end diff --git a/app/controllers/admin/newsletters_controller.rb b/app/controllers/admin/newsletters_controller.rb index dc06a5488..4dbf963f7 100644 --- a/app/controllers/admin/newsletters_controller.rb +++ b/app/controllers/admin/newsletters_controller.rb @@ -44,12 +44,6 @@ class Admin::NewslettersController < Admin::BaseController redirect_to admin_newsletters_path, notice: t("admin.newsletters.delete_success") end - def users - zip = NewsletterZip.new('emails') - zip.create - send_file(File.join(zip.path), type: 'application/zip') - end - def deliver @newsletter = Newsletter.find(params[:id]) Mailer.newsletter(@newsletter).deliver_later diff --git a/config/routes/admin.rb b/config/routes/admin.rb index fb3c2a4de..384fb685c 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -149,6 +149,10 @@ namespace :admin do get :users, on: :collection end + resources :emails_download, only: :index do + get :generate_csv, on: :collection + end + resource :stats, only: :show do get :proposal_notifications, on: :collection get :direct_messages, on: :collection diff --git a/lib/newsletter_zip.rb b/lib/newsletter_zip.rb deleted file mode 100644 index dead7cf82..000000000 --- a/lib/newsletter_zip.rb +++ /dev/null @@ -1,25 +0,0 @@ -require 'zip' -class NewsletterZip - attr_accessor :filename - - def initialize(filename) - @filename = filename - end - - def emails - User.newsletter.pluck(:email).join("\n") - end - - def path - Rails.root + "/tmp/#{filename}.zip" - end - - def create - Zip::File.open(path, Zip::File::CREATE) do |zipfile| - zipfile.get_output_stream("#{filename}.txt") do |file| - file.write emails - end - end - end - -end \ No newline at end of file From 6c36e17ce2edd50ab44edc0adcbc7b40483f658d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 15 Feb 2018 16:23:51 +0100 Subject: [PATCH 2/7] Added `SEGMENTS` variable to UserSegments class --- lib/user_segments.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/user_segments.rb b/lib/user_segments.rb index a52ec1b03..02fbe9b5f 100644 --- a/lib/user_segments.rb +++ b/lib/user_segments.rb @@ -1,4 +1,11 @@ class UserSegments + SEGMENTS = %w(all_users + proposal_authors + investment_authors + feasible_and_undecided_investment_authors + selected_investment_authors + winner_investment_authors) + def self.all_users User.newsletter.active end From 1c458d4be3a9aa156dcb77f7fc2b441b0b2cf757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 15 Feb 2018 16:24:20 +0100 Subject: [PATCH 3/7] Added emails download views --- .../prevent_double_submission.js.coffee | 2 +- app/views/admin/_menu.html.erb | 5 ++++- .../admin/emails_download/index.html.erb | 20 +++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/emails_download/index.html.erb diff --git a/app/assets/javascripts/prevent_double_submission.js.coffee b/app/assets/javascripts/prevent_double_submission.js.coffee index 56a099730..743119c22 100644 --- a/app/assets/javascripts/prevent_double_submission.js.coffee +++ b/app/assets/javascripts/prevent_double_submission.js.coffee @@ -22,7 +22,7 @@ App.PreventDoubleSubmission = initialize: -> $('form').on('submit', (event) -> - unless event.target.id == "new_officing_voter" + unless event.target.id == "new_officing_voter" || "admin_download_emails" buttons = $(this).find(':button, :submit') App.PreventDoubleSubmission.disable_buttons(buttons) ).on('ajax:success', (event) -> diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 896574e31..c8dcfbbe2 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -203,10 +203,13 @@ <%= t("admin.menu.emails") %> -
    > +
      >
    • > <%= link_to t("admin.menu.newsletters"), admin_newsletters_path %>
    • +
    • > + <%= link_to t("admin.menu.emails_download"), admin_emails_download_index_path %> +
diff --git a/app/views/admin/emails_download/index.html.erb b/app/views/admin/emails_download/index.html.erb new file mode 100644 index 000000000..20bdb1775 --- /dev/null +++ b/app/views/admin/emails_download/index.html.erb @@ -0,0 +1,20 @@ +

<%= t("admin.emails_download.index.title") %>

+ +
+ <%= form_tag generate_csv_admin_emails_download_index_path, + method: :get, + id: "admin_download_emails" do %> + + +

+ <%= t('admin.emails_download.index.download_segment_help_text') %> +

+ + <%= select_tag :users_segment, options_for_select(UserSegments::SEGMENTS + .collect { |s| [t("admin.segment_recipient.#{s}"), s] }) %> + +
+ <%= submit_tag t('admin.emails_download.index.download_emails_button'), class: "button" %> +
+ <% end %> +
From c78ae7e346b01b0f427ec38e2be716b2c9625aa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 15 Feb 2018 16:24:33 +0100 Subject: [PATCH 4/7] Added emails download translations --- config/locales/en/admin.yml | 7 +++++++ config/locales/es/admin.yml | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 1f6f1d226..2a0e0b502 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -467,6 +467,7 @@ en: moderators: Moderators emails: Sending of emails newsletters: Newsletters + emails_download: Emails download valuators: Valuators poll_officers: Poll officers polls: Polls @@ -554,6 +555,12 @@ en: body: Email content body_help_text: This is how the users will see the email send_alert: Are you sure you want to send this newsletter to %{n} users? + emails_download: + index: + title: Emails download + download_segment: Download segment + download_segment_help_text: Download in CSV format + download_emails_button: Download emails list valuators: index: title: Valuators diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 455dae8dd..df4588bc8 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -466,6 +466,7 @@ es: moderators: Moderadores emails: Envío de emails newsletters: Newsletters + emails_download: Descarga de emails valuators: Evaluadores poll_officers: Presidentes de mesa polls: Votaciones @@ -553,6 +554,12 @@ es: body: Contenido del email body_help_text: Así es como verán el email los usuarios send_alert: ¿Estás seguro/a de que quieres enviar esta newsletter a %{n} usuarios? + emails_download: + index: + title: Descarga de emails + download_segment: Descargar segmento + download_segment_help_text: Descarga en formato CSV + download_emails_button: Descargar lista de emails valuators: index: title: Evaluadores From 752ffd678ff20a76e5c3fd9bb97fff36259dfe61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 15 Feb 2018 16:40:39 +0100 Subject: [PATCH 5/7] Added download emails spec and renamed newsletters spec --- .../admin/emails/emails_download_spec.rb | 30 +++++++++++++++++++ .../newsletters_spec.rb} | 0 2 files changed, 30 insertions(+) create mode 100644 spec/features/admin/emails/emails_download_spec.rb rename spec/features/admin/{newsletter/emails_spec.rb => emails/newsletters_spec.rb} (100%) diff --git a/spec/features/admin/emails/emails_download_spec.rb b/spec/features/admin/emails/emails_download_spec.rb new file mode 100644 index 000000000..ec3908307 --- /dev/null +++ b/spec/features/admin/emails/emails_download_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +feature "Admin download user emails" do + + background do + admin = create(:administrator) + login_as(admin.user) + end + + context "Index" do + scenario "returns the selected users segment csv file" do + user1 = create(:user) + user2 = create(:user) + + visit admin_emails_download_index_path + + within('#admin_download_emails') do + select 'All users', from: 'users_segment' + click_button 'Download emails list' + end + + header = page.response_headers['Content-Disposition'] + expect(header).to match /^attachment/ + expect(header).to match /filename="All users.csv"$/ + + expect(page).to have_content(user1.email) + expect(page).to have_content(user2.email) + end + end +end diff --git a/spec/features/admin/newsletter/emails_spec.rb b/spec/features/admin/emails/newsletters_spec.rb similarity index 100% rename from spec/features/admin/newsletter/emails_spec.rb rename to spec/features/admin/emails/newsletters_spec.rb From 7f30d0c620f4bf7afaad0632efb8327f633d395e Mon Sep 17 00:00:00 2001 From: rgarcia Date: Tue, 20 Feb 2018 17:33:54 +0100 Subject: [PATCH 6/7] Fix doble submit specs We have a custom implementation to avoid doble submissions which causes some custom problems :relieved: This commit should fix the following three specs spec/features/comments/legislation_annotations_spec.rb:367 spec/features/comments/legislation_questions_spec.rb:333 spec/features/custom/probe_option_comments_spec.rb:298 One of the custom problems comes from ajax:success[1] not being triggered, instead only ajax:complete seems to be triggered We should remove this custom implementation and use the standard solution provided in rails[2] [1] https://github.com/AyuntamientoMadrid/consul/blob/master/app/assets/java scripts/prevent_double_submission.js.coffee#L28 [2] https://stackoverflow.com/questions/9570912/how-to-disable-a-form-submit -button-a-l%C3%A0-ruby-on-rails-way/9572893#9572893 --- .../javascripts/prevent_double_submission.js.coffee | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/prevent_double_submission.js.coffee b/app/assets/javascripts/prevent_double_submission.js.coffee index 743119c22..5f080ddfe 100644 --- a/app/assets/javascripts/prevent_double_submission.js.coffee +++ b/app/assets/javascripts/prevent_double_submission.js.coffee @@ -22,11 +22,15 @@ App.PreventDoubleSubmission = initialize: -> $('form').on('submit', (event) -> - unless event.target.id == "new_officing_voter" || "admin_download_emails" + unless event.target.id == "new_officing_voter" || + event.target.id == "admin_download_emails" + buttons = $(this).find(':button, :submit') App.PreventDoubleSubmission.disable_buttons(buttons) ).on('ajax:success', (event) -> - unless event.target.id == "new_officing_voter" + unless event.target.id == "new_officing_voter" || + event.target.id == "admin_download_emails" + buttons = $(this).find(':button, :submit') App.PreventDoubleSubmission.reset_buttons(buttons) ) From f873bff5c62484e8c1525cb1ff05b2b0818513e6 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Tue, 20 Feb 2018 22:32:19 +0100 Subject: [PATCH 7/7] Fix admin menu controller name conditionals --- app/views/admin/_menu.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index c8dcfbbe2..134725cda 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -63,7 +63,7 @@
    >
  • > + %w(booth_assignments officer_assignments recounts results).include?(controller_name) %>> <%= link_to t('admin.menu.polls'), admin_polls_path %>
  • @@ -198,12 +198,12 @@ <% end %> -
  • +
  • > <%= t("admin.menu.emails") %> -
      > +
        >
      • > <%= link_to t("admin.menu.newsletters"), admin_newsletters_path %>