From 5a69ffc61917f9f9b8fbee91259a847a390c4a13 Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 20 Aug 2025 13:10:28 +0200 Subject: [PATCH] Reduce duplicated code and simplify code related with link_to_poll method --- app/components/polls/poll_component.html.erb | 16 +++-------- app/components/polls/poll_component.rb | 28 +++++++++++++++----- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/app/components/polls/poll_component.html.erb b/app/components/polls/poll_component.html.erb index 17002ba0c..917f14366 100644 --- a/app/components/polls/poll_component.html.erb +++ b/app/components/polls/poll_component.html.erb @@ -8,13 +8,9 @@
- <% if poll.questions.one? %> -

<%= link_to_poll poll.questions.first.title, poll %>

-
<%= dates %>
- <% else %> -

<%= link_to_poll poll.name, poll %>

-
<%= dates %>
- +

<%= link_to header_text, path %>

+
<%= dates %>
+ <% if poll.questions.many? %>
- <% 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" %> diff --git a/app/components/polls/poll_component.rb b/app/components/polls/poll_component.rb index af70041d7..4e7d465de 100644 --- a/app/components/polls/poll_component.rb +++ b/app/components/polls/poll_component.rb @@ -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