From 8311e7e6b843cfadaa64d0e632eb0e98cfa0dd0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:32:18 +0200 Subject: [PATCH] Extract component to render answers additional information --- .../read_more_answer_component.html.erb | 60 ++++++++++++++++++ .../questions/read_more_answer_component.rb | 9 +++ app/views/polls/show.html.erb | 63 +------------------ .../read_more_answer_component_spec.rb | 32 ++++++++++ spec/system/polls/polls_spec.rb | 17 ----- 5 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 app/components/polls/questions/read_more_answer_component.html.erb create mode 100644 app/components/polls/questions/read_more_answer_component.rb create mode 100644 spec/components/polls/questions/read_more_answer_component_spec.rb diff --git a/app/components/polls/questions/read_more_answer_component.html.erb b/app/components/polls/questions/read_more_answer_component.html.erb new file mode 100644 index 000000000..becece5a7 --- /dev/null +++ b/app/components/polls/questions/read_more_answer_component.html.erb @@ -0,0 +1,60 @@ +
" id="answer_<%= answer.id %>"> +

<%= answer.title %>

+ + <% if answer.images.any? %> + <%= render "polls/gallery", answer: answer %> + <% end %> + + <% if answer.description.present? %> +
+
+ <%= wysiwyg(answer.description) %> +
+
+ + +
+
+ <% end %> + + <% if answer.documents.present? %> + + <% end %> + + <% if answer.videos.present? %> + + <% end %> +
diff --git a/app/components/polls/questions/read_more_answer_component.rb b/app/components/polls/questions/read_more_answer_component.rb new file mode 100644 index 000000000..edb34180c --- /dev/null +++ b/app/components/polls/questions/read_more_answer_component.rb @@ -0,0 +1,9 @@ +class Polls::Questions::ReadMoreAnswerComponent < ApplicationComponent + with_collection_parameter :answer + attr_reader :answer + delegate :wysiwyg, to: :helpers + + def initialize(answer:) + @answer = answer + end +end diff --git a/app/views/polls/show.html.erb b/app/views/polls/show.html.erb index 29904041c..906595616 100644 --- a/app/views/polls/show.html.erb +++ b/app/views/polls/show.html.erb @@ -46,68 +46,7 @@
- <% @poll_questions_answers.each do |answer| %> -
" id="answer_<%= answer.id %>"> -

<%= answer.title %>

- - <% if answer.images.any? %> - <%= render "gallery", answer: answer %> - <% end %> - - <% if answer.description.present? %> -
-
- <%= wysiwyg(answer.description) %> -
-
- - -
-
- <% end %> - - <% if answer.documents.present? %> - - <% end %> - - <% if answer.videos.present? %> - - <% end %> -
- <% end %> + <%= render Polls::Questions::ReadMoreAnswerComponent.with_collection(@poll_questions_answers) %>
diff --git a/spec/components/polls/questions/read_more_answer_component_spec.rb b/spec/components/polls/questions/read_more_answer_component_spec.rb new file mode 100644 index 000000000..4e4902a81 --- /dev/null +++ b/spec/components/polls/questions/read_more_answer_component_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +describe Polls::Questions::ReadMoreAnswerComponent do + include Rails.application.routes.url_helpers + let(:poll) { create(:poll) } + let(:question) { create(:poll_question, poll: poll) } + let(:answer) { create(:poll_question_answer, question: question) } + + it "renders answers with videos" do + create(:poll_answer_video, answer: answer, title: "Awesome video", url: "youtube.com/watch?v=123") + + render_inline Polls::Questions::ReadMoreAnswerComponent.new(answer: answer) + + expect(page).to have_link("Awesome video", href: "youtube.com/watch?v=123") + end + + it "renders answers with images" do + create(:image, imageable: answer, title: "The yes movement") + + render_inline Polls::Questions::ReadMoreAnswerComponent.new(answer: answer) + + expect(page).to have_css "img[alt='The yes movement']" + end + + it "renders answers with documents" do + create(:document, documentable: answer, title: "The yes movement") + + render_inline Polls::Questions::ReadMoreAnswerComponent.new(answer: answer) + + expect(page).to have_link("The yes movement") + end +end diff --git a/spec/system/polls/polls_spec.rb b/spec/system/polls/polls_spec.rb index 220d832da..78cfae471 100644 --- a/spec/system/polls/polls_spec.rb +++ b/spec/system/polls/polls_spec.rb @@ -152,14 +152,6 @@ describe "Polls" do expect(page).to have_current_path(poll_path(poll.slug)) end - scenario "Show answers with videos" do - create(:poll_answer_video, poll: poll, title: "Awesome video", url: "youtube.com/watch?v=123") - - visit poll_path(poll) - - expect(page).to have_link("Awesome video", href: "youtube.com/watch?v=123") - end - scenario "Lists questions from proposals as well as regular ones" do normal_question = create(:poll_question, poll: poll) proposal_question = create(:poll_question, poll: poll, proposal: create(:proposal)) @@ -203,15 +195,6 @@ describe "Polls" do end end - scenario "Answer images are shown" do - question = create(:poll_question, :yes_no, poll: poll) - create(:image, imageable: question.question_answers.first, title: "The yes movement") - - visit poll_path(poll) - - expect(page).to have_css "img[alt='The yes movement']" - end - scenario "Buttons to slide through images work back and forth" do question = create(:poll_question, :yes_no, poll: poll) create(:image, imageable: question.question_answers.last, title: "The no movement")