Refactor segment constant into a class method

We're going to make it dynamic using the geozones. Besides, class
methods can be overwritten using custom models, while constants can't be
overwritten without getting a warning [1].

Makes the definition of segments with geozones a little cleaner. I
think it’s worth it, compared to the slight memory gain of using a
constant [2].

[1] warning: already initialized constant UserSegments::SEGMENTS

[2] https://stackoverflow.com/questions/15903835/class-method-vs-constant-in-ruby-rails#answer-15903970
This commit is contained in:
rgarcia
2018-07-24 20:58:51 +02:00
committed by Javi Martín
parent 58aa861ef9
commit 68a2281203
7 changed files with 17 additions and 15 deletions

View File

@@ -1,6 +1,6 @@
module UserSegmentsHelper module UserSegmentsHelper
def user_segments_options def user_segments_options
UserSegments::SEGMENTS.map do |user_segment_name| UserSegments.segments.map do |user_segment_name|
[t("admin.segment_recipient.#{user_segment_name}"), user_segment_name] [t("admin.segment_recipient.#{user_segment_name}"), user_segment_name]
end end
end end

View File

@@ -14,7 +14,7 @@ section "Creating Newsletters" do
5.times do |n| 5.times do |n|
Newsletter.create!( Newsletter.create!(
subject: "Newsletter subject #{n}", subject: "Newsletter subject #{n}",
segment_recipient: UserSegments::SEGMENTS.sample, segment_recipient: UserSegments.segments.sample,
from: "no-reply@consul.dev", from: "no-reply@consul.dev",
body: newsletter_body.sample, body: newsletter_body.sample,
sent_at: [Time.current, nil].sample sent_at: [Time.current, nil].sample

View File

@@ -1,5 +1,6 @@
class UserSegments class UserSegments
SEGMENTS = %w[all_users def self.segments
%w[all_users
administrators administrators
all_proposal_authors all_proposal_authors
proposal_authors proposal_authors
@@ -8,6 +9,7 @@ class UserSegments
selected_investment_authors selected_investment_authors
winner_investment_authors winner_investment_authors
not_supported_on_current_budget].freeze not_supported_on_current_budget].freeze
end
def self.all_users def self.all_users
User.active.where.not(confirmed_at: nil) User.active.where.not(confirmed_at: nil)

View File

@@ -1,7 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory :newsletter do factory :newsletter do
sequence(:subject) { |n| "Subject #{n}" } sequence(:subject) { |n| "Subject #{n}" }
segment_recipient { UserSegments::SEGMENTS.sample } segment_recipient { UserSegments.segments.sample }
sequence(:from) { |n| "noreply#{n}@consul.dev" } sequence(:from) { |n| "noreply#{n}@consul.dev" }
sequence(:body) { |n| "Body #{n}" } sequence(:body) { |n| "Body #{n}" }
end end

View File

@@ -24,7 +24,7 @@ FactoryBot.define do
sequence(:title) { |n| "Admin Notification title #{n}" } sequence(:title) { |n| "Admin Notification title #{n}" }
sequence(:body) { |n| "Admin Notification body #{n}" } sequence(:body) { |n| "Admin Notification body #{n}" }
link { nil } link { nil }
segment_recipient { UserSegments::SEGMENTS.sample } segment_recipient { UserSegments.segments.sample }
recipients_count { nil } recipients_count { nil }
sent_at { nil } sent_at { nil }

View File

@@ -215,7 +215,7 @@ describe "Admin Notifications", :admin do
end end
scenario "Select list of users to send notification" do scenario "Select list of users to send notification" do
UserSegments::SEGMENTS.each do |user_segment| UserSegments.segments.each do |user_segment|
segment_recipient = I18n.t("admin.segment_recipient.#{user_segment}") segment_recipient = I18n.t("admin.segment_recipient.#{user_segment}")
visit new_admin_admin_notification_path visit new_admin_admin_notification_path

View File

@@ -162,7 +162,7 @@ describe "Admin newsletter emails", :admin do
end end
scenario "Select list of users to send newsletter" do scenario "Select list of users to send newsletter" do
UserSegments::SEGMENTS.each do |user_segment| UserSegments.segments.each do |user_segment|
visit new_admin_newsletter_path visit new_admin_newsletter_path
fill_in_newsletter_form(segment_recipient: I18n.t("admin.segment_recipient.#{user_segment}")) fill_in_newsletter_form(segment_recipient: I18n.t("admin.segment_recipient.#{user_segment}"))