diff --git a/app/components/polls/questions/read_more_answer_component.html.erb b/app/components/polls/questions/read_more_answer_component.html.erb
index 06b374a6d..1810f981a 100644
--- a/app/components/polls/questions/read_more_answer_component.html.erb
+++ b/app/components/polls/questions/read_more_answer_component.html.erb
@@ -1,5 +1,5 @@
<%= question.title %>
-<% question.question_answers.each do |answer| %>
+<% question.answers_with_read_more.each do |answer| %>
" id="answer_<%= answer.id %>">
<%= answer.title %>
diff --git a/app/components/polls/questions/read_more_answer_component.rb b/app/components/polls/questions/read_more_answer_component.rb
index a88801581..81d868ab4 100644
--- a/app/components/polls/questions/read_more_answer_component.rb
+++ b/app/components/polls/questions/read_more_answer_component.rb
@@ -6,4 +6,8 @@ class Polls::Questions::ReadMoreAnswerComponent < ApplicationComponent
def initialize(question:)
@question = question
end
+
+ def render?
+ question.answers_with_read_more?
+ end
end
diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb
index 6be7aecc9..e513d3298 100644
--- a/app/models/poll/question.rb
+++ b/app/models/poll/question.rb
@@ -74,4 +74,12 @@ class Poll::Question < ApplicationRecord
def possible_answers
question_answers.joins(:translations).pluck("poll_question_answer_translations.title")
end
+
+ def answers_with_read_more?
+ answers_with_read_more.any?
+ end
+
+ def answers_with_read_more
+ question_answers.select(&:with_read_more?)
+ end
end
diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb
index ea251c180..57aba5609 100644
--- a/app/models/poll/question/answer.rb
+++ b/app/models/poll/question/answer.rb
@@ -40,4 +40,8 @@ class Poll::Question::Answer < ApplicationRecord
def total_votes_percentage
question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes
end
+
+ def with_read_more?
+ description.present? || images.any? || documents.any? || videos.any?
+ end
end
diff --git a/spec/components/polls/questions/read_more_answer_component_spec.rb b/spec/components/polls/questions/read_more_answer_component_spec.rb
index 6c441e878..dff77db77 100644
--- a/spec/components/polls/questions/read_more_answer_component_spec.rb
+++ b/spec/components/polls/questions/read_more_answer_component_spec.rb
@@ -7,6 +7,8 @@ describe Polls::Questions::ReadMoreAnswerComponent do
let(:answer) { create(:poll_question_answer, question: question) }
it "renders question title" do
+ create(:poll_question_answer, question: question, description: "Question answer description")
+
render_inline Polls::Questions::ReadMoreAnswerComponent.new(question: question)
expect(page).to have_content "Question title?"
@@ -21,6 +23,14 @@ describe Polls::Questions::ReadMoreAnswerComponent do
expect("Answer B").to appear_before("Answer A")
end
+ it "does not render when answers does not have more information" do
+ answer.update!(description: nil)
+
+ render_inline Polls::Questions::ReadMoreAnswerComponent.new(question: question)
+
+ expect(page).not_to be_rendered
+ end
+
it "renders answers with videos" do
create(:poll_answer_video, answer: answer, title: "Awesome video", url: "youtube.com/watch?v=123")
diff --git a/spec/models/poll/question/answer_spec.rb b/spec/models/poll/question/answer_spec.rb
index 2d5b64482..3ee4ddb1e 100644
--- a/spec/models/poll/question/answer_spec.rb
+++ b/spec/models/poll/question/answer_spec.rb
@@ -34,4 +34,30 @@ describe Poll::Question::Answer do
expect(Poll::Question::Answer.with_content).to be_empty
end
end
+
+ describe "#with_read_more?" do
+ it "returns false when the answer does not have description, images, videos nor documents" do
+ poll_question_answer = build(:poll_question_answer, description: nil)
+
+ expect(poll_question_answer.with_read_more?).to be_falsy
+ end
+
+ it "returns true when the answer has description, images, videos or documents" do
+ poll_question_answer = build(:poll_question_answer, description: "Answer description")
+
+ expect(poll_question_answer.with_read_more?).to be_truthy
+
+ poll_question_answer = build(:poll_question_answer, :with_image)
+
+ expect(poll_question_answer.with_read_more?).to be_truthy
+
+ poll_question_answer = build(:poll_question_answer, :with_document)
+
+ expect(poll_question_answer.with_read_more?).to be_truthy
+
+ poll_question_answer = build(:poll_question_answer, :with_video)
+
+ expect(poll_question_answer.with_read_more?).to be_truthy
+ end
+ end
end