From eef9f584100b5e9f1d9d9dfdd74e0e5c94e66f83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 7 Jun 2024 16:16:59 +0200 Subject: [PATCH] Extract component to render poll geozones This way we remove a bit of duplication. These changes also affect the way geozones are rendered in a couple of minor ways, making them more consistent: * No empty list of geozones is rendered when there are no geozones (before these changes, an empty list was rendered in the index action but not in the show action) * The text clarifying the geozone restriction is always shown (before these changes, it was shown in the index action but not in the show action) We've added tests for these cases. --- .../polls/geozones_component.html.erb | 9 +++++++++ app/components/polls/geozones_component.rb | 11 +++++++++++ app/components/polls/poll_component.html.erb | 11 +---------- .../polls/poll_header_component.html.erb | 8 +------- spec/components/polls/poll_component_spec.rb | 14 ++++++++++++++ .../polls/poll_header_component_spec.rb | 17 +++++++++++++++++ 6 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 app/components/polls/geozones_component.html.erb create mode 100644 app/components/polls/geozones_component.rb create mode 100644 spec/components/polls/poll_header_component_spec.rb diff --git a/app/components/polls/geozones_component.html.erb b/app/components/polls/geozones_component.html.erb new file mode 100644 index 000000000..09785642b --- /dev/null +++ b/app/components/polls/geozones_component.html.erb @@ -0,0 +1,9 @@ +

+ <%= t("polls.index.geozone_info") %> +

+ + diff --git a/app/components/polls/geozones_component.rb b/app/components/polls/geozones_component.rb new file mode 100644 index 000000000..75e982c0b --- /dev/null +++ b/app/components/polls/geozones_component.rb @@ -0,0 +1,11 @@ +class Polls::GeozonesComponent < ApplicationComponent + attr_reader :poll + + def initialize(poll) + @poll = poll + end + + def render? + poll.geozones.any? + end +end diff --git a/app/components/polls/poll_component.html.erb b/app/components/polls/poll_component.html.erb index 24594fc7e..110bef076 100644 --- a/app/components/polls/poll_component.html.erb +++ b/app/components/polls/poll_component.html.erb @@ -40,16 +40,7 @@ <% end %> <% end %> - <% if poll.geozones.any? %> -

- <%= t("polls.index.geozone_info") %> -

- <% end %> - + <%= render Polls::GeozonesComponent.new(poll) %> <%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %>
diff --git a/app/components/polls/poll_header_component.html.erb b/app/components/polls/poll_header_component.html.erb index c4b2e6cad..6058b9bcf 100644 --- a/app/components/polls/poll_header_component.html.erb +++ b/app/components/polls/poll_header_component.html.erb @@ -11,13 +11,7 @@ <%= auto_link_already_sanitized_html simple_format(poll.summary) %> - <% if poll.geozones.any? %> - - <% end %> + <%= render Polls::GeozonesComponent.new(poll) %> <%= render SDG::TagListComponent.new(poll, linkable: false) %>
diff --git a/spec/components/polls/poll_component_spec.rb b/spec/components/polls/poll_component_spec.rb index 9cd5ea50a..a12041ff8 100644 --- a/spec/components/polls/poll_component_spec.rb +++ b/spec/components/polls/poll_component_spec.rb @@ -63,6 +63,20 @@ describe Polls::PollComponent do end end + describe "geozones" do + it "renders a list of geozones when the poll is geozone-restricted" do + render_inline Polls::PollComponent.new(create(:poll, geozone_restricted_to: [create(:geozone)])) + + expect(page).to have_css ".tags" + end + + it "does not render a list of geozones when the poll isn't geozone-restricted" do + render_inline Polls::PollComponent.new(create(:poll)) + + expect(page).not_to have_css ".tags" + end + end + it "shows a link to poll stats if enabled" do poll = create(:poll, :expired, name: "Poll with stats", stats_enabled: true) diff --git a/spec/components/polls/poll_header_component_spec.rb b/spec/components/polls/poll_header_component_spec.rb new file mode 100644 index 000000000..6062a62ec --- /dev/null +++ b/spec/components/polls/poll_header_component_spec.rb @@ -0,0 +1,17 @@ +require "rails_helper" + +describe Polls::PollHeaderComponent do + describe "geozones" do + it "shows a text when the poll is geozone-restricted" do + render_inline Polls::PollHeaderComponent.new(create(:poll, geozone_restricted_to: [create(:geozone)])) + + expect(page).to have_content "Only residents in the following areas can participate" + end + + it "does not show the text when the poll isn't geozone-restricted" do + render_inline Polls::PollHeaderComponent.new(create(:poll)) + + expect(page).not_to have_content "Only residents in the following areas can participate" + end + end +end