From 985d3da032f0a2b6667c1b52dec4308c17ef8f4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 27 Mar 2025 18:54:28 +0100 Subject: [PATCH] Move officing menu partial to a component This way we can move some system tests to component tests and stop creating records after starting the browser with a `visit`. We could also split the system test in two, but since these tests aren't checking any user interactions, moving the to component tests we check the same things while making the tests faster. Since the partial was using an instance variable, we're passing it to the component. We're naming it `voter_user` instead of `user` because passing something named `user` could make us think that we're passing the `current_user`. I wasn't sure about naming it `voter` because it's a `User` record and not a `Poll::Voter` record, but naming it `voter` would definitely be an option. --- .../officing/menu_component.html.erb} | 2 +- app/components/officing/menu_component.rb | 18 +++++++++ app/helpers/officers_helper.rb | 8 ---- app/views/layouts/admin.html.erb | 2 + .../officing/menu_component_spec.rb | 37 +++++++++++++++++++ spec/system/budget_polls/officing_spec.rb | 37 ------------------- 6 files changed, 58 insertions(+), 46 deletions(-) rename app/{views/officing/_menu.html.erb => components/officing/menu_component.html.erb} (94%) create mode 100644 app/components/officing/menu_component.rb create mode 100644 spec/components/officing/menu_component_spec.rb delete mode 100644 spec/system/budget_polls/officing_spec.rb diff --git a/app/views/officing/_menu.html.erb b/app/components/officing/menu_component.html.erb similarity index 94% rename from app/views/officing/_menu.html.erb rename to app/components/officing/menu_component.html.erb index 71c65b884..23b91e04f 100644 --- a/app/views/officing/_menu.html.erb +++ b/app/components/officing/menu_component.html.erb @@ -3,7 +3,7 @@ <% if vote_collection_shift? %>
  • <%= " is-active" if controller_name == "voters" %> - <%= " is-hidden" if controller_name == "voters" && Poll.votable_by(@user).any? %>"> + <%= " is-hidden" if controller_name == "voters" && Poll.votable_by(voter_user).any? %>"> <%= link_to t("officing.menu.voters"), new_officing_residence_path, class: "users-link" %>
  • <% end %> diff --git a/app/components/officing/menu_component.rb b/app/components/officing/menu_component.rb new file mode 100644 index 000000000..eb5df5a54 --- /dev/null +++ b/app/components/officing/menu_component.rb @@ -0,0 +1,18 @@ +class Officing::MenuComponent < ApplicationComponent + attr_reader :voter_user + use_helpers :current_user + + def initialize(voter_user:) + @voter_user = voter_user + end + + private + + def vote_collection_shift? + current_user.poll_officer.officer_assignments.voting_days.where(date: Time.current.to_date).any? + end + + def final_recount_shift? + current_user.poll_officer.officer_assignments.final.where(date: Time.current.to_date).any? + end +end diff --git a/app/helpers/officers_helper.rb b/app/helpers/officers_helper.rb index 9e6a6bb47..9a9d1aded 100644 --- a/app/helpers/officers_helper.rb +++ b/app/helpers/officers_helper.rb @@ -1,12 +1,4 @@ module OfficersHelper - def vote_collection_shift? - current_user.poll_officer.officer_assignments.voting_days.where(date: Time.current.to_date).any? - end - - def final_recount_shift? - current_user.poll_officer.officer_assignments.final.where(date: Time.current.to_date).any? - end - def no_shifts? current_user.poll_officer.officer_assignments.where(date: Time.current.to_date).blank? end diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index e88e4773e..fc6807977 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -17,6 +17,8 @@ <%= render Admin::MenuComponent.new %> <% elsif namespace == "sdg_management" %> <%= render SDGManagement::MenuComponent.new %> + <% elsif namespace == "officing" %> + <%= render Officing::MenuComponent.new(voter_user: @user) %> <% else %> <%= render "/#{namespace}/menu" %> <% end %> diff --git a/spec/components/officing/menu_component_spec.rb b/spec/components/officing/menu_component_spec.rb new file mode 100644 index 000000000..025eda47d --- /dev/null +++ b/spec/components/officing/menu_component_spec.rb @@ -0,0 +1,37 @@ +require "rails_helper" + +describe Officing::MenuComponent do + let(:booth) { create(:poll_booth) } + let(:officer) { create(:poll_officer) } + let(:component) { Officing::MenuComponent.new(voter_user: nil) } + + before do + create(:poll_booth_assignment, booth: booth) + sign_in(officer.user) + end + + it "shows the validate document link when there are vote collection shifts assigned" do + create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection) + + render_inline component + + expect(page).to have_content "Validate document" + expect(page).not_to have_content "Total recounts and results" + end + + it "shows the total recounts link when there are recount scrutinity shifts assigned" do + create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :recount_scrutiny) + + render_inline component + + expect(page).not_to have_content "Validate document" + expect(page).to have_content "Total recounts and results" + end + + it "does not show any links when there are no shifts assigned" do + render_inline component + + expect(page).not_to have_content "Validate document" + expect(page).not_to have_content "Total recounts and results" + end +end diff --git a/spec/system/budget_polls/officing_spec.rb b/spec/system/budget_polls/officing_spec.rb deleted file mode 100644 index 87ec22f2b..000000000 --- a/spec/system/budget_polls/officing_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -require "rails_helper" - -describe "Budget Poll Officing" do - scenario "Show sidebar menus if officer has shifts assigned" do - booth = create(:poll_booth) - booth_assignment = create(:poll_booth_assignment, booth: booth) - officer = create(:poll_officer) - - create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :vote_collection) - - login_as officer.user - visit officing_root_path - - expect(page).not_to have_content("You don't have officing shifts today") - expect(page).to have_content("Validate document") - expect(page).not_to have_content("Total recounts and results") - - create(:poll_shift, officer: officer, booth: booth, date: Date.current, task: :recount_scrutiny) - create(:poll_officer_assignment, booth_assignment: booth_assignment, officer: officer) - - refresh - - expect(page).not_to have_content("You don't have officing shifts today") - expect(page).to have_content("Validate document") - expect(page).to have_content("Total recounts and results") - end - - scenario "Do not show sidebar menus if officer has no shifts assigned" do - login_as(create(:poll_officer).user) - - visit officing_root_path - - expect(page).to have_content("You don't have officing shifts today") - expect(page).not_to have_content("Validate document") - expect(page).not_to have_content("Total recounts and results") - end -end