From 6d26ce57cb8d3acaef29e06ae6f22c7fe16ff81e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 5 Sep 2021 02:14:22 +0200 Subject: [PATCH] Show answers with attachments in additional info We weren't showing the details of answers without a description, even if they had images, videos or documents. Some users found that behavior unexpected since the description isn't a mandatory field and so they left it blank, but they added images to that answer and they didn't appear on the poll page. Note we had a condition not to show the title of an answer when it had no description. I think that condition was redundant because answers without a description weren't loaded in the first place. Anyway, that condition doesn't make sense anymore because we're displaying answers with images but no description. --- app/controllers/polls_controller.rb | 2 +- app/models/poll/question/answer.rb | 8 ++++++ app/views/polls/show.html.erb | 4 +-- spec/factories/polls.rb | 12 +++++++++ spec/models/poll/question/answer_spec.rb | 32 ++++++++++++++++++++++++ spec/system/polls/questions_spec.rb | 13 ++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 6665d5db3..c04fab13b 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -22,7 +22,7 @@ class PollsController < ApplicationController @questions = @poll.questions.for_render.sort_for_list @token = poll_voter_token(@poll, current_user) @poll_questions_answers = Poll::Question::Answer.where(question: @poll.questions) - .where.not(description: "").order(:given_order) + .with_content.order(:given_order) @answers_by_question_id = {} poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user&.id) diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 1141cfc4b..ea251c180 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -14,6 +14,14 @@ class Poll::Question::Answer < ApplicationRecord validates_translation :title, presence: true validates :given_order, presence: true, uniqueness: { scope: :question_id } + scope :with_content, -> { where.not(id: without_content) } + scope :without_content, -> do + where(description: "") + .left_joins(:images).where(images: { id: nil }) + .left_joins(:documents).where(documents: { id: nil }) + .left_joins(:videos).where(poll_question_answer_videos: { id: nil }) + end + def self.order_answers(ordered_array) ordered_array.each_with_index do |answer_id, order| find(answer_id).update_column(:given_order, (order + 1)) diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 02db5eda8..ebc55daa7 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -50,9 +50,7 @@
<% @poll_questions_answers.each do |answer| %>
" id="answer_<%= answer.id %>"> - <% if answer.description.present? %> -

<%= answer.title %>

- <% end %> +

<%= answer.title %>

<% if answer.images.any? %> <%= render "gallery", answer: answer %> diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index 2d07fcb2c..fe98957cc 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -73,6 +73,18 @@ FactoryBot.define do transient { poll { association(:poll) } } question { association(:poll_question, poll: poll) } + + trait :with_image do + after(:create) { |answer| create(:image, imageable: answer) } + end + + trait :with_document do + after(:create) { |answer| create(:document, documentable: answer) } + end + + trait :with_video do + after(:create) { |answer| create(:poll_answer_video, answer: answer) } + end end factory :poll_answer_video, class: "Poll::Question::Answer::Video" do diff --git a/spec/models/poll/question/answer_spec.rb b/spec/models/poll/question/answer_spec.rb index e1af0365c..2d5b64482 100644 --- a/spec/models/poll/question/answer_spec.rb +++ b/spec/models/poll/question/answer_spec.rb @@ -2,4 +2,36 @@ require "rails_helper" describe Poll::Question::Answer do it_behaves_like "globalizable", :poll_question_answer + + describe "#with_content" do + it "returns answers with a description" do + answer = create(:poll_question_answer, description: "I've got a description") + + expect(Poll::Question::Answer.with_content).to eq [answer] + end + + it "returns answers with images and no description" do + answer = create(:poll_question_answer, :with_image, description: "") + + expect(Poll::Question::Answer.with_content).to eq [answer] + end + + it "returns answers with documents and no description" do + question = create(:poll_question_answer, :with_document, description: "") + + expect(Poll::Question::Answer.with_content).to eq [question] + end + + it "returns answers with videos and no description" do + question = create(:poll_question_answer, :with_video, description: "") + + expect(Poll::Question::Answer.with_content).to eq [question] + end + + it "does not return answers with no description and no images, documents nor videos" do + create(:poll_question_answer, description: "") + + expect(Poll::Question::Answer.with_content).to be_empty + end + end end diff --git a/spec/system/polls/questions_spec.rb b/spec/system/polls/questions_spec.rb index f61b0ec7c..e100b77b5 100644 --- a/spec/system/polls/questions_spec.rb +++ b/spec/system/polls/questions_spec.rb @@ -10,4 +10,17 @@ describe "Poll Questions" do expect(proposal_question.title).to appear_before(normal_question.title) end + + scenario "shows answers with an image and no description" do + poll = create(:poll) + answer = create(:poll_question_answer, poll: poll, title: "Pedestrian road", description: "") + create(:image, imageable: answer, title: "Trees on both sides of the road") + + visit poll_path(poll) + + within "#poll_more_info_answers" do + expect(page).to have_content "Pedestrian road" + expect(page).to have_selector "img[alt='Trees on both sides of the road']" + end + end end