Extract methods in poll callout component

This commit is contained in:
Javi Martín
2025-07-09 13:57:29 +02:00
parent b48682e3e4
commit 34a1e65ca9
2 changed files with 33 additions and 23 deletions

View File

@@ -1,32 +1,17 @@
<% if can?(:answer, poll) %>
<% if poll.voted_in_booth?(current_user) %>
<div class="callout warning">
<%= t("polls.show.already_voted_in_booth") %>
</div>
<% elsif poll.voted_in_web?(current_user) %>
<div class="callout warning">
<%= t("polls.show.already_voted_in_web") %>
</div>
<% if voted_in_booth? %>
<%= callout(t("polls.show.already_voted_in_booth")) %>
<% elsif voted_in_web? %>
<%= callout(t("polls.show.already_voted_in_web")) %>
<% end %>
<% else %>
<% if current_user.nil? %>
<div class="callout primary">
<%= sanitize(t("polls.show.cant_answer_not_logged_in",
signin: link_to_signin(class: "probe-message"),
signup: link_to_signup(class: "probe-message"))) %>
</div>
<%= callout(not_logged_in_text, html_class: "primary") %>
<% elsif current_user.unverified? %>
<div class="callout warning">
<%= sanitize(t("polls.show.cant_answer_verify",
verify_link: link_to(t("polls.show.verify_link"), verification_path))) %>
</div>
<%= callout(unverified_text) %>
<% elsif poll.expired? %>
<div class="callout alert">
<%= t("polls.show.cant_answer_expired") %>
</div>
<%= callout(t("polls.show.cant_answer_expired"), html_class: "alert") %>
<% else %>
<div class="callout warning">
<%= t("polls.show.cant_answer_wrong_geozone") %>
</div>
<%= callout(t("polls.show.cant_answer_wrong_geozone")) %>
<% end %>
<% end %>

View File

@@ -5,4 +5,29 @@ class Polls::CalloutComponent < ApplicationComponent
def initialize(poll)
@poll = poll
end
private
def voted_in_booth?
poll.voted_in_booth?(current_user)
end
def voted_in_web?
poll.voted_in_web?(current_user)
end
def callout(text, html_class: "warning")
tag.div(text, class: "callout #{html_class}")
end
def not_logged_in_text
sanitize(t("polls.show.cant_answer_not_logged_in",
signin: link_to_signin(class: "probe-message"),
signup: link_to_signup(class: "probe-message")))
end
def unverified_text
sanitize(t("polls.show.cant_answer_verify",
verify_link: link_to(t("polls.show.verify_link"), verification_path)))
end
end