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/assets/javascripts/prevent_double_submission.js.coffee b/app/assets/javascripts/prevent_double_submission.js.coffee
index 56a099730..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"
+ 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)
)
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/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb
index 896574e31..134725cda 100644
--- a/app/views/admin/_menu.html.erb
+++ b/app/views/admin/_menu.html.erb
@@ -63,7 +63,7 @@
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 %>
+
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
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
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
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