Refactor vote type descriptions to use data attributes

This makes it easier to extend support for new types (e.g., 'open')
without adding more conditional logic to the JavaScript.
This commit is contained in:
taitus
2025-10-10 13:07:53 +02:00
parent 2a5985f6ef
commit eb10a3135b
3 changed files with 26 additions and 14 deletions

View File

@@ -2,16 +2,18 @@
"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 {
var select_field = $(this);
$("[data-vote-type]").hide(0, function() {
$("[data-vote-type=" + select_field.val() + "]").show();
});
if (select_field.val() === "multiple") {
$(".max-votes").show();
$(".description-unique").hide();
$(".description-multiple").show();
$(".votation-type-max-votes").prop("disabled", false);
} else {
$(".max-votes").hide();
$(".votation-type-max-votes").prop("disabled", true);
}
},
initialize: function() {

View File

@@ -4,12 +4,9 @@
<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>
<% descriptions.each do |vote_type, text| %>
<%= description_tag(vote_type, text) %>
<% end %>
</div>
</div>

View File

@@ -4,4 +4,17 @@ class Admin::VotationTypes::FieldsComponent < ApplicationComponent
def initialize(form:)
@form = form
end
private
def descriptions
{
unique: t("admin.polls.votation_type.unique_description"),
multiple: t("admin.polls.votation_type.multiple_description")
}
end
def description_tag(vote_type, text)
tag.span(text, data: { vote_type: vote_type })
end
end