Add VotationType fields to admin poll question form

This commit is contained in:
decabeza
2022-08-30 13:13:33 +02:00
committed by Senén Rodero Rodríguez
parent d1c1aa6691
commit 815a526d78
13 changed files with 129 additions and 3 deletions

View File

@@ -0,0 +1,21 @@
(function() {
"use strict";
App.AdminVotationTypesFields = {
adjustForm: function() {
if ($(this).val() === "unique") {
$(".max-votes").hide();
$(".description-unique").show();
$(".description-multiple").hide();
$(".votation-type-max-votes").prop("disabled", true);
} else {
$(".max-votes").show();
$(".description-unique").hide();
$(".description-multiple").show();
$(".votation-type-max-votes").prop("disabled", false);
}
},
initialize: function() {
$(".votation-type-vote-type").on("change", App.AdminVotationTypesFields.adjustForm).trigger("change");
}
};
}).call(this);

View File

@@ -167,6 +167,7 @@ var initialize_modules = function() {
} }
App.AdminBudgetsWizardCreationStep.initialize(); App.AdminBudgetsWizardCreationStep.initialize();
App.AdminMachineLearningScripts.initialize(); App.AdminMachineLearningScripts.initialize();
App.AdminVotationTypesFields.initialize();
App.BudgetEditAssociations.initialize(); App.BudgetEditAssociations.initialize();
App.BudgetHideMoney.initialize(); App.BudgetHideMoney.initialize();
App.Datepicker.initialize(); App.Datepicker.initialize();

View File

@@ -29,6 +29,14 @@
<% end %> <% end %>
</div> </div>
<div class="row">
<div class="small-12">
<%= f.fields_for :votation_type do |votation_f| %>
<%= render Admin::VotationTypes::FieldsComponent.new(form: votation_f) %>
<% end %>
</div>
</div>
<div class="row"> <div class="row">
<div class="small-12 medium-4 large-2 margin-top column"> <div class="small-12 medium-4 large-2 margin-top column">
<%= f.submit(class: "button success expanded", value: t("shared.save")) %> <%= f.submit(class: "button success expanded", value: t("shared.save")) %>

View File

@@ -0,0 +1,20 @@
<div class="small-12 medium-6 column">
<%= form.enum_select :vote_type, {}, class: "votation-type-vote-type" %>
</div>
<div class="small-12 column">
<div class="callout primary">
<span class="description-unique">
<%= t("admin.polls.votation_type.unique_description") %>
</span>
<span class="description-multiple hidden">
<%= t("admin.polls.votation_type.multiple_description") %>
</span>
</div>
</div>
<div class="small-12 medium-6 column end">
<div class="max-votes">
<%= form.number_field :max_votes, min: 2, max: 999, class: "votation-type-max-votes" %>
</div>
</div>

View File

@@ -0,0 +1,7 @@
class Admin::VotationTypes::FieldsComponent < ApplicationComponent
attr_reader :form
def initialize(form:)
@form = form
end
end

View File

@@ -14,10 +14,10 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
end end
def new def new
@polls = Poll.all
proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present? proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present?
@question.copy_attributes_from_proposal(proposal) @question.copy_attributes_from_proposal(proposal)
@question.poll = @poll @question.poll = @poll
@question.votation_type = VotationType.new
authorize! :create, @question authorize! :create, @question
end end
@@ -58,8 +58,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController
end end
def allowed_params def allowed_params
attributes = [:poll_id, :question, :proposal_id] attributes = [:poll_id, :question, :proposal_id, votation_type_attributes: [:vote_type, :max_votes]]
[*attributes, translation_params(Poll::Question)] [*attributes, translation_params(Poll::Question)]
end end

View File

@@ -3,6 +3,7 @@ module Questionable
included do included do
has_one :votation_type, as: :questionable, dependent: :destroy has_one :votation_type, as: :questionable, dependent: :destroy
accepts_nested_attributes_for :votation_type
delegate :max_votes, :multiple?, :vote_type, to: :votation_type, allow_nil: true delegate :max_votes, :multiple?, :vote_type, to: :votation_type, allow_nil: true
end end

View File

