Move poll callout partial to a component

This way it'll be easier to refactor it.

Note there was a system test which tested both the callout and the form
when unverified users visit a poll. We've split this system test in two
component tests.
This commit is contained in:
Javi Martín
2025-07-09 13:40:54 +02:00
parent a7e1b42b6c
commit 5402cb6042
7 changed files with 51 additions and 38 deletions

View File

@@ -1,4 +1,4 @@
<% unless can?(:answer, @poll) %>
<% unless can?(:answer, poll) %>
<% if current_user.nil? %>
<div class="callout primary">
<%= sanitize(t("polls.show.cant_answer_not_logged_in",
@@ -10,7 +10,7 @@
<%= sanitize(t("polls.show.cant_answer_verify",
verify_link: link_to(t("polls.show.verify_link"), verification_path))) %>
</div>
<% elsif @poll.expired? %>
<% elsif poll.expired? %>
<div class="callout alert">
<%= t("polls.show.cant_answer_expired") %>
</div>

View File

@@ -0,0 +1,8 @@
class Polls::CalloutComponent < ApplicationComponent
attr_reader :poll
use_helpers :can?, :current_user, :link_to_signin, :link_to_signup
def initialize(poll)
@poll = poll
end
end

View File

@@ -16,7 +16,7 @@
<div class="row margin">
<div class="small-12 medium-9 column">
<%= render "callout" %>
<%= render Polls::CalloutComponent.new(@poll) %>
<% if @poll.voted_in_booth?(current_user) %>
<div class="callout warning">

View File

@@ -0,0 +1,27 @@
require "rails_helper"
describe Polls::CalloutComponent do
it "asks anonymous users to sign in" do
render_inline Polls::CalloutComponent.new(create(:poll))
expect(page).to have_content "You must sign in or sign up to participate"
end
it "shows a message to level 2 users when a poll has finished" do
sign_in(create(:user, :level_two))
render_inline Polls::CalloutComponent.new(create(:poll, :expired))
expect(page).to have_content "This poll has finished"
end
it "asks unverified users to verify their account" do
sign_in(create(:user, :incomplete_verification))
render_inline Polls::CalloutComponent.new(create(:poll))
expect(page).to have_content "You must verify your account in order to answer"
expect(page).not_to have_content "You have already participated in this poll. " \
"If you vote again it will be overwritten"
end
end

View File

@@ -20,6 +20,19 @@ describe Polls::FormComponent do
expect(page).to have_button "Vote", disabled: true
end
it "renders disabled answers to unverified users" do
sign_in(create(:user, :incomplete_verification))
render_inline Polls::FormComponent.new(web_vote)
page.find("fieldset[disabled]") do |fieldset|
expect(fieldset).to have_field "Yes"
expect(fieldset).to have_field "No"
end
expect(page).to have_button "Vote", disabled: true
end
context "expired poll" do
let(:poll) { create(:poll, :expired) }

View File

@@ -168,14 +168,6 @@ describe "Polls" do
end
end
scenario "Non-logged in users" do
create(:poll_question, :yes_no, poll: poll)
visit poll_path(poll)
expect(page).to have_content("You must sign in or sign up to participate")
end
scenario "Level 1 users" do
poll.update!(geozone_restricted_to: [geozone])
create(:poll_question, :yes_no, poll: poll)
@@ -186,17 +178,6 @@ describe "Polls" do
expect(page).to have_content("You must verify your account in order to answer")
end
scenario "Level 2 users in an expired poll" do
expired_poll = create(:poll, :expired)
create(:poll_question, :yes_no, poll: expired_poll)
login_as(create(:user, :level_two, geozone: geozone))
visit poll_path(expired_poll)
expect(page).to have_content("This poll has finished")
end
scenario "Level 2 users answering" do
poll.update!(geozone_restricted_to: [geozone])
create(:poll_question, :yes_no, poll: poll, title: "Do you agree?")

View File

@@ -28,22 +28,6 @@ describe "Voter" do
"If you vote again it will be overwritten."
end
scenario "Voting via web as unverified user" do
user = create(:user, :incomplete_verification)
login_as user
visit poll_path(poll)
within_fieldset "Is this question stupid?" do
expect(page).to have_field "Yes", type: :radio, disabled: true
expect(page).to have_field "No", type: :radio, disabled: true
end
expect(page).to have_content "You must verify your account in order to answer"
expect(page).not_to have_content "You have already participated in this poll. " \
"If you vote again it will be overwritten"
end
scenario "Voting in booth" do
admin_user = admin.user
login_through_form_as_officer(officer)