Files
nairobi/lib/user_segments.rb
Bertocq 6b41b6487b Add UserSegments#user_segment_emails helper method
Why:

Both Newsletters and Email Downloads need the same logic: To extract the
emails from all the users in the segment that have newsletter flag
active, removing all empty email values.

How:

1- UserSegments#user_segment_emails holds that repeated logic and is used
on both Newsletter & EmailDownload.

2- Rename Newsletter#list_of_recipients to list_of_recipient_emails as
it is more descriptive. There is no need to pass entire Users around,
only the emails are needed at Mailer#newsletter method.

3- Cleanup Newsletter#list_of_recipient_emails model spec scenario
2018-03-01 20:59:20 +01:00

54 lines
1.4 KiB
Ruby

class UserSegments
SEGMENTS = %w(all_users
administrators
proposal_authors
investment_authors
feasible_and_undecided_investment_authors
selected_investment_authors
winner_investment_authors)
def self.all_users
User.active
end
def self.administrators
all_users.administrators
end
def self.proposal_authors
author_ids(Proposal.not_archived.not_retired.pluck(:author_id).uniq)
end
def self.investment_authors
author_ids(current_budget_investments.pluck(:author_id).uniq)
end
def self.feasible_and_undecided_investment_authors
unfeasible_and_finished_condition = "feasibility = 'unfeasible' and valuation_finished = true"
investments = current_budget_investments.where.not(unfeasible_and_finished_condition)
author_ids(investments.pluck(:author_id).uniq)
end
def self.selected_investment_authors
author_ids(current_budget_investments.selected.pluck(:author_id).uniq)
end
def self.winner_investment_authors
author_ids(current_budget_investments.winners.pluck(:author_id).uniq)
end
def self.user_segment_emails(users_segment)
UserSegments.send(users_segment).newsletter.pluck(:email).compact
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