Merge pull request #4361 from consul/sdg_per_budget_stats

Split investment projects statistics by participatory budget
This commit is contained in:
Javi Martín
2021-02-26 14:41:13 +01:00
committed by GitHub
6 changed files with 64 additions and 14 deletions

View File

@@ -0,0 +1,5 @@
.sdg-goal-stats {
h4 {
@include grid-column-gutter;
}
}

View File

@@ -4,9 +4,14 @@
<div class="stats-numbers">
<% stats.each do |text, amount, options = {}| %>
<div class="small-12 medium-4 column">
<%= tag.p(options) do %>
<%= text %> <br><span class="number"><%= amount %></span>
<%= render Admin::Stats::StatComponent.new(text: text, amount: amount, options: options) %>
<% end %>
<% bugdets_stats.each do |budget_name, *budget_stats| %>
<div class="budget-stats">
<h4><%= budget_name %></h4>
<% budget_stats.each do |text, amount, options = {}| %>
<%= render Admin::Stats::StatComponent.new(text: text, amount: amount, options: options) %>
<% end %>
</div>
<% end %>

View File

@@ -13,15 +13,35 @@ class Admin::Stats::SDG::GoalComponent < ApplicationComponent
[
[t("admin.stats.sdg.polls"), goal.polls.count],
[t("admin.stats.sdg.proposals"), goal.proposals.count],
[t("admin.stats.sdg.debates"), goal.debates.count],
[t("admin.stats.sdg.budget_investments.sent"), goal.budget_investments.count],
[t("admin.stats.sdg.budget_investments.winners"), goal.budget_investments.winners.count, featured],
[t("admin.stats.sdg.budget_investments.amount"), amount, featured]
[t("admin.stats.sdg.debates"), goal.debates.count]
]
end
def amount
number_to_currency(goal.budget_investments.winners.sum(:price), precision: 0)
def bugdets_stats
Budget.order(created_at: :desc).map do |budget|
[
budget.name,
[t("admin.stats.sdg.budget_investments.sent"), sent(budget)],
[t("admin.stats.sdg.budget_investments.winners"), winners(budget), featured],
[t("admin.stats.sdg.budget_investments.amount"), amount(budget), featured]
]
end
end
def sent(budget)
investments(budget).count
end
def winners(budget)
investments(budget).winners.count
end
def amount(budget)
number_to_currency(investments(budget).winners.sum(:price), precision: 0)
end
def investments(budget)
goal.budget_investments.by_budget(budget)
end
def featured

View File

@@ -0,0 +1,5 @@
<div class="small-12 medium-4 column">
<%= tag.p(options) do %>
<%= text %> <br><span class="number"><%= amount %></span>
<% end %>
</div>

View File

@@ -0,0 +1,9 @@
class Admin::Stats::StatComponent < ApplicationComponent
attr_reader :text, :amount, :options
def initialize(text:, amount:, options: {})
@text = text
@amount = amount
@options = options
end
end

View File

@@ -8,16 +8,22 @@ describe Admin::Stats::SDG::GoalComponent, type: :component do
create_list(:poll, 2, sdg_goals: [goal])
create_list(:proposal, 3, sdg_goals: [goal])
create_list(:debate, 1, sdg_goals: [goal])
create_list(:budget_investment, 2, :winner, sdg_goals: [goal], price: 1000)
create_list(:budget_investment, 2, sdg_goals: [goal], price: 1000)
past = create(:budget, name: "Past year budget")
create_list(:budget_investment, 1, :winner, sdg_goals: [goal], price: 1000, budget: past)
current = create(:budget, name: "Current budget")
create_list(:budget_investment, 2, sdg_goals: [goal], price: 1000, budget: current)
render_inline component
expect(page).to have_text "Proposals 3"
expect(page).to have_text "Polls 2"
expect(page).to have_text "Debates 1"
expect(page).to have_text "Investment projects sent 4"
expect(page).to have_text "Winner investment projects 2"
expect(page).to have_text "Approved amount $2,000"
expect("Current budget").to appear_before("Past year budget")
expect(page).to have_text "Investment projects sent 2"
expect(page).to have_text "Winner investment projects 0"
expect(page).to have_text "Approved amount $0"
expect(page).to have_text "Investment projects sent 1"
expect(page).to have_text "Winner investment projects 1"
expect(page).to have_text "Approved amount $1,000"
end
end