From 07f2419f8f2f0872030a9a5d9edf28f57003bcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Fri, 9 Feb 2018 17:48:52 +0100 Subject: [PATCH] Added newsletter model and UserSegments class --- app/models/newsletter.rb | 23 ++++++++++++++++++++++ lib/user_segments.rb | 42 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 app/models/newsletter.rb create mode 100644 lib/user_segments.rb diff --git a/app/models/newsletter.rb b/app/models/newsletter.rb new file mode 100644 index 000000000..0b7bc4481 --- /dev/null +++ b/app/models/newsletter.rb @@ -0,0 +1,23 @@ +class Newsletter < ActiveRecord::Base + enum segment_recipient: { all_users: 1, + proposal_authors: 2, + investment_authors: 3, + feasible_and_undecided_investment_authors: 4, + selected_investment_authors: 5, + winner_investment_authors: 6 } + + validates :subject, presence: true + validates :segment_recipient, presence: true + validates :from, presence: true + validates :body, presence: true + + validates_format_of :from, :with => /@/ + + def list_of_recipients + UserSegments.send(segment_recipient) + end + + def draft? + sent_at.nil? + end +end diff --git a/lib/user_segments.rb b/lib/user_segments.rb new file mode 100644 index 000000000..a52ec1b03 --- /dev/null +++ b/lib/user_segments.rb @@ -0,0 +1,42 @@ +class UserSegments + def self.all_users + User.newsletter.active + end + + def self.proposal_authors + author_ids = Proposal.not_archived.not_retired.pluck(:author_id).uniq + author_ids(author_ids) + end + + def self.investment_authors + author_ids = current_budget_investments.pluck(:author_id).uniq + author_ids(author_ids) + end + + def self.feasible_and_undecided_investment_authors + author_ids = current_budget_investments.where(feasibility: %w(feasible undecided)) + .pluck(:author_id).uniq + + author_ids(author_ids) + end + + def self.selected_investment_authors + author_ids = current_budget_investments.selected.pluck(:author_id).uniq + author_ids(author_ids) + end + + def self.winner_investment_authors + author_ids = current_budget_investments.winners.pluck(:author_id).uniq + author_ids(author_ids) + end + + private + + def self.current_budget_investments + Budget.current.investments + end + + def self.author_ids(author_ids) + all_users.where(id: author_ids) + end +end