Rename Poll::Question::Answer to Poll::Question::Option

Having a class named `Poll::Question::Answer` and another class named
`Poll::Answer` was so confusing that no developer working on the project
has ever been capable of remembering which is which for more than a few
seconds.

Furthermore, we're planning to add open answers to polls, and we might
add a reference from the `poll_answers` table to the
`poll_question_answers` table to property differentiate between open
answers and closed answers. Having yet another thing named answer would
be more than what our brains can handle (we know it because we did this
once in a prototype).

So we're renaming `Poll::Question::Answer` to `Poll::Question::Option`.
Hopefully that'll make it easier to remember. The name is also (more or
less) consistent with the `Legislation::QuestionOption` class, which is
similar.

We aren't changing the table or columns names for now in order to avoid
possible issues when upgrading (old code running with the new database
tables/columns after running the migrations but before deployment has
finished, for instance). We might do it in the future.

I've tried not to change the internationalization keys either so
existing translations would still be valid. However, since we have to
change the keys in `activerecord.yml` so methods like
`human_attribute_name` keep working, I'm also changing them in places
where similar keys were used (like `poll_question_answer` or
`poll/question/answer`).

Note that it isn't clear whether we should use `option` or
`question_option` in some cases. In order to keep things simple, we're
using `option` where we were using `answer` and `question_option` where
we were using `question_answer`.

Also note we're adding tests for the admin menu component, since at
first I forgot to change the `answers` reference there and all tests
passed.
This commit is contained in:
Javi Martín
2024-05-13 17:38:43 +02:00
parent 6188281d33
commit 38b38d1fcc
100 changed files with 676 additions and 655 deletions

View File

@@ -19,4 +19,20 @@ describe Admin::MenuComponent, controller: Admin::NewslettersController do
expect(page).to have_css "button[aria-expanded='false']", exact_text: "Settings"
end
describe "#polls_link" do
it "is marked as current when managing poll options",
controller: Admin::Poll::Questions::OptionsController do
render_inline Admin::MenuComponent.new
expect(page).to have_css "[aria-current]", exact_text: "Polls"
end
it "is marked as current when managing poll options content",
controller: Admin::Poll::Questions::Options::VideosController do
render_inline Admin::MenuComponent.new
expect(page).to have_css "[aria-current]", exact_text: "Polls"
end
end
end

View File

@@ -1,18 +1,18 @@
require "rails_helper"
describe Admin::Poll::Questions::Answers::Documents::IndexComponent do
describe Admin::Poll::Questions::Options::Documents::IndexComponent do
before { sign_in(create(:administrator).user) }
let(:future_answer) { create(:poll_question_answer, poll: create(:poll, :future)) }
let(:current_answer) { create(:poll_question_answer, poll: create(:poll)) }
let(:future_answer) { create(:poll_question_option, poll: create(:poll, :future)) }
let(:current_answer) { create(:poll_question_option, poll: create(:poll)) }
it "displays the 'Add new document' link when the poll has not started" do
render_inline Admin::Poll::Questions::Answers::Documents::IndexComponent.new(future_answer)
render_inline Admin::Poll::Questions::Options::Documents::IndexComponent.new(future_answer)
expect(page).to have_link "Add new document"
end
it "does not display the 'Add new document' link when the poll has started" do
render_inline Admin::Poll::Questions::Answers::Documents::IndexComponent.new(current_answer)
render_inline Admin::Poll::Questions::Options::Documents::IndexComponent.new(current_answer)
expect(page).not_to have_link "Add new document"
end

View File

