From 76b08398cf8f24bda5735ed45e77ad53fa164603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 2 Feb 2023 19:49:52 +0100 Subject: [PATCH] Extract component for balloting budget admin stats We're also moving the tests, but we're keeping one system test in order to test the controller and the navigation to get to this page. Note we're slightly changing the order of the methods in the component; the order of the instance variables was `user_`, `vote_`, `vote_`, `user_`, which was hard to follow. --- .../stats/budget_balloting_component.html.erb | 57 ++++++++++++++++++ .../admin/stats/budget_balloting_component.rb | 25 ++++++++ app/controllers/admin/stats_controller.rb | 8 --- .../admin/stats/budget_balloting.html.erb | 58 +------------------ .../stats/budget_balloting_component_spec.rb | 28 +++++++++ spec/system/admin/stats_spec.rb | 13 ----- 6 files changed, 111 insertions(+), 78 deletions(-) create mode 100644 app/components/admin/stats/budget_balloting_component.html.erb create mode 100644 app/components/admin/stats/budget_balloting_component.rb create mode 100644 spec/components/admin/stats/budget_balloting_component_spec.rb diff --git a/app/components/admin/stats/budget_balloting_component.html.erb b/app/components/admin/stats/budget_balloting_component.html.erb new file mode 100644 index 000000000..c76967f32 --- /dev/null +++ b/app/components/admin/stats/budget_balloting_component.html.erb @@ -0,0 +1,57 @@ +<%= back_link_to budgets_admin_stats_path %> + +

<%= budget.name %> - <%= t("admin.stats.budget_balloting.title") %>

+ +
+
+
+ +
+ +
+

+ <%= t("admin.stats.budget_balloting.participant_count") %> +
+ + <%= user_count %> + +

+
+
+
+ + + + + <% vote_count_by_heading.each do |heading_name, count| %> + + + + + <% end %> +
<%= t("admin.stats.budget_balloting.votes_per_heading") %>
+ <%= heading_name %> + + <%= number_with_delimiter count %> +
+ + + + + <% user_count_by_district.each do |heading_name, count| %> + + + + + <% end %> +
<%= t("admin.stats.budget_balloting.participants_per_district") %>
+ <%= heading_name %> + + <%= number_with_delimiter count %> +
diff --git a/app/components/admin/stats/budget_balloting_component.rb b/app/components/admin/stats/budget_balloting_component.rb new file mode 100644 index 000000000..b08bce488 --- /dev/null +++ b/app/components/admin/stats/budget_balloting_component.rb @@ -0,0 +1,25 @@ +class Admin::Stats::BudgetBallotingComponent < ApplicationComponent + attr_reader :budget + + def initialize(budget) + @budget = budget + end + + private + + def vote_count + budget.lines.count + end + + def user_count + budget.ballots.select { |ballot| ballot.lines.any? }.count + end + + def vote_count_by_heading + budget.lines.group(:heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort + end + + def user_count_by_district + User.where.not(balloted_heading_id: nil).group(:balloted_heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort + end +end diff --git a/app/controllers/admin/stats_controller.rb b/app/controllers/admin/stats_controller.rb index 5f91ce61f..8634788a8 100644 --- a/app/controllers/admin/stats_controller.rb +++ b/app/controllers/admin/stats_controller.rb @@ -75,14 +75,6 @@ class Admin::StatsController < Admin::BaseController @budget = Budget.find(params[:budget_id]) authorize! :read_admin_stats, @budget, message: t("admin.stats.budgets.no_data_before_balloting_phase") - - @user_count = @budget.ballots.select { |ballot| ballot.lines.any? }.count - - @vote_count = @budget.lines.count - - @vote_count_by_heading = @budget.lines.group(:heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort - - @user_count_by_district = User.where.not(balloted_heading_id: nil).group(:balloted_heading_id).count.map { |k, v| [Budget::Heading.find(k).name, v] }.sort end def polls diff --git a/app/views/admin/stats/budget_balloting.html.erb b/app/views/admin/stats/budget_balloting.html.erb index 72d1d6a54..6b7cb33c7 100644 --- a/app/views/admin/stats/budget_balloting.html.erb +++ b/app/views/admin/stats/budget_balloting.html.erb @@ -1,57 +1 @@ -<%= back_link_to budgets_admin_stats_path %> - -

<%= @budget.name %> - <%= t("admin.stats.budget_balloting.title") %>

- -
-
-
- -
- -
-

- <%= t("admin.stats.budget_balloting.participant_count") %> -
- - <%= @user_count %> - -

-
-
-
- - - - - <% @vote_count_by_heading.each do |heading_name, count| %> - - - - - <% end %> -
<%= t("admin.stats.budget_balloting.votes_per_heading") %>
- <%= heading_name %> - - <%= number_with_delimiter count %> -
- - - - - <% @user_count_by_district.each do |heading_name, count| %> - - - - - <% end %> -
<%= t("admin.stats.budget_balloting.participants_per_district") %>
- <%= heading_name %> - - <%= number_with_delimiter count %> -
+<%= render Admin::Stats::BudgetBallotingComponent.new(@budget) %> diff --git a/spec/components/admin/stats/budget_balloting_component_spec.rb b/spec/components/admin/stats/budget_balloting_component_spec.rb new file mode 100644 index 000000000..9595559ef --- /dev/null +++ b/spec/components/admin/stats/budget_balloting_component_spec.rb @@ -0,0 +1,28 @@ +require "rails_helper" + +describe Admin::Stats::BudgetBallotingComponent do + let(:budget) { create(:budget, :balloting) } + let(:heading) { create(:budget_heading, budget: budget) } + let(:investment) { create(:budget_investment, :feasible, :selected, heading: heading) } + + it "shows the number of votes in investment projects" do + investment_2 = create(:budget_investment, :feasible, :selected, budget: budget) + + create(:user, ballot_lines: [investment, investment_2]) + create(:user, ballot_lines: [investment_2]) + + render_inline Admin::Stats::BudgetBallotingComponent.new(budget) + + expect(page).to have_css "p", exact_text: "Votes 3", normalize_ws: true + end + + it "shows the number of users that have voted a investment project" do + create(:user, ballot_lines: [investment]) + create(:user, ballot_lines: [investment]) + create(:user) + + render_inline Admin::Stats::BudgetBallotingComponent.new(budget) + + expect(page).to have_css "p", exact_text: "Participants 2", normalize_ws: true + end +end diff --git a/spec/system/admin/stats_spec.rb b/spec/system/admin/stats_spec.rb index 590af7591..62f11d3a2 100644 --- a/spec/system/admin/stats_spec.rb +++ b/spec/system/admin/stats_spec.rb @@ -195,19 +195,6 @@ describe "Stats", :admin do end expect(page).to have_content "VOTES\n3" - end - - scenario "Number of users that have voted a investment project" do - create(:user, ballot_lines: [investment]) - create(:user, ballot_lines: [investment]) - create(:user) - - visit admin_stats_path - click_link "Participatory Budgets" - within("#budget_#{budget.id}") do - click_link "Final voting" - end - expect(page).to have_content "PARTICIPANTS\n2" end end