From de303aa1dfc04091521048e7f0a49eaee2abf911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 26 Aug 2025 18:42:03 +0200 Subject: [PATCH] Extract component to render dashboard polls Just like we usually do when reorganizing code. --- .../dashboard/polls_component.html.erb | 5 +++ app/components/dashboard/polls_component.rb | 11 +++++ app/views/dashboard/polls/index.html.erb | 8 +--- .../dashboard/poll_component_spec.rb | 43 ++++++++----------- .../dashboard/polls_component_spec.rb | 9 ++++ 5 files changed, 43 insertions(+), 33 deletions(-) create mode 100644 app/components/dashboard/polls_component.html.erb create mode 100644 app/components/dashboard/polls_component.rb create mode 100644 spec/components/dashboard/polls_component_spec.rb diff --git a/app/components/dashboard/polls_component.html.erb b/app/components/dashboard/polls_component.html.erb new file mode 100644 index 000000000..68cb5b18a --- /dev/null +++ b/app/components/dashboard/polls_component.html.erb @@ -0,0 +1,5 @@ +
+ <% polls.each do |poll| %> + <%= render Dashboard::PollComponent.new(poll) %> + <% end %> +
diff --git a/app/components/dashboard/polls_component.rb b/app/components/dashboard/polls_component.rb new file mode 100644 index 000000000..4dad2c277 --- /dev/null +++ b/app/components/dashboard/polls_component.rb @@ -0,0 +1,11 @@ +class Dashboard::PollsComponent < ApplicationComponent + attr_reader :polls + + def initialize(polls) + @polls = polls + end + + def render? + polls.any? + end +end diff --git a/app/views/dashboard/polls/index.html.erb b/app/views/dashboard/polls/index.html.erb index ad362dd84..035874638 100644 --- a/app/views/dashboard/polls/index.html.erb +++ b/app/views/dashboard/polls/index.html.erb @@ -3,13 +3,7 @@
<%= Setting["proposals.poll_description"] %> - <% if @polls.any? %> -
- <% @polls.each do |poll| %> - <%= render Dashboard::PollComponent.new(poll) %> - <% end %> -
- <% end %> + <%= render Dashboard::PollsComponent.new(@polls) %>
diff --git a/spec/components/dashboard/poll_component_spec.rb b/spec/components/dashboard/poll_component_spec.rb index 101615e39..f31c7fd15 100644 --- a/spec/components/dashboard/poll_component_spec.rb +++ b/spec/components/dashboard/poll_component_spec.rb @@ -9,27 +9,13 @@ describe Dashboard::PollComponent do describe "Poll card content" do describe "actions visibility" do - it "shows edit link for upcoming polls" do - upcoming = create(:poll, related: proposal, starts_at: 1.week.from_now) - - render_inline Dashboard::PollComponent.new(upcoming) - - page.find("div#poll_#{upcoming.id}") do |poll_card| - expect(poll_card).to have_link "Edit survey", - href: edit_proposal_dashboard_poll_path(proposal, upcoming) - expect(poll_card).not_to have_link "View results" - end - end - it "shows results link for current polls" do current = create(:poll, related: proposal) render_inline Dashboard::PollComponent.new(current) - page.find("div#poll_#{current.id}") do |poll_card| - expect(poll_card).not_to have_link "Edit survey" - expect(poll_card).to have_link "View results", href: results_proposal_poll_path(proposal, current) - end + expect(page).not_to have_link "Edit survey" + expect(page).to have_link "View results", href: results_proposal_poll_path(proposal, current) end it "shows results link for expired polls" do @@ -37,10 +23,17 @@ describe Dashboard::PollComponent do render_inline Dashboard::PollComponent.new(expired) - page.find("div#poll_#{expired.id}") do |poll_card| - expect(poll_card).not_to have_link "Edit survey" - expect(poll_card).to have_link "View results", href: results_proposal_poll_path(proposal, expired) - end + expect(page).not_to have_link "Edit survey" + expect(page).to have_link "View results", href: results_proposal_poll_path(proposal, expired) + end + + it "shows edit link for upcoming polls" do + upcoming = create(:poll, related: proposal, starts_at: 1.week.from_now) + + render_inline Dashboard::PollComponent.new(upcoming) + + expect(page).to have_link "Edit survey", href: edit_proposal_dashboard_poll_path(proposal, upcoming) + expect(page).not_to have_link "View results" end end @@ -49,12 +42,10 @@ describe Dashboard::PollComponent do render_inline Dashboard::PollComponent.new(expired) - page.find("div#poll_#{expired.id}") do |poll_card| - expect(page).to have_content I18n.l(expired.starts_at.to_date) - expect(page).to have_content I18n.l(expired.ends_at.to_date) - expect(page).to have_link expired.title - expect(page).to have_link expired.title, href: proposal_poll_path(proposal, expired) - end + expect(page).to have_content I18n.l(expired.starts_at.to_date) + expect(page).to have_content I18n.l(expired.ends_at.to_date) + expect(page).to have_link expired.title + expect(page).to have_link expired.title, href: proposal_poll_path(proposal, expired) end end end diff --git a/spec/components/dashboard/polls_component_spec.rb b/spec/components/dashboard/polls_component_spec.rb new file mode 100644 index 000000000..2ac73f65a --- /dev/null +++ b/spec/components/dashboard/polls_component_spec.rb @@ -0,0 +1,9 @@ +require "rails_helper" + +describe Dashboard::PollsComponent do + it "is not rendered when there aren't any polls" do + render_inline Dashboard::PollsComponent.new(Poll.none) + + expect(page).not_to be_rendered + end +end