Permit to modify contestants categories
All checks were successful
CI / scan_ruby (push) Successful in 20s
CI / scan_js (push) Successful in 13s
CI / lint (push) Successful in 13s
CI / test (push) Successful in 34s

This commit is contained in:
sto 2025-07-16 10:22:47 +02:00
parent 657c5ac47b
commit b13ef30807
3 changed files with 26 additions and 5 deletions

View File

@ -24,6 +24,7 @@ class ContestantsController < ApplicationController
@contestant = Contestant.new(contestant_params) @contestant = Contestant.new(contestant_params)
@contestant.contest_id = @contest.id @contestant.contest_id = @contest.id
if @contestant.save if @contestant.save
update_contestant_categories
redirect_to contest_path(@contest), notice: t("contestants.new.notice") redirect_to contest_path(@contest), notice: t("contestants.new.notice")
else else
@action_name = t("helpers.buttons.back") @action_name = t("helpers.buttons.back")
@ -36,6 +37,7 @@ class ContestantsController < ApplicationController
authorize @contest authorize @contest
if @contestant.update(contestant_params) if @contestant.update(contestant_params)
update_contestant_categories
redirect_to @contest, notice: t("contestants.edit.notice") redirect_to @contest, notice: t("contestants.edit.notice")
else else
@action_name = t("helpers.buttons.back") @action_name = t("helpers.buttons.back")
@ -126,4 +128,15 @@ class ContestantsController < ApplicationController
def contestant_params def contestant_params
params.expect(contestant: [ :email, :name ]) params.expect(contestant: [ :email, :name ])
end end
def update_contestant_categories
@contestant.categories.clear
@contest.categories.each do |category|
logger.info(params[:contestant]["category_#{category.id}".to_sym] == "1")
if params[:contestant].key?("category_#{category.id}".to_sym) && params[:contestant]["category_#{category.id}".to_sym] == "1"
@contestant.categories << category
end
end
@contestant.save
end
end end

View File

@ -15,6 +15,14 @@
= form.label :email = form.label :email
.form-text .form-text
= t("activerecord.attributes.contestant.email_description") = t("activerecord.attributes.contestant.email_description")
- if @contest.categories
.row.mt-4
.col
- @contest.categories.each do |category|
.form-check.form-switch
= form.check_box "category_#{category.id}".to_sym, class: "form-check-input", checked: @contestant.categories.where(id: category.id).any?
= form.label category.name
.row.mt-4 .row.mt-4
.col .col
- if method == :patch - if method == :patch

View File

@ -54,13 +54,13 @@ javascript:
option value=category.id option value=category.id
= category.name = category.name
javascript: javascript:
const select = document.getElementById('categories'); categorySelectEl = document.getElementById('categories');
const urlParams = new URLSearchParams(window.location.search); urlParams = new URLSearchParams(window.location.search);
const selectedCategory = urlParams.get('category'); selectedCategory = urlParams.get('category');
Array.from(select.children).forEach((option) => { Array.from(categorySelectEl.children).forEach((option) => {
if (option.value == selectedCategory) option.selected = true; if (option.value == selectedCategory) option.selected = true;
}); });
select.addEventListener('change', (e) => { categorySelectEl.addEventListener('change', (e) => {
window.location.href = `#{contest_path(@contest)}?category=${e.target.value}` window.location.href = `#{contest_path(@contest)}?category=${e.target.value}`
}) })
.d-flex.flex-column style="overflow-y: auto" .d-flex.flex-column style="overflow-y: auto"