@@ -1,13 +1,13 @@
require "rails_helper"
describe Admin::Poll::Questions::Answers::Documents::TableActionsComponent, :admin do
let(:future_answer) { create(:poll_question_answer, poll: create(:poll, :future)) }
let(:current_answer) { create(:poll_question_answer, poll: create(:poll)) }
describe Admin::Poll::Questions::Options::Documents::TableActionsComponent, :admin do
let(:future_answer) { create(:poll_question_option, poll: create(:poll, :future)) }
let(:current_answer) { create(:poll_question_option, poll: create(:poll)) }
it "displays the destroy action when the poll has not started" do
document = create(:document, documentable: future_answer)
render_inline Admin::Poll::Questions::Answers::Documents::TableActionsComponent.new(document)
render_inline Admin::Poll::Questions::Options::Documents::TableActionsComponent.new(document)
expect(page).to have_link "Download file"
expect(page).to have_button "Delete"
@@ -17,7 +17,7 @@ describe Admin::Poll::Questions::Answers::Documents::TableActionsComponent, :adm
it "does not display the destroy action when the poll has started" do
document = create(:document, documentable: current_answer)
render_inline Admin::Poll::Questions::Answers::Documents::TableActionsComponent.new(document)
render_inline Admin::Poll::Questions::Options::Documents::TableActionsComponent.new(document)
expect(page).to have_link "Download file"
expect(page).not_to have_button "Delete"

View File

@@ -1,19 +1,19 @@
require "rails_helper"
describe Admin::Poll::Questions::Answers::TableActionsComponent, :admin do
describe Admin::Poll::Questions::Options::TableActionsComponent, :admin do
it "displays the edit and destroy actions when the poll has not started" do
answer = create(:poll_question_answer, poll: create(:poll, :future))
option = create(:poll_question_option, poll: create(:poll, :future))
render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer)
render_inline Admin::Poll::Questions::Options::TableActionsComponent.new(option)
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
it "does not display the edit and destroy actions when the poll has started" do
answer = create(:poll_question_answer, poll: create(:poll))
option = create(:poll_question_option, poll: create(:poll))
render_inline Admin::Poll::Questions::Answers::TableActionsComponent.new(answer)
render_inline Admin::Poll::Questions::Options::TableActionsComponent.new(option)
expect(page).not_to have_link "Edit"
expect(page).not_to have_button "Delete"

View File

@@ -1,19 +1,19 @@
require "rails_helper"
describe Admin::Poll::Questions::Answers::Videos::TableActionsComponent, :admin do
describe Admin::Poll::Questions::Options::Videos::TableActionsComponent, :admin do
it "displays the edit and destroy actions when the poll has not started" do
video = create(:poll_answer_video, poll: create(:poll, :future))
video = create(:poll_option_video, poll: create(:poll, :future))
render_inline Admin::Poll::Questions::Answers::Videos::TableActionsComponent.new(video)
render_inline Admin::Poll::Questions::Options::Videos::TableActionsComponent.new(video)
expect(page).to have_link "Edit"
expect(page).to have_button "Delete"
end
it "does not display the edit and destroy actions when the poll has started" do
video = create(:poll_answer_video, poll: create(:poll))
video = create(:poll_option_video, poll: create(:poll))
render_inline Admin::Poll::Questions::Answers::Videos::TableActionsComponent.new(video)
render_inline Admin::Poll::Questions::Options::Videos::TableActionsComponent.new(video)
expect(page).not_to have_link "Edit"
expect(page).not_to have_button "Delete"

View File