@@ -28,6 +28,21 @@
<%= link_to @question.proposal.title, proposal_path(@question.proposal) %> <%= link_to @question.proposal.title, proposal_path(@question.proposal) %>
</p> </p>
<% end %> <% end %>
<% if @question.votation_type.present? %>
<p>
<strong><%= t("admin.polls.votation_type.title") %></strong>
<br>
<%= VotationType.human_attribute_name("vote_type.#{@question.vote_type}") %>
</p>
<% if @question.max_votes.present? %>
<p>
<strong><%= VotationType.human_attribute_name("max_votes") %></strong>
<br>
<%= @question.max_votes %>
</p>
<% end %>
<% end %>
</div> </div>
</div> </div>

View File

@@ -145,6 +145,9 @@ en:
local_census_record: local_census_record:
one: Local census record one: Local census record
other: Local census records other: Local census records
votation_type:
one: Votation type
other: Votation types
attributes: attributes:
budget: budget:
name: "Name" name: "Name"
@@ -514,6 +517,12 @@ en:
document_number: Document number document_number: Document number
date_of_birth: Date of birth date_of_birth: Date of birth
postal_code: Postal code postal_code: Postal code
votation_type:
max_votes: Maximum number of votes
vote_type: Votation type
votation_type/vote_type:
unique: Unique answer
multiple: Multiple answers
errors: errors:
models: models:
user: user:

View File

@@ -1107,6 +1107,10 @@ en:
alert: "This action will remove the poll and all its associated questions." alert: "This action will remove the poll and all its associated questions."
success_notice: "Poll deleted successfully" success_notice: "Poll deleted successfully"
unable_notice: "You cannot delete a poll that has votes" unable_notice: "You cannot delete a poll that has votes"
votation_type:
title: "Votation type"
unique_description: "It's only possible to answer one time to the question."
multiple_description: "Allows to choose multiple answers. It's possible to set the maximum number of answers."
questions: questions:
index: index:
title: "Questions" title: "Questions"

View File

@@ -145,6 +145,9 @@ es:
local_census_record: local_census_record:
one: Registro del censo local one: Registro del censo local
other: Registros del censo local other: Registros del censo local
votation_type:
one: Tipo de votación
other: Tipos de votación
attributes: attributes:
budget: budget:
name: "Nombre" name: "Nombre"
@@ -514,6 +517,12 @@ es:
document_number: Número de documento document_number: Número de documento
date_of_birth: Fecha de nacimiento date_of_birth: Fecha de nacimiento
postal_code: Código postal postal_code: Código postal
votation_type:
max_votes: Número máximo de votos
vote_type: Tipo de votación
votation_type/vote_type:
unique: Respuesta única
multiple: Respuesta múltiple
errors: errors:
models: models:
user: user:

View File

@@ -1106,6 +1106,10 @@ es:
alert: "Esta acción eliminará la votación y todas sus preguntas asociadas." alert: "Esta acción eliminará la votación y todas sus preguntas asociadas."
success_notice: "Votación eliminada correctamente" success_notice: "Votación eliminada correctamente"
unable_notice: "No se pueden eliminar votaciones con votos" unable_notice: "No se pueden eliminar votaciones con votos"
votation_type:
title: "Tipo de votación"
unique_description: "Solo se puede responder a la pregunta con una única respuesta."
multiple_description: "Permite elegir más de una respuesta. Se puede elegir el número máximo de respuestas."
questions: questions:
index: index:
title: "Preguntas de votaciones" title: "Preguntas de votaciones"

View File

@@ -65,6 +65,34 @@ describe "Admin poll questions", :admin do
expect(page).not_to have_link "Create question" expect(page).not_to have_link "Create question"
end end
describe "With votation type" do
before do
poll = create(:poll, :future)
visit admin_poll_path(poll)
click_link "Create question"
end
scenario "Unique" do
fill_in "Question", with: "Question with unique answer"
select "Unique answer", from: "Votation type"
click_button "Save"
expect(page).to have_content "Question with unique answer"
expect(page).to have_content "Unique answer"
end
scenario "Multiple" do
fill_in "Question", with: "Question with multiple answers"
select "Multiple answers", from: "Votation type"
fill_in "Maximum number of votes", with: 6
click_button "Save"
expect(page).to have_content "Question with multiple answers"
expect(page).to have_content "Multiple answers"
end
end
end end
scenario "Create from proposal" do scenario "Create from proposal" do