Added newsletter model and UserSegments class

This commit is contained in:
María Checa
2018-02-09 17:48:52 +01:00
parent a22faadc1f
commit 07f2419f8f
2 changed files with 65 additions and 0 deletions

23
app/models/newsletter.rb Normal file
View File

@@ -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

42
lib/user_segments.rb Normal file
View File

@@ -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