Files
nairobi/lib/user_segments.rb
Javi Martín d639cd587a Remove unnecessary uniq calls
The code `where(id: ids)` is equivalent to `where(id: ids.uniq)`.

Since Rails 5 uses `distinct` instead of `uniq` and in most cases where
we use `uniq` with `pluck` we should simply remove the `uniq` call (as
done in this commit), we're also removing the `Rails/UniqBeforePluck`
rubocop rule.
2019-09-10 22:27:33 +02:00

71 lines
1.9 KiB
Ruby

class UserSegments
SEGMENTS = %w[all_users
administrators
all_proposal_authors
proposal_authors
investment_authors
feasible_and_undecided_investment_authors
selected_investment_authors
winner_investment_authors
not_supported_on_current_budget]
def self.all_users
User.active
end
def self.administrators
all_users.administrators
end
def self.all_proposal_authors
author_ids(Proposal.pluck(:author_id))
end
def self.proposal_authors
author_ids(Proposal.not_archived.not_retired.pluck(:author_id))
end
def self.investment_authors
author_ids(current_budget_investments.pluck(:author_id))
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))
end
def self.selected_investment_authors
author_ids(current_budget_investments.selected.pluck(:author_id))
end
def self.winner_investment_authors
author_ids(current_budget_investments.winners.pluck(:author_id))
end
def self.not_supported_on_current_budget
author_ids(
User.where(
"id NOT IN (SELECT DISTINCT(voter_id) FROM votes"\
" WHERE votable_type = ? AND votes.votable_id IN (?))",
"Budget::Investment",
current_budget_investments.pluck(:id)
)
)
end
def self.user_segment_emails(users_segment)
UserSegments.send(users_segment).newsletter.order(:created_at).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