@@ -1,12 +1,12 @@
require "rails_helper"
describe Polls::Questions::AnswersComponent do
describe Polls::Questions::OptionsComponent do
include Rails.application.routes.url_helpers
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, :yes_no, poll: poll) }
it "renders answers in given order" do
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect("Yes").to appear_before("No")
end
@@ -14,7 +14,7 @@ describe Polls::Questions::AnswersComponent do
it "renders buttons to vote question answers" do
sign_in(create(:user, :verified))
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_button "Yes"
expect(page).to have_button "No"
@@ -26,7 +26,7 @@ describe Polls::Questions::AnswersComponent do
create(:poll_answer, author: user, question: question, answer: "Yes")
sign_in(user)
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_button "You have voted Yes"
expect(page).to have_button "Vote No"
@@ -40,7 +40,7 @@ describe Polls::Questions::AnswersComponent do
create(:poll_answer, author: user, question: question, answer: "Answer C")
sign_in(user)
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_button "You have voted Answer A"
expect(page).to have_button "Vote Answer B", disabled: true
@@ -48,7 +48,7 @@ describe Polls::Questions::AnswersComponent do
end
it "when user is not signed in, renders answers links pointing to user sign in path" do
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_link "Yes", href: new_user_session_path
expect(page).to have_link "No", href: new_user_session_path
@@ -57,7 +57,7 @@ describe Polls::Questions::AnswersComponent do
it "when user is not verified, renders answers links pointing to user verification in path" do
sign_in(create(:user))
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_link "Yes", href: verification_path
expect(page).to have_link "No", href: verification_path
@@ -68,7 +68,7 @@ describe Polls::Questions::AnswersComponent do
create(:poll_voter, :from_booth, poll: poll, user: user)
sign_in(user)
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_css "span.disabled", text: "Yes"
expect(page).to have_css "span.disabled", text: "No"
@@ -78,7 +78,7 @@ describe Polls::Questions::AnswersComponent do
question = create(:poll_question, :yes_no, poll: create(:poll, :expired))
sign_in(create(:user, :level_two))
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_css "span.disabled", text: "Yes"
expect(page).to have_css "span.disabled", text: "No"
@@ -93,7 +93,7 @@ describe Polls::Questions::AnswersComponent do
poll.geozones << geozone
sign_in(create(:user, :level_two))
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_css "span.disabled", text: "Yes"
expect(page).to have_css "span.disabled", text: "No"
@@ -103,7 +103,7 @@ describe Polls::Questions::AnswersComponent do
poll.geozones << geozone
sign_in(create(:user, :level_two, geozone: geozone))
render_inline Polls::Questions::AnswersComponent.new(question)
render_inline Polls::Questions::OptionsComponent.new(question)
expect(page).to have_button "Yes"
expect(page).to have_button "No"

View File

@@ -3,9 +3,9 @@ require "rails_helper"
describe Polls::Questions::QuestionComponent do
it "renders more information links when any question answer has additional information" do
question = create(:poll_question)
answer_a = create(:poll_question_answer, question: question, title: "Answer A")
answer_b = create(:poll_question_answer, question: question, title: "Answer B")
allow_any_instance_of(Poll::Question::Answer).to receive(:with_read_more?).and_return(true)
answer_a = create(:poll_question_option, question: question, title: "Answer A")
answer_b = create(:poll_question_option, question: question, title: "Answer B")
allow_any_instance_of(Poll::Question::Option).to receive(:with_read_more?).and_return(true)
render_inline Polls::Questions::QuestionComponent.new(question: question)

View File

@@ -4,51 +4,51 @@ describe Polls::Questions::ReadMoreComponent do
include Rails.application.routes.url_helpers
let(:poll) { create(:poll) }
let(:question) { create(:poll_question, poll: poll, title: "Question title?") }
let(:answer) { create(:poll_question_answer, question: question) }
let(:option) { create(:poll_question_option, question: question) }
it "renders question title" do
create(:poll_question_answer, question: question, description: "Question answer description")
create(:poll_question_option, question: question, description: "Question option description")
render_inline Polls::Questions::ReadMoreComponent.new(question: question)
expect(page).to have_content "Question title?"
end
it "renders answers in the given order" do
create(:poll_question_answer, title: "Answer A", question: question, given_order: 2)
create(:poll_question_answer, title: "Answer B", question: question, given_order: 1)
it "renders options in the given order" do
create(:poll_question_option, title: "Answer A", question: question, given_order: 2)
create(:poll_question_option, title: "Answer B", question: question, given_order: 1)
render_inline Polls::Questions::ReadMoreComponent.new(question: question)
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)
it "does not render when options does not have more information" do
option.update!(description: nil)
render_inline Polls::Questions::ReadMoreComponent.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")
it "renders options with videos" do
create(:poll_option_video, option: option, title: "Awesome video", url: "youtube.com/watch?v=123")
render_inline Polls::Questions::ReadMoreComponent.new(question: question)
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")
it "renders options with images" do
create(:image, imageable: option, title: "The yes movement")
render_inline Polls::Questions::ReadMoreComponent.new(question: question)
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")
it "renders options with documents" do
create(:document, documentable: option, title: "The yes movement")
render_inline Polls::Questions::ReadMoreComponent.new(question: question)