diff --git a/app/components/polls/access_status_component.html.erb b/app/components/polls/access_status_component.html.erb index 046094db9..d82f5d669 100644 --- a/app/components/polls/access_status_component.html.erb +++ b/app/components/polls/access_status_component.html.erb @@ -1,19 +1,3 @@ -<% if current_user %> - <% if current_user.unverified? %> -
"> - <%= t("polls.index.unverified") %> -
- <% elsif cannot?(:answer, poll) %> -
"> - <%= t("polls.index.cant_answer") %> -
- <% elsif !poll.votable_by?(current_user) %> -
"> - <%= t("polls.index.already_answer") %> -
- <% end %> -<% else %> -
"> - <%= t("polls.index.not_logged_in") %> -
-<% end %> +
+ <%= text %> +
diff --git a/app/components/polls/access_status_component.rb b/app/components/polls/access_status_component.rb index faf10512e..f93c38a97 100644 --- a/app/components/polls/access_status_component.rb +++ b/app/components/polls/access_status_component.rb @@ -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 diff --git a/spec/components/polls/access_status_component_spec.rb b/spec/components/polls/access_status_component_spec.rb index 405f06049..9ac1b77cc 100644 --- a/spec/components/polls/access_status_component_spec.rb +++ b/spec/components/polls/access_status_component_spec.rb @@ -37,4 +37,12 @@ describe Polls::AccessStatusComponent do expect(page).to have_css ".already-answer", count: 1 expect(page).to have_content "You already have participated in this poll" end + + it "is not rendered when users can vote" do + sign_in(create(:user, :level_two)) + + render_inline Polls::AccessStatusComponent.new(create(:poll)) + + expect(page).not_to be_rendered + end end