From 6a216670ee235c79a99ec4c484e80ec1067e944d Mon Sep 17 00:00:00 2001 From: Andrea Ranaldi Date: Wed, 18 Dec 2024 17:04:57 +0100 Subject: [PATCH] 104 add happening question (#109) * Update (#108) * 103 aggiungere tipi di domande integer e data (#107) * aggiunte tipologie di domande per allegati, date, numeri * Aggiunti devcontainer * navbar title configuration with RAILS_TITLE and RAILS_ICON (#106) * attivati tasti per aggiungere e rimuovere le domande per gli happening * removed codeql external flow --- .github/workflows/codeql.yml | 94 ------------------- .../questions/sub_form_component.html.haml | 20 ++-- .../editor/happenings_controller.rb | 6 +- .../controllers/nested_controller.js | 26 +++-- app/models/happening.rb | 2 +- app/views/editor/events/index.html.haml | 2 +- app/views/editor/happenings/edit.html.haml | 11 ++- config/locales/activerecord.it.yml | 9 ++ 8 files changed, 52 insertions(+), 118 deletions(-) delete mode 100644 .github/workflows/codeql.yml diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml deleted file mode 100644 index 32fdad1..0000000 --- a/.github/workflows/codeql.yml +++ /dev/null @@ -1,94 +0,0 @@ -# For most projects, this workflow file will not need changing; you simply need -# to commit it to your repository. -# -# You may wish to alter this file to override the set of languages analyzed, -# or to provide custom queries or build logic. -# -# ******** NOTE ******** -# We have attempted to detect the languages in your repository. Please check -# the `language` matrix defined below to confirm you have the correct set of -# supported CodeQL languages. -# -name: "CodeQL Advanced" - -on: - push: - branches: [ "master" ] - pull_request: - branches: [ "master" ] - schedule: - - cron: '44 0 * * 0' - -jobs: - analyze: - name: Analyze (${{ matrix.language }}) - # Runner size impacts CodeQL analysis time. To learn more, please see: - # - https://gh.io/recommended-hardware-resources-for-running-codeql - # - https://gh.io/supported-runners-and-hardware-resources - # - https://gh.io/using-larger-runners (GitHub.com only) - # Consider using larger runners or machines with greater resources for possible analysis time improvements. - runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }} - permissions: - # required for all workflows - security-events: write - - # required to fetch internal or private CodeQL packs - packages: read - - # only required for workflows in private repositories - actions: read - contents: read - - strategy: - fail-fast: false - matrix: - include: - - language: javascript-typescript - build-mode: none - - language: ruby - build-mode: none - # CodeQL supports the following values keywords for 'language': 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift' - # Use `c-cpp` to analyze code written in C, C++ or both - # Use 'java-kotlin' to analyze code written in Java, Kotlin or both - # Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both - # To learn more about changing the languages that are analyzed or customizing the build mode for your analysis, - # see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning. - # If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how - # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Initializes the CodeQL tools for scanning. - - name: Initialize CodeQL - uses: github/codeql-action/init@v3 - with: - languages: ${{ matrix.language }} - build-mode: ${{ matrix.build-mode }} - # If you wish to specify custom queries, you can do so here or in a config file. - # By default, queries listed here will override any specified in a config file. - # Prefix the list here with "+" to use these queries and those in the config file. - - # For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs - # queries: security-extended,security-and-quality - - # If the analyze step fails for one of the languages you are analyzing with - # "We were unable to automatically build your code", modify the matrix above - # to set the build mode to "manual" for that language. Then modify this step - # to build your code. - # ℹī¸ Command-line programs to run using the OS shell. - # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun - - if: matrix.build-mode == 'manual' - shell: bash - run: | - echo 'If you are using a "manual" build mode for one or more of the' \ - 'languages you are analyzing, replace this with the commands to build' \ - 'your code, for example:' - echo ' make bootstrap' - echo ' make release' - exit 1 - - - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 - with: - category: "/language:${{matrix.language}}" diff --git a/app/components/editor/questions/sub_form_component.html.haml b/app/components/editor/questions/sub_form_component.html.haml index f87aba3..e6d20f8 100644 --- a/app/components/editor/questions/sub_form_component.html.haml +++ b/app/components/editor/questions/sub_form_component.html.haml @@ -1,19 +1,23 @@ -.field - = @form.label :title - .control - = @form.text_field :title, class: 'input ', required: true - .field.is-horizontal .field-body + .field + = @form.label :title + .control + = @form.text_field :title, class: 'input ', required: true .field = @form.label :category .control - = @form.select :category, [['String',:string],['Text box',:text], ['Selezione', :select], ['File', :file]], {}, mandatory: true, data: {controller: 'slim', target: 'slim.simple'} + .select.is-fullwidth= @form.select :category, [['Stringa',:string],['Box testo',:text], ['Selezione', :select], ['File', :file],['Data', :date],['Numero', :number]], {}, mandatory: true .field - = @form.label :madatory + = @form.label :mandatory .control - = @form.select :mandatory, [['Si',true],['No',false]], {}, mandatory: true, data: {controller: 'slim', target: 'slim.simple'} + .select.is-fullwidth= @form.select :mandatory, [['Si',true],['No',false]], {}, mandatory: true .field = @form.label :weight .control = @form.number_field :weight, class: 'input', required: true + .field + = @form.label :_destroy + .control + .select.is-fullwidth= @form.select :_destroy, [['Mantieni', nil],['Elimina', 1]] + diff --git a/app/controllers/editor/happenings_controller.rb b/app/controllers/editor/happenings_controller.rb index 0c0f8c4..8586817 100644 --- a/app/controllers/editor/happenings_controller.rb +++ b/app/controllers/editor/happenings_controller.rb @@ -30,7 +30,9 @@ def new end # GET /editor/events/:event_id/happenings/:id/edit - def edit; end + def edit + @happening.questions.new + end # POST /editor/happenings def create @@ -85,7 +87,7 @@ def set_happening # Filter params for set an {Happening} def happening_params - params.require(:happening).permit(:title, :image, :event_id, :max_tickets, :max_tickets_for_user, :start_at, :start_sale_at, :stop_sale_at, questions_attributes: [ :id, :title, :category, :mandatory, :weight ]) + params.require(:happening).permit(:title, :image, :event_id, :max_tickets, :max_tickets_for_user, :start_at, :start_sale_at, :stop_sale_at, questions_attributes: [ :id, :title, :category, :mandatory, :weight, :_destroy ]) end def massive_create_params diff --git a/app/javascript/controllers/nested_controller.js b/app/javascript/controllers/nested_controller.js index 9b236c5..4e6d665 100644 --- a/app/javascript/controllers/nested_controller.js +++ b/app/javascript/controllers/nested_controller.js @@ -2,15 +2,23 @@ import { Controller } from "@hotwired/stimulus" // Connects to data-controller="nested" export default class extends Controller { - static targets = ["row"] // 1 + static targets = [ 'content', 'template' ] - connect() { - } - - deleteRow() { // 2 - event.preventDefault(); // 3 - - let index = event.currentTarget.getAttribute("data-index"); // 4, 5 - this.bookRowTargets[index].classList.toggle("hidden"); // 6, 7 + add() { + event.preventDefault() + let clone = document.createElement('div') + clone.setAttribute('class', this.templateTarget.getAttribute('class')) + clone.dataset.target = 'nested.entry' + clone.innerHTML = this.templateTarget.innerHTML + this.contentTarget.appendChild(clone) + let index = this.templateTarget.dataset.index + let newIndex = this.contentTarget.children.length + 1 + clone.querySelectorAll('[name]').forEach( (field) => { + field.setAttribute('name', field.getAttribute('name').replace(`[${index}]`, `[${newIndex}]`)) + field.setAttribute('id', field.getAttribute('name').replace(`[${index}]`, `[${newIndex}]`)) + } ) + clone.querySelectorAll('[for]').forEach( (field) => { + field.setAttribute('for', field.getAttribute('for').replace(`_${index}_`, `_${newIndex}_`)) + } ) } } diff --git a/app/models/happening.rb b/app/models/happening.rb index 074d348..630d418 100644 --- a/app/models/happening.rb +++ b/app/models/happening.rb @@ -46,7 +46,7 @@ class Happening < ApplicationRecord attachable.variant :ticket, resize_to_limit: [ 150, 68 ] end - accepts_nested_attributes_for :questions, reject_if: :all_blank + accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true validates :event, presence: true validates :start_at, presence: true diff --git a/app/views/editor/events/index.html.haml b/app/views/editor/events/index.html.haml index e808ada..55d7e1a 100644 --- a/app/views/editor/events/index.html.haml +++ b/app/views/editor/events/index.html.haml @@ -2,7 +2,7 @@ %h3.title.is-size-3.has-text-centered %i.fas.fa-ticket-alt = t 'editor' - = t('activerecord.models.events.other') + = t('activerecord.models.event.other') .columns %aside.column.is-3 = render Layout::SearchComponent.new(url: editor_events_url, turbo_frame: 'events', categories: @categories, editor: true) diff --git a/app/views/editor/happenings/edit.html.haml b/app/views/editor/happenings/edit.html.haml index 889efee..26503f5 100644 --- a/app/views/editor/happenings/edit.html.haml +++ b/app/views/editor/happenings/edit.html.haml @@ -6,7 +6,7 @@ %turbo-frame#happening .box - = form_with(model: [:editor, @happening], data: {turbo_frame: 'happening'}) do |form| + = form_with(model: [:editor, @happening], data: {turbo_frame: 'happening', controller: 'nested'}) do |form| = form.hidden_field :event_id, value: @event.id .field.is-horizontal .field-body @@ -65,10 +65,15 @@ %i.fas.fa-chair %p.help.is-danger= @happening.errors[:max_tickets_for_user].join(' - ') .divider Domande - = form.fields_for :questions do |sub_form| - = render Editor::Questions::SubFormComponent.new form: sub_form + .div{data: {nested_target: 'content'}} + = form.fields_for :questions do |sub_form| + - if sub_form.object.persisted? + = render Editor::Questions::SubFormComponent.new form: sub_form + - else + %template{data: {nested_target: 'template'}}= render Editor::Questions::SubFormComponent.new form: sub_form .buttons.has-text-centered = form.button fas_icon('save', text: t('site.generic.save')), class: 'button is-success' %a.button.is-link{href: editor_happening_path(@happening)}= fas_icon 'chevron-left', text: 'Indietro' = link_to fas_icon('trash', text: 'Elimina'), editor_happening_path(@happening), class: 'button is-warning', data: {turbo_confirm: 'Attenzione la data e tutte le prenotazioni saranno eliminate, confermi?', turbo_method: :delete, turbo_frame: 'yield'} + %a.is-success.button{data: {action: 'nested#add'}}= fas_icon 'plus', text: 'Aggiungi domanda' diff --git a/config/locales/activerecord.it.yml b/config/locales/activerecord.it.yml index f76706e..6c35c0e 100644 --- a/config/locales/activerecord.it.yml +++ b/config/locales/activerecord.it.yml @@ -24,6 +24,12 @@ it: stop_sale_at: Data di fine prenotazioni repeat_in: Giorni in cui ripetere la data repeat_fo: N. di giorni per cui ripetere l'evento + question: + title: Titolo + category: Categorie + mandatory: Obbligatorio + weight: Ordine + _destroy: Elimina template: title: Titolo data: Struttura domande @@ -42,6 +48,9 @@ it: happening: one: Data other: Date + question: + one: Domanda + other: Domande template: one: Template domande other: Template domande