diff --git a/app/controllers/admin/emails_download_controller.rb b/app/controllers/admin/emails_download_controller.rb index 3bafa7668..b05ad83b7 100644 --- a/app/controllers/admin/emails_download_controller.rb +++ b/app/controllers/admin/emails_download_controller.rb @@ -4,7 +4,7 @@ class Admin::EmailsDownloadController < Admin::BaseController def generate_csv users_segment = params[:users_segment] - filename = t("admin.segment_recipient.#{users_segment}") + filename = UserSegments.segment_name(users_segment) csv_file = users_segment_emails_csv(users_segment) send_data csv_file, filename: "#{filename}.csv" diff --git a/app/helpers/user_segments_helper.rb b/app/helpers/user_segments_helper.rb index 16ee9e923..3f4d5e2e1 100644 --- a/app/helpers/user_segments_helper.rb +++ b/app/helpers/user_segments_helper.rb @@ -1,15 +1,11 @@ module UserSegmentsHelper def user_segments_options UserSegments.segments.map do |user_segment_name| - [t("admin.segment_recipient.#{user_segment_name}"), user_segment_name] + [segment_name(user_segment_name), user_segment_name] end end def segment_name(user_segment) - if user_segment && UserSegments.respond_to?(user_segment) - I18n.t("admin.segment_recipient.#{user_segment}") - else - I18n.t("admin.segment_recipient.invalid_recipients_segment") - end + UserSegments.segment_name(user_segment) || I18n.t("admin.segment_recipient.invalid_recipients_segment") end end diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 2cf5a6e35..b5932360a 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -81,6 +81,7 @@ search: - app/ - db/pages/ - db/dev_seeds/ + - lib/ ## Root directories for relative keys resolution. # relative_roots: diff --git a/lib/user_segments.rb b/lib/user_segments.rb index 03a0497c9..b125f42c2 100644 --- a/lib/user_segments.rb +++ b/lib/user_segments.rb @@ -11,6 +11,10 @@ class UserSegments not_supported_on_current_budget].freeze end + def self.segment_name(segment) + I18n.t("admin.segment_recipient.#{segment}") if segments.include?(segment.to_s) + end + def self.all_users User.active.where.not(confirmed_at: nil) end @@ -53,8 +57,8 @@ class UserSegments ) end - def self.user_segment_emails(users_segment) - UserSegments.send(users_segment).newsletter.order(:created_at).pluck(:email).compact + def self.user_segment_emails(segment) + UserSegments.send(segment).newsletter.order(:created_at).pluck(:email).compact end private diff --git a/spec/lib/user_segments_spec.rb b/spec/lib/user_segments_spec.rb index 8d7f6eceb..01194f42e 100644 --- a/spec/lib/user_segments_spec.rb +++ b/spec/lib/user_segments_spec.rb @@ -5,6 +5,22 @@ describe UserSegments do let(:user2) { create(:user) } let(:user3) { create(:user) } + describe ".segment_name" do + it "returns a readable name of the segment" do + expect(UserSegments.segment_name("all_users")).to eq "All users" + expect(UserSegments.segment_name("administrators")).to eq "Administrators" + expect(UserSegments.segment_name("proposal_authors")).to eq "Proposal authors" + end + + it "accepts symbols as parameters" do + expect(UserSegments.segment_name(:all_users)).to eq "All users" + end + + it "returns nil for invalid segments" do + expect(UserSegments.segment_name("invalid")).to be nil + end + end + describe ".all_users" do it "returns all active users enabled" do active_user = create(:user) diff --git a/spec/system/admin/admin_notifications_spec.rb b/spec/system/admin/admin_notifications_spec.rb index 596ecd3c5..9a3adaf49 100644 --- a/spec/system/admin/admin_notifications_spec.rb +++ b/spec/system/admin/admin_notifications_spec.rb @@ -215,15 +215,15 @@ describe "Admin Notifications", :admin do end scenario "Select list of users to send notification" do - UserSegments.segments.each do |user_segment| - segment_recipient = I18n.t("admin.segment_recipient.#{user_segment}") + UserSegments.segments.each do |segment| + segment_recipient = UserSegments.segment_name(segment) visit new_admin_admin_notification_path fill_in_admin_notification_form(segment_recipient: segment_recipient) click_button "Create notification" - expect(page).to have_content(I18n.t("admin.segment_recipient.#{user_segment}")) + expect(page).to have_content segment_recipient end end end diff --git a/spec/system/admin/emails/newsletters_spec.rb b/spec/system/admin/emails/newsletters_spec.rb index a64f2d987..3af4f19d1 100644 --- a/spec/system/admin/emails/newsletters_spec.rb +++ b/spec/system/admin/emails/newsletters_spec.rb @@ -16,7 +16,7 @@ describe "Admin newsletter emails", :admin do expect(page).to have_link "Go back", href: admin_newsletters_path expect(page).to have_content "This is a subject" - expect(page).to have_content I18n.t("admin.segment_recipient.#{newsletter.segment_recipient}") + expect(page).to have_content "All users" expect(page).to have_content "no-reply@consul.dev" expect(page).to have_content "This is a body" end @@ -40,10 +40,9 @@ describe "Admin newsletter emails", :admin do expect(page).to have_css(".newsletter", count: 3) newsletters.each do |newsletter| - segment_recipient = I18n.t("admin.segment_recipient.#{newsletter.segment_recipient}") within("#newsletter_#{newsletter.id}") do expect(page).to have_content newsletter.subject - expect(page).to have_content segment_recipient + expect(page).to have_content UserSegments.segment_name(newsletter.segment_recipient) end end end @@ -162,13 +161,15 @@ describe "Admin newsletter emails", :admin do end scenario "Select list of users to send newsletter" do - UserSegments.segments.each do |user_segment| + UserSegments.segments.each do |segment| + segment_recipient = UserSegments.segment_name(segment) + visit new_admin_newsletter_path - fill_in_newsletter_form(segment_recipient: I18n.t("admin.segment_recipient.#{user_segment}")) + fill_in_newsletter_form(segment_recipient: segment_recipient) click_button "Create Newsletter" - expect(page).to have_content(I18n.t("admin.segment_recipient.#{user_segment}")) + expect(page).to have_content segment_recipient end end end