Reduce duplicated code and simplify code related with link_to_poll method

This commit is contained in:
taitus
2025-08-20 13:10:28 +02:00
parent 5944bb85c5
commit 5a69ffc619
2 changed files with 26 additions and 18 deletions

View File

@@ -8,13 +8,9 @@
</div> </div>
<div class="poll-info"> <div class="poll-info">
<% if poll.questions.one? %> <h4><%= link_to header_text, path %></h4>
<h4><%= link_to_poll poll.questions.first.title, poll %></h4> <div class="dates"><%= dates %></div>
<div class="dates"><%= dates %></div> <% if poll.questions.many? %>
<% else %>
<h4><%= link_to_poll poll.name, poll %></h4>
<div class="dates"><%= dates %></div>
<ul class="margin-top"> <ul class="margin-top">
<% poll.questions.sort_for_list.each do |question| %> <% poll.questions.sort_for_list.each do |question| %>
<li><%= question.title %></li> <li><%= question.title %></li>
@@ -25,9 +21,5 @@
<%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %> <%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %>
</div> </div>
<% if poll.expired? %> <%= link_to link_text, path, class: "button hollow expanded" %>
<%= link_to_poll t("polls.index.participate_button_expired"), poll, class: "button hollow expanded" %>
<% else %>
<%= link_to_poll t("polls.index.participate_button"), poll, class: "button hollow expanded" %>
<% end %>
</div> </div>

View File

@@ -12,13 +12,29 @@ class Polls::PollComponent < ApplicationComponent
t("polls.dates", open_at: l(poll.starts_at.to_date), closed_at: l(poll.ends_at.to_date)) t("polls.dates", open_at: l(poll.starts_at.to_date), closed_at: l(poll.ends_at.to_date))
end end
def link_to_poll(text, poll, options = {}) def header_text
if can?(:results, poll) if poll.questions.one?
link_to text, results_poll_path(id: poll.slug || poll.id), options poll.questions.first.title
elsif can?(:stats, poll)
link_to text, stats_poll_path(id: poll.slug || poll.id), options
else else
link_to text, poll_path(id: poll.slug || poll.id), options poll.name
end
end
def link_text
if poll.expired?
t("polls.index.participate_button_expired")
else
t("polls.index.participate_button")
end
end
def path
if can?(:results, poll)
results_poll_path(id: poll.slug || poll.id)
elsif can?(:stats, poll)
stats_poll_path(id: poll.slug || poll.id)
else
poll_path(id: poll.slug || poll.id)
end end
end end
end end