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.
This commit is contained in:
Javi Martín
2024-06-07 16:16:59 +02:00
parent ae026f0f6f
commit eef9f58410
6 changed files with 53 additions and 17 deletions

View File

@@ -0,0 +1,9 @@
<p>
<small><%= t("polls.index.geozone_info") %></small>
</p>
<ul class="tags">
<% poll.geozones.each do |geozone| %>
<li><span><%= geozone.name %></span></li>
<% end %>
</ul>

View File

@@ -0,0 +1,11 @@
class Polls::GeozonesComponent < ApplicationComponent
attr_reader :poll
def initialize(poll)
@poll = poll
end
def render?
poll.geozones.any?
end
end

View File

@@ -40,16 +40,7 @@
<% end %> <% end %>
</ul> </ul>
<% end %> <% end %>
<% if poll.geozones.any? %> <%= render Polls::GeozonesComponent.new(poll) %>
<p>
<small><%= t("polls.index.geozone_info") %></small>
</p>
<% end %>
<ul class="tags">
<% poll.geozones.each do |g| %>
<li><span><%= g.name %></span></li>
<% end %>
</ul>
<%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %> <%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %>
</div> </div>
<div class="small-12 medium-3 column table" data-equalizer-watch> <div class="small-12 medium-3 column table" data-equalizer-watch>

View File

@@ -11,13 +11,7 @@
<%= auto_link_already_sanitized_html simple_format(poll.summary) %> <%= auto_link_already_sanitized_html simple_format(poll.summary) %>
<% if poll.geozones.any? %> <%= render Polls::GeozonesComponent.new(poll) %>
<ul class="margin-top tags">
<% poll.geozones.each do |g| %>
<li><span><%= g.name %></span></li>
<% end %>
</ul>
<% end %>
<%= render SDG::TagListComponent.new(poll, linkable: false) %> <%= render SDG::TagListComponent.new(poll, linkable: false) %>
</div> </div>

View File

@@ -63,6 +63,20 @@ describe Polls::PollComponent do
end end
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 it "shows a link to poll stats if enabled" do
poll = create(:poll, :expired, name: "Poll with stats", stats_enabled: true) poll = create(:poll, :expired, name: "Poll with stats", stats_enabled: true)

View File

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