Rename variables describing poll options as answers

Since we've renamed the class to `Option`, having variables, methods and
texts refering to it as `answer` was confusing.
This commit is contained in:
Javi Martín
2024-06-07 17:27:56 +02:00
parent 38b38d1fcc
commit 8997ed316c
28 changed files with 123 additions and 123 deletions

View File

@@ -1,26 +1,26 @@
(function() {
"use strict";
App.Options = {
initializeOptions: function(answers) {
$(answers).on("cocoon:after-insert", function(e, new_answer) {
initializeOptions: function(options) {
$(options).on("cocoon:after-insert", function(e, new_option) {
var given_order;
given_order = App.Options.maxGivenOrder(answers) + 1;
$(new_answer).find("[name$='[given_order]']").val(given_order);
given_order = App.Options.maxGivenOrder(options) + 1;
$(new_option).find("[name$='[given_order]']").val(given_order);
});
},
maxGivenOrder: function(answers) {
maxGivenOrder: function(options) {
var max_order;
max_order = 0;
$(answers).find("[name$='[given_order]']").each(function(index, answer) {
$(options).find("[name$='[given_order]']").each(function(index, option) {
var value;
value = parseFloat($(answer).val());
value = parseFloat($(option).val());
max_order = value > max_order ? value : max_order;
});
return max_order;
},
nestedOptions: function() {
$(".js-answers").each(function(index, answers) {
App.Options.initializeOptions(answers);
$(".js-answers").each(function(index, options) {
App.Options.initializeOptions(options);
});
},
initialize: function() {

View File

@@ -3,20 +3,20 @@
App.Polls = {
initialize: function() {
$(".zoom-link").on("click", function(event) {
var answer;
answer = $(event.target).closest("div.answer");
var option;
option = $(event.target).closest("div.answer");
if ($(answer).hasClass("medium-6")) {
$(answer).removeClass("medium-6");
$(answer).addClass("answer-divider");
if (!$(answer).hasClass("first")) {
$(answer).insertBefore($(answer).prev("div.answer"));
if ($(option).hasClass("medium-6")) {
$(option).removeClass("medium-6");
$(option).addClass("answer-divider");
if (!$(option).hasClass("first")) {
$(option).insertBefore($(option).prev("div.answer"));
}
} else {
$(answer).addClass("medium-6");
$(answer).removeClass("answer-divider");
if (!$(answer).hasClass("first")) {
$(answer).insertAfter($(answer).next("div.answer"));
$(option).addClass("medium-6");
$(option).removeClass("answer-divider");
if (!$(option).hasClass("first")) {
$(option).insertAfter($(option).next("div.answer"));
}
}
});

View File

@@ -89,7 +89,7 @@ class Admin::ActionComponent < ApplicationComponent
end
def default_path
if %i[answers configure destroy preview show].include?(action.to_sym)
if %i[configure destroy options preview show].include?(action.to_sym)
namespaced_polymorphic_path(namespace, record)
else
namespaced_polymorphic_path(namespace, record, { action: action }.merge(request.query_parameters))

View File

@@ -1,3 +1,3 @@
<%= render Admin::AllowedTableActionsComponent.new(question) do |actions| %>
<%= actions.action(:answers, text: t("admin.polls.show.edit_answers")) %>
<%= actions.action(:options, text: t("admin.polls.show.edit_answers")) %>
<% end %>

View File

@@ -16,7 +16,7 @@
title: t("poll_questions.show.vote_answer", answer: question_option.title),
class: "button secondary hollow",
"aria-pressed": false,
disabled: disable_answer?(question_option) %>
disabled: disable_option?(question_option) %>
<% end %>
<% end %>
<% elsif !user_signed_in? %>

View File

@@ -18,7 +18,7 @@ class Polls::Questions::OptionsComponent < ApplicationComponent
user_answers.find_by(answer: question_option.title)
end
def disable_answer?(question_option)
def disable_option?(question_option)
question.multiple? && user_answers.count == question.max_votes
end

View File

@@ -16,7 +16,7 @@
<% if question.options_with_read_more? %>
<div>
<p><%= t("poll_questions.read_more_about") %></p>
<p><%= answers_read_more_links %></p>
<p><%= options_read_more_links %></p>
</div>
<% end %>
</div>

View File

@@ -5,9 +5,9 @@ class Polls::Questions::QuestionComponent < ApplicationComponent
@question = question
end
def answers_read_more_links
safe_join(question.options_with_read_more.map do |answer|
link_to answer.title, "#answer_#{answer.id}"
def options_read_more_links
safe_join(question.options_with_read_more.map do |option|
link_to option.title, "#answer_#{option.id}"
end, ", ")
end
end

View File

@@ -1,52 +1,52 @@
<h2><%= question.title %></h2>
<% question.options_with_read_more.each do |answer| %>
<div class="small-12 medium-6 column end answer <%= cycle("first", "") %>" id="answer_<%= answer.id %>">
<h3><%= answer.title %></h3>
<% question.options_with_read_more.each do |option| %>
<div class="small-12 medium-6 column end answer <%= cycle("first", "") %>" id="answer_<%= option.id %>">
<h3><%= option.title %></h3>
<div class="margin-top">
<% if answer.description.present? %>
<div id="answer_description_<%= answer.id %>" class="answer-description short" data-toggler="short">
<%= wysiwyg(answer.description) %>
<% if option.description.present? %>
<div id="answer_description_<%= option.id %>" class="answer-description short" data-toggler="short">
<%= wysiwyg(option.description) %>
</div>
<div class="read-more">
<button type="button" id="read_more_<%= answer.id %>"
data-toggle="answer_description_<%= answer.id %> read_more_<%= answer.id %> read_less_<%= answer.id %>"
<button type="button" id="read_more_<%= option.id %>"
data-toggle="answer_description_<%= option.id %> read_more_<%= option.id %> read_less_<%= option.id %>"
data-toggler="hide">
<%= t("polls.show.read_more", answer: answer.title) %>
<%= t("polls.show.read_more", answer: option.title) %>
</button>
<button type="button" id="read_less_<%= answer.id %>"
data-toggle="answer_description_<%= answer.id %> read_more_<%= answer.id %> read_less_<%= answer.id %>"
<button type="button" id="read_less_<%= option.id %>"
data-toggle="answer_description_<%= option.id %> read_more_<%= option.id %> read_less_<%= option.id %>"
data-toggler="hide"
class="hide">
<%= t("polls.show.read_less", answer: answer.title) %>
<%= t("polls.show.read_less", answer: option.title) %>
</button>
</div>
<% end %>
<% if answer.images.any? %>
<%= render "polls/gallery", answer: answer %>
<% if option.images.any? %>
<%= render "polls/gallery", option: option %>
<% end %>
<% if answer.documents.present? %>
<% if option.documents.present? %>
<div class="document-link">
<p>
<strong><%= t("polls.show.documents") %></strong>
</p>
<% answer.documents.each do |document| %>
<% option.documents.each do |document| %>
<%= render Documents::DocumentComponent.new(document) %>
<% end %>
</div>
<% end %>
<% if answer.videos.present? %>
<% if option.videos.present? %>
<div class="video-link">
<p>
<span class="icon-video"></span>&nbsp;
<strong><%= t("polls.show.videos") %></strong>
</p>
<% answer.videos.each do |video| %>
<% option.videos.each do |video| %>
<%= link_to video.title,
video.url,
rel: "nofollow" %><br>

View File

@@ -2,22 +2,22 @@
<table id="question_<%= question.id %>_results_table">
<thead>
<tr>
<%- question.question_options.each do |answer| %>
<th scope="col" class="<%= answer_styles(answer) %>">
<% if most_voted_answer?(answer) %>
<%- question.question_options.each do |option| %>
<th scope="col" class="<%= option_styles(option) %>">
<% if most_voted_option?(option) %>
<span class="show-for-sr"><%= t("polls.show.results.most_voted_answer") %></span>
<% end %>
<%= answer.title %>
<%= option.title %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<%- question.question_options.each do |answer| %>
<td id="answer_<%= answer.id %>_result" class="<%= answer_styles(answer) %>">
<%= answer.total_votes %>
(<%= answer.total_votes_percentage.round(2) %>%)
<%- question.question_options.each do |option| %>
<td id="answer_<%= option.id %>_result" class="<%= option_styles(option) %>">
<%= option.total_votes %>
(<%= option.total_votes_percentage.round(2) %>%)
</td>
<% end %>
</tr>

View File

@@ -5,11 +5,11 @@ class Polls::Results::QuestionComponent < ApplicationComponent
@question = question
end
def answer_styles(answer)
"win" if most_voted_answer?(answer)
def option_styles(option)
"win" if most_voted_option?(option)
end
def most_voted_answer?(answer)
answer.id == question.most_voted_option_id
def most_voted_option?(option)
option.id == question.most_voted_option_id
end
end

View File

@@ -8,11 +8,11 @@ module OfficingHelper
options_for_select(options, params[:oa])
end
def answer_result_value(question_id, answer_index)
def answer_result_value(question_id, option_index)
return nil if params.blank?
return nil if params[:questions].blank?
return nil if params[:questions][question_id.to_s].blank?
params[:questions][question_id.to_s][answer_index.to_s]
params[:questions][question_id.to_s][option_index.to_s]
end
end

View File

@@ -26,7 +26,7 @@
</td>
<td>
<%= render Admin::TableActionsComponent.new(question) do |actions| %>
<%= actions.action(:answers, text: t("admin.polls.show.edit_answers")) %>
<%= actions.action(:options, text: t("admin.polls.show.edit_answers")) %>
<% end %>
</td>
</tr>

View File

@@ -71,30 +71,30 @@
</thead>
<tbody class="sortable" data-js-url="<%= admin_question_options_order_options_path(@question.id) %>">
<% @question.question_options.each do |answer| %>
<tr id="<%= dom_id(answer) %>" class="poll_question_answer" data-answer-id="<%= answer.id %>">
<td class="align-top"><%= answer.title %></td>
<td class="align-top break"><%= wysiwyg(answer.description) %></td>
<% @question.question_options.each do |option| %>
<tr id="<%= dom_id(option) %>" class="poll_question_answer" data-answer-id="<%= option.id %>">
<td class="align-top"><%= option.title %></td>
<td class="align-top break"><%= wysiwyg(option.description) %></td>
<td class="align-top text-center">
(<%= answer.images.count %>)
(<%= option.images.count %>)
<br>
<%= link_to t("admin.questions.show.answers.images_list"),
admin_option_images_path(answer) %>
admin_option_images_path(option) %>
</td>
<td class="align-top text-center">
(<%= answer.documents.count rescue 0 %>)
(<%= option.documents.count rescue 0 %>)
<br>
<%= link_to t("admin.questions.show.answers.documents_list"),
admin_option_documents_path(answer) %>
admin_option_documents_path(option) %>
</td>
<td class="align-top text-center">
(<%= answer.videos.count %>)
(<%= option.videos.count %>)
<br>
<%= link_to t("admin.questions.show.answers.video_list"),
admin_option_videos_path(answer) %>
admin_option_videos_path(option) %>
</td>
<td>
<%= render Admin::Poll::Questions::Options::TableActionsComponent.new(answer) %>
<%= render Admin::Poll::Questions::Options::TableActionsComponent.new(option) %>
</td>
</tr>
<% end %>

View File

@@ -13,8 +13,8 @@
</div>
<div class="js-answers">
<%= f.fields_for :question_options do |answer| %>
<%= render "question_option_fields", f: answer %>
<%= f.fields_for :question_options do |option_form| %>
<%= render "question_option_fields", f: option_form %>
<% end %>
<div class="answer-links links row expanded">

View File

@@ -16,9 +16,9 @@
<div class="small-12 column">
<h3><%= question.title %></h3>
</div>
<% question.question_options.each_with_index do |answer, i| %>
<% question.question_options.each_with_index do |option, i| %>
<div class="small-12 medium-6 large-3 column end">
<label><%= answer.title %></label>
<label><%= option.title %></label>
<%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %>
</div>
<% end %>

View File

@@ -1,6 +1,6 @@
<div class="orbit margin-bottom" role="region" aria-label="<%= answer.title %>" data-orbit data-auto-play="false" data-use-m-u-i="false">
<button type="button" data-toggle="answer_<%= answer.id %> zoom_<%= answer.id %>" class="zoom-link hide-for-small-only">
<span id="zoom_<%= answer.id %>" class="icon-search-plus" data-toggler="icon-search-plus icon-search-minus"></span>
<div class="orbit margin-bottom" role="region" aria-label="<%= option.title %>" data-orbit data-auto-play="false" data-use-m-u-i="false">
<button type="button" data-toggle="answer_<%= option.id %> zoom_<%= option.id %>" class="zoom-link hide-for-small-only">
<span id="zoom_<%= option.id %>" class="icon-search-plus" data-toggler="icon-search-plus icon-search-minus"></span>
<span class="show-for-sr"><%= t("polls.show.zoom_plus") %></span>
</button>
@@ -16,7 +16,7 @@
</button>
</li>
<% answer.images.reverse.each_with_index do |image, index| %>
<% option.images.reverse.each_with_index do |image, index| %>
<li class="orbit-slide <%= is_active_class(index) %>">
<%= link_to image.attachment do %>
<%= image_tag image.attachment,
@@ -30,7 +30,7 @@
</ul>
<nav class="orbit-bullets">
<% answer.images.each_with_index do |image, index| %>
<% option.images.each_with_index do |image, index| %>
<button class="<%= is_active_class(index) %>" data-slide="<%= index %>">
<span class="show-for-sr"><%= image.title.unicode_normalize %></span>
</button>

View File

@@ -49,7 +49,7 @@ section "Creating polls" do
end
end
section "Creating Poll Questions & Answers" do
section "Creating Poll Questions & Options" do
Poll.find_each do |poll|
(3..5).to_a.sample.times do
question_title = Faker::Lorem.sentence(word_count: 3).truncate(60) + "?"
@@ -210,12 +210,12 @@ section "Creating Poll Results" do
author = Poll::Officer.first.user
poll.questions.each do |question|
question.question_options.each do |answer|
question.question_options.each do |option|
Poll::PartialResult.create!(officer_assignment: officer_assignment,
booth_assignment: booth_assignment,
date: Date.current,
question: question,
answer: answer.title,
answer: option.title,
author: author,
amount: rand(999),
origin: "booth")

View File

@@ -2,17 +2,17 @@ require "rails_helper"
describe Admin::Poll::Questions::Options::Documents::IndexComponent do
before { sign_in(create(:administrator).user) }
let(:future_answer) { create(:poll_question_option, poll: create(:poll, :future)) }
let(:current_answer) { create(:poll_question_option, poll: create(:poll)) }
let(:future_option) { create(:poll_question_option, poll: create(:poll, :future)) }
let(:current_option) { 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::Options::Documents::IndexComponent.new(future_answer)
render_inline Admin::Poll::Questions::Options::Documents::IndexComponent.new(future_option)
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::Options::Documents::IndexComponent.new(current_answer)
render_inline Admin::Poll::Questions::Options::Documents::IndexComponent.new(current_option)
expect(page).not_to have_link "Add new document"
end

View File

@@ -1,11 +1,11 @@
require "rails_helper"
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)) }
let(:future_option) { create(:poll_question_option, poll: create(:poll, :future)) }
let(:current_option) { 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)
document = create(:document, documentable: future_option)
render_inline Admin::Poll::Questions::Options::Documents::TableActionsComponent.new(document)
@@ -15,7 +15,7 @@ describe Admin::Poll::Questions::Options::Documents::TableActionsComponent, :adm
end
it "does not display the destroy action when the poll has started" do
document = create(:document, documentable: current_answer)
document = create(:document, documentable: current_option)
render_inline Admin::Poll::Questions::Options::Documents::TableActionsComponent.new(document)

View File

@@ -1,18 +1,18 @@
require "rails_helper"
describe Polls::Questions::QuestionComponent do
it "renders more information links when any question answer has additional information" do
it "renders more information links when any question option has additional information" do
question = create(:poll_question)
answer_a = create(:poll_question_option, question: question, title: "Answer A")
answer_b = create(:poll_question_option, question: question, title: "Answer B")
option_a = create(:poll_question_option, question: question, title: "Answer A")
option_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)
poll_question = page.find("#poll_question_#{question.id}")
expect(poll_question).to have_content("Read more about")
expect(poll_question).to have_link("Answer A", href: "#answer_#{answer_a.id}")
expect(poll_question).to have_link("Answer B", href: "#answer_#{answer_b.id}")
expect(poll_question).to have_link("Answer A", href: "#answer_#{option_a.id}")
expect(poll_question).to have_link("Answer B", href: "#answer_#{option_b.id}")
expect(poll_question).to have_content("Answer A, Answer B")
end
end

View File

@@ -2,12 +2,12 @@ require "rails_helper"
describe DocumentsController do
describe "DELETE destroy" do
context "Poll answers administration", :admin do
let(:current_answer) { create(:poll_question_option, poll: create(:poll)) }
let(:future_answer) { create(:poll_question_option, poll: create(:poll, :future)) }
context "Poll options administration", :admin do
let(:current_option) { create(:poll_question_option, poll: create(:poll)) }
let(:future_option) { create(:poll_question_option, poll: create(:poll, :future)) }
it "is not possible for an already started poll" do
document = create(:document, documentable: current_answer)
document = create(:document, documentable: current_option)
delete :destroy, params: { id: document }
expect(flash[:alert]).to eq "You do not have permission to " \
@@ -16,11 +16,11 @@ describe DocumentsController do
end
it "is possible for a not started poll" do
document = create(:document, documentable: future_answer)
request.env["HTTP_REFERER"] = admin_option_documents_path(future_answer)
document = create(:document, documentable: future_option)
request.env["HTTP_REFERER"] = admin_option_documents_path(future_option)
delete :destroy, params: { id: document }
expect(response).to redirect_to admin_option_documents_path(future_answer)
expect(response).to redirect_to admin_option_documents_path(future_option)
expect(flash[:notice]).to eq "Document was deleted successfully."
expect(Document.count).to eq 0
end

View File

@@ -114,7 +114,7 @@ describe "Polymorphic routes" do
expect(admin_polymorphic_path(question)).to eq(admin_question_path(question))
end
it "routes poll answer videos" do
it "routes poll option videos" do
video = create(:poll_option_video)
expect(admin_polymorphic_path(video)).to eq admin_option_video_path(video.option, video)

View File

@@ -1,12 +1,12 @@
module Polls
def vote_for_poll_via_web(poll, question, answer)
def vote_for_poll_via_web(poll, question, option)
visit poll_path(poll)
within("#poll_question_#{question.id}_answers") do
click_button answer
click_button option
expect(page).to have_button("You have voted #{answer}")
expect(page).not_to have_button("Vote #{answer}")
expect(page).to have_button("You have voted #{option}")
expect(page).not_to have_button("Vote #{option}")
end
end

View File

@@ -119,7 +119,7 @@ describe "Admin polls", :admin do
expect(page).to have_content("There are no polls.")
end
scenario "Can destroy poll with questions and answers" do
scenario "Can destroy poll with questions and options" do
poll = create(:poll, name: "Do you support CONSUL?")
create(:poll_question, :yes_no, poll: poll)

View File

@@ -166,7 +166,7 @@ describe "Polls" do
expect(page).to have_css ".nested-fields", count: 1
end
scenario "Edit poll allows users to remove answers" do
scenario "Edit poll allows users to remove options" do
poll = create(:poll, related: proposal, starts_at: 1.week.from_now)
create(:poll_question, :yes_no, poll: poll)
visit proposal_dashboard_polls_path(proposal)

View File

@@ -11,7 +11,7 @@ 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
scenario "shows options with an image and no description" do
poll = create(:poll)
option = create(:poll_question_option, poll: poll, title: "Pedestrian road", description: "")
create(:image, imageable: option, title: "Trees on both sides of the road")

View File

@@ -8,13 +8,13 @@ describe "Poll Results" do
poll = create(:poll, results_enabled: true)
question1 = create(:poll_question, poll: poll)
answer1 = create(:poll_question_option, question: question1, title: "Yes")
answer2 = create(:poll_question_option, question: question1, title: "No")
option1 = create(:poll_question_option, question: question1, title: "Yes")
option2 = create(:poll_question_option, question: question1, title: "No")
question2 = create(:poll_question, poll: poll)
answer3 = create(:poll_question_option, question: question2, title: "Blue")
answer4 = create(:poll_question_option, question: question2, title: "Green")
answer5 = create(:poll_question_option, question: question2, title: "Yellow")
option3 = create(:poll_question_option, question: question2, title: "Blue")
option4 = create(:poll_question_option, question: question2, title: "Green")
option5 = create(:poll_question_option, question: question2, title: "Yellow")
login_as user1
vote_for_poll_via_web(poll, question1, "Yes")
@@ -39,18 +39,18 @@ describe "Poll Results" do
expect(page).to have_content(question2.title)
within("#question_#{question1.id}_results_table") do
expect(find("#answer_#{answer1.id}_result")).to have_content("2 (66.67%)")
expect(find("#answer_#{answer2.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{option1.id}_result")).to have_content("2 (66.67%)")
expect(find("#answer_#{option2.id}_result")).to have_content("1 (33.33%)")
end
within("#question_#{question2.id}_results_table") do
expect(find("#answer_#{answer3.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{answer4.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{answer5.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{option3.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{option4.id}_result")).to have_content("1 (33.33%)")
expect(find("#answer_#{option5.id}_result")).to have_content("1 (33.33%)")
end
end
scenario "Results for polls with questions but without answers" do
scenario "Results for polls with questions but without options" do
poll = create(:poll, :expired, results_enabled: true)
question = create(:poll_question, poll: poll)