Extract methods in poll status component
This way we remove duplication in the HTML. We're also adding a test checking what happens when users can vote in order to test the `render?` method we've added.
This commit is contained in:
@@ -1,19 +1,3 @@
|
||||
<% if current_user %>
|
||||
<% if current_user.unverified? %>
|
||||
<div class="access-status unverified" title="<%= t("polls.index.unverified") %>">
|
||||
<span class="show-for-sr"><%= t("polls.index.unverified") %></span>
|
||||
</div>
|
||||
<% elsif cannot?(:answer, poll) %>
|
||||
<div class="access-status cant-answer" title="<%= t("polls.index.cant_answer") %>">
|
||||
<span class="show-for-sr"><%= t("polls.index.cant_answer") %></span>
|
||||
</div>
|
||||
<% elsif !poll.votable_by?(current_user) %>
|
||||
<div class="access-status already-answer" title="<%= t("polls.index.already_answer") %>">
|
||||
<span class="show-for-sr"><%= t("polls.index.already_answer") %></span>
|
||||
</div>
|
||||
<% end %>
|
||||
<% else %>
|
||||
<div class="access-status not-logged-in" title="<%= t("polls.index.not_logged_in") %>">
|
||||
<span class="show-for-sr"><%= t("polls.index.not_logged_in") %></span>
|
||||
</div>
|
||||
<% end %>
|
||||
<div class="access-status <%= html_class %>" title="<%= text %>">
|
||||
<span class="show-for-sr"><%= text %></span>
|
||||
</div>
|
||||
|
||||
@@ -5,4 +5,32 @@ class Polls::AccessStatusComponent < ApplicationComponent
|
||||
def initialize(poll)
|
||||
@poll = poll
|
||||
end
|
||||
|
||||
def render?
|
||||
attributes.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def text
|
||||
attributes[:text]
|
||||
end
|
||||
|
||||
def html_class
|
||||
attributes[:class]
|
||||
end
|
||||
|
||||
def attributes
|
||||
if current_user
|
||||
if current_user.unverified?
|
||||
{ text: t("polls.index.unverified"), class: "unverified" }
|
||||
elsif cannot?(:answer, poll)
|
||||
{ text: t("polls.index.cant_answer"), class: "cant-answer" }
|
||||
elsif !poll.votable_by?(current_user)
|
||||
{ text: t("polls.index.already_answer"), class: "already-answer" }
|
||||
end
|
||||
else
|
||||
{ text: t("polls.index.not_logged_in"), class: "not-logged-in" }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user