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.
This commit is contained in:
Javi Martín
2025-03-27 18:54:28 +01:00
parent 4c4aa210a1
commit 985d3da032
6 changed files with 58 additions and 46 deletions

View File

@@ -3,7 +3,7 @@
<% if vote_collection_shift? %> <% if vote_collection_shift? %>
<li class="<%= "js-vote-collection" %> <li class="<%= "js-vote-collection" %>
<%= " is-active" if controller_name == "voters" %> <%= " 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" %> <%= link_to t("officing.menu.voters"), new_officing_residence_path, class: "users-link" %>
</li> </li>
<% end %> <% end %>

View File

@@ -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

View File

@@ -1,12 +1,4 @@
module OfficersHelper 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? def no_shifts?
current_user.poll_officer.officer_assignments.where(date: Time.current.to_date).blank? current_user.poll_officer.officer_assignments.where(date: Time.current.to_date).blank?
end end

View File

@@ -17,6 +17,8 @@
<%= render Admin::MenuComponent.new %> <%= render Admin::MenuComponent.new %>
<% elsif namespace == "sdg_management" %> <% elsif namespace == "sdg_management" %>
<%= render SDGManagement::MenuComponent.new %> <%= render SDGManagement::MenuComponent.new %>
<% elsif namespace == "officing" %>
<%= render Officing::MenuComponent.new(voter_user: @user) %>
<% else %> <% else %>
<%= render "/#{namespace}/menu" %> <%= render "/#{namespace}/menu" %>
<% end %> <% end %>

View File

@@ -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

View File

@@ -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