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 class="poll-info">
<% if poll.questions.one? %>
<h4><%= link_to_poll poll.questions.first.title, poll %></h4>
<div class="dates"><%= dates %></div>
<% else %>
<h4><%= link_to_poll poll.name, poll %></h4>
<div class="dates"><%= dates %></div>
<h4><%= link_to header_text, path %></h4>
<div class="dates"><%= dates %></div>
<% if poll.questions.many? %>
<ul class="margin-top">
<% poll.questions.sort_for_list.each do |question| %>
<li><%= question.title %></li>
@@ -25,9 +21,5 @@
<%= render SDG::TagListComponent.new(poll, limit: 5, linkable: false) %>
</div>
<% if poll.expired? %>
<%= 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 %>
<%= link_to link_text, path, class: "button hollow expanded" %>
</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))
end
def link_to_poll(text, poll, options = {})
if can?(:results, poll)
link_to text, results_poll_path(id: poll.slug || poll.id), options
elsif can?(:stats, poll)
link_to text, stats_poll_path(id: poll.slug || poll.id), options
def header_text
if poll.questions.one?
poll.questions.first.title
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