Do not show answers without additional information

This commit is contained in:
decabeza
2022-09-01 12:29:26 +02:00
committed by Senén Rodero Rodríguez
parent d70a0bf1d1
commit 4fba76f64f
6 changed files with 53 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
<h2><%= question.title %></h2> <h2><%= question.title %></h2>
<% question.question_answers.each do |answer| %> <% question.answers_with_read_more.each do |answer| %>
<div class="small-12 medium-6 column end answer <%= cycle("first", "") %>" id="answer_<%= answer.id %>"> <div class="small-12 medium-6 column end answer <%= cycle("first", "") %>" id="answer_<%= answer.id %>">
<h3><%= answer.title %></h3> <h3><%= answer.title %></h3>

View File

@@ -6,4 +6,8 @@ class Polls::Questions::ReadMoreAnswerComponent < ApplicationComponent
def initialize(question:) def initialize(question:)
@question = question @question = question
end end
def render?
question.answers_with_read_more?
end
end end

View File

@@ -74,4 +74,12 @@ class Poll::Question < ApplicationRecord
def possible_answers def possible_answers
question_answers.joins(:translations).pluck("poll_question_answer_translations.title") question_answers.joins(:translations).pluck("poll_question_answer_translations.title")
end 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 end

View File

@@ -40,4 +40,8 @@ class Poll::Question::Answer < ApplicationRecord
def total_votes_percentage def total_votes_percentage
question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes question.answers_total_votes.zero? ? 0 : (total_votes * 100.0) / question.answers_total_votes
end end
def with_read_more?
description.present? || images.any? || documents.any? || videos.any?
end
end end

View File

@@ -7,6 +7,8 @@ describe Polls::Questions::ReadMoreAnswerComponent do
let(:answer) { create(:poll_question_answer, question: question) } let(:answer) { create(:poll_question_answer, question: question) }
it "renders question title" do it "renders question title" do
create(:poll_question_answer, question: question, description: "Question answer description")
render_inline Polls::Questions::ReadMoreAnswerComponent.new(question: question) render_inline Polls::Questions::ReadMoreAnswerComponent.new(question: question)
expect(page).to have_content "Question title?" expect(page).to have_content "Question title?"
@@ -21,6 +23,14 @@ describe Polls::Questions::ReadMoreAnswerComponent do
expect("Answer B").to appear_before("Answer A") expect("Answer B").to appear_before("Answer A")
end 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 it "renders answers with videos" do
create(:poll_answer_video, answer: answer, title: "Awesome video", url: "youtube.com/watch?v=123") create(:poll_answer_video, answer: answer, title: "Awesome video", url: "youtube.com/watch?v=123")

View File

@@ -34,4 +34,30 @@ describe Poll::Question::Answer do
expect(Poll::Question::Answer.with_content).to be_empty expect(Poll::Question::Answer.with_content).to be_empty
end end
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 end