Use budget stats model to calculate admin stats
This way we reduce duplication.
This commit is contained in:
@@ -7,12 +7,20 @@ class Admin::Stats::BudgetBallotingComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def stats
|
||||||
|
@stats ||= Budget::Stats.new(budget)
|
||||||
|
end
|
||||||
|
|
||||||
|
def headings_stats
|
||||||
|
@headings_stats ||= stats.headings
|
||||||
|
end
|
||||||
|
|
||||||
def vote_count
|
def vote_count
|
||||||
budget.lines.count
|
stats.total_votes
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_count
|
def user_count
|
||||||
budget.ballots.select { |ballot| ballot.lines.any? }.count
|
stats.total_participants_vote_phase
|
||||||
end
|
end
|
||||||
|
|
||||||
def vote_count_by_heading
|
def vote_count_by_heading
|
||||||
@@ -21,9 +29,7 @@ class Admin::Stats::BudgetBallotingComponent < ApplicationComponent
|
|||||||
|
|
||||||
def user_count_by_heading
|
def user_count_by_heading
|
||||||
budget.headings.map do |heading|
|
budget.headings.map do |heading|
|
||||||
ballots = budget.ballots.joins(:lines).where(budget_ballot_lines: { heading_id: heading })
|
[heading.name, headings_stats[heading.id][:total_participants_vote_phase]]
|
||||||
|
|
||||||
[heading.name, ballots.select(:user_id).distinct.count]
|
|
||||||
end.select { |_, count| count > 0 }.sort
|
end.select { |_, count| count > 0 }.sort
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,30 +7,25 @@ class Admin::Stats::BudgetSupportingComponent < ApplicationComponent
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def votes
|
def stats
|
||||||
Vote.where(votable_type: "Budget::Investment")
|
@stats ||= Budget::Stats.new(budget)
|
||||||
.includes(:budget_investment)
|
end
|
||||||
.where(budget_investments: { heading_id: budget.heading_ids })
|
|
||||||
|
def headings_stats
|
||||||
|
@headings_stats ||= stats.headings
|
||||||
end
|
end
|
||||||
|
|
||||||
def vote_count
|
def vote_count
|
||||||
votes.count
|
stats.total_supports
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_count
|
def user_count
|
||||||
votes.select(:voter_id).distinct.count
|
stats.total_participants_support_phase
|
||||||
end
|
end
|
||||||
|
|
||||||
def user_count_by_heading
|
def user_count_by_heading
|
||||||
budget.headings.map do |heading|
|
budget.headings.map do |heading|
|
||||||
[heading, voters_in_heading(heading)]
|
[heading, headings_stats[heading.id][:total_participants_support_phase]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def voters_in_heading(heading)
|
|
||||||
Vote.where(votable_type: "Budget::Investment")
|
|
||||||
.includes(:budget_investment)
|
|
||||||
.where(budget_investments: { heading_id: heading.id })
|
|
||||||
.select("votes.voter_id").distinct.count
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -53,6 +53,10 @@ class Budget::Stats
|
|||||||
budget.investments.count
|
budget.investments.count
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def total_supports
|
||||||
|
supports(budget).count
|
||||||
|
end
|
||||||
|
|
||||||
def total_votes
|
def total_votes
|
||||||
budget.ballots.pluck(:ballot_lines_count).sum
|
budget.ballots.pluck(:ballot_lines_count).sum
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user