diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7584990cb..9b0a6ffef 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -430,22 +430,6 @@ class ApplicationController < ActionController::Base controller_instance.try(:nav_bar_profile) end - # Extract a value from params based on the "path" - # - # params: { dossiers: { champs_public_attributes: { 1234 => { value: "hello" } } } } - # - # Usage: read_param_value("dossiers[champs_public_attributes][1234]", "value") - def read_param_value(path, name) - parts = path.split(/\[|\]\[|\]/) + [name] - parts.reduce(params) do |value, part| - if part.to_i != 0 - value[part.to_i] || value[part] - else - value[part] - end - end - end - def cast_bool(value) ActiveRecord::Type::Boolean.new.deserialize(value) end diff --git a/app/controllers/champs/carte_controller.rb b/app/controllers/champs/carte_controller.rb index dee8e5853..d2fe38e77 100644 --- a/app/controllers/champs/carte_controller.rb +++ b/app/controllers/champs/carte_controller.rb @@ -1,19 +1,15 @@ -class Champs::CarteController < ApplicationController - before_action :authenticate_logged_user! - +class Champs::CarteController < Champs::ChampController def index - @champ = policy_scope(Champ).find(params[:champ_id]) @focus = params[:focus].present? end def create - champ = policy_scope(Champ).find(params[:champ_id]) geo_area = if params_source == GeoArea.sources.fetch(:cadastre) - champ.geo_areas.find_by("properties->>'id' = :id", id: create_params_feature[:properties][:id]) + @champ.geo_areas.find_by("properties->>'id' = :id", id: create_params_feature[:properties][:id]) end if geo_area.nil? - geo_area = champ.geo_areas.build(source: params_source, properties: {}) + geo_area = @champ.geo_areas.build(source: params_source, properties: {}) if save_feature(geo_area, create_params_feature) render json: { feature: geo_area.to_feature }, status: :created @@ -26,8 +22,7 @@ class Champs::CarteController < ApplicationController end def update - champ = policy_scope(Champ).find(params[:champ_id]) - geo_area = champ.geo_areas.find(params[:id]) + geo_area = @champ.geo_areas.find(params[:id]) if save_feature(geo_area, update_params_feature) head :no_content @@ -37,9 +32,8 @@ class Champs::CarteController < ApplicationController end def destroy - champ = policy_scope(Champ).find(params[:champ_id]) - champ.geo_areas.find(params[:id]).destroy! - champ.touch + @champ.geo_areas.find(params[:id]).destroy! + @champ.touch head :no_content end @@ -82,7 +76,7 @@ class Champs::CarteController < ApplicationController geo_area.properties.merge!(feature[:properties]) end if geo_area.save - geo_area.champ.touch + @champ.touch true end end diff --git a/app/controllers/champs/champ_controller.rb b/app/controllers/champs/champ_controller.rb index 4322055be..5c03ee863 100644 --- a/app/controllers/champs/champ_controller.rb +++ b/app/controllers/champs/champ_controller.rb @@ -11,11 +11,15 @@ class Champs::ChampController < ApplicationController .find(params[:champ_id]) else dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id]) - type_de_champ = dossier.revision.types_de_champ.find_by!(stable_id: params[:stable_id]) - dossier.champ_for_update(type_de_champ, params[:row_id]) + type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id]) + dossier.champ_for_update(type_de_champ, params_row_id) end end + def params_row_id + params[:row_id] + end + def set_champ @champ = find_champ end diff --git a/app/controllers/champs/options_controller.rb b/app/controllers/champs/options_controller.rb index 0338700aa..909a4f4be 100644 --- a/app/controllers/champs/options_controller.rb +++ b/app/controllers/champs/options_controller.rb @@ -1,13 +1,10 @@ -class Champs::OptionsController < ApplicationController +class Champs::OptionsController < Champs::ChampController include TurboChampsConcern - before_action :authenticate_logged_user! - def remove - champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) - champ.remove_option([params[:option]].compact, true) - champs = champ.private? ? champ.dossier.champs_private_all : champ.dossier.champs_public_all - @dossier = champ.private? ? nil : champ.dossier - @to_show, @to_hide, @to_update = champs_to_turbo_update({ params[:champ_id] => true }, champs) + @champ.remove_option([params[:option]].compact, true) + @dossier = @champ.private? ? nil : @champ.dossier + champs_attributes = params[:champ_id].present? ? { @champ.id => true } : { @champ.public_id => { with_public_id: true } } + @to_show, @to_hide, @to_update = champs_to_turbo_update(champs_attributes, @champ.dossier.champs) end end diff --git a/app/controllers/champs/piece_justificative_controller.rb b/app/controllers/champs/piece_justificative_controller.rb index 20d865492..43d1b8134 100644 --- a/app/controllers/champs/piece_justificative_controller.rb +++ b/app/controllers/champs/piece_justificative_controller.rb @@ -1,7 +1,4 @@ -class Champs::PieceJustificativeController < ApplicationController - before_action :authenticate_logged_user! - before_action :set_champ - +class Champs::PieceJustificativeController < Champs::ChampController def show respond_to do |format| format.turbo_stream @@ -23,10 +20,6 @@ class Champs::PieceJustificativeController < ApplicationController private - def set_champ - @champ = policy_scope(Champ).find(params[:champ_id]) - end - def attach_piece_justificative save_succeed = nil diff --git a/app/controllers/champs/repetition_controller.rb b/app/controllers/champs/repetition_controller.rb index 998dc00cb..6a09dd275 100644 --- a/app/controllers/champs/repetition_controller.rb +++ b/app/controllers/champs/repetition_controller.rb @@ -1,8 +1,5 @@ -class Champs::RepetitionController < ApplicationController - before_action :authenticate_logged_user! - +class Champs::RepetitionController < Champs::ChampController def add - @champ = find_champ row = @champ.add_row(@champ.dossier.revision) @first_champ_id = row.map(&:focusable_input_id).compact.first @row_id = row.first&.row_id @@ -10,21 +7,14 @@ class Champs::RepetitionController < ApplicationController end def remove - @champ = find_champ - @champ.champs.where(row_id: params[:row_id]).destroy_all - @champ.reload - @row_id = params[:row_id] + @champ.remove_row(params[:row_id]) + @to_remove = "safe-row-selector-#{params[:row_id]}" + @to_focus = @champ.focusable_input_id || helpers.dom_id(@champ, :create_repetition) end private - def find_champ - if params[:champ_id].present? - policy_scope(Champ).includes(:champs).find(params[:champ_id]) - else - policy_scope(Champ) - .includes(:champs, :type_de_champ) - .find_by!(dossier_id: params[:dossier_id], type_de_champ: { stable_id: params[:stable_id] }) - end + def params_row_id + nil end end diff --git a/app/controllers/champs/rna_controller.rb b/app/controllers/champs/rna_controller.rb index a8f49f7bd..e96ab6671 100644 --- a/app/controllers/champs/rna_controller.rb +++ b/app/controllers/champs/rna_controller.rb @@ -1,9 +1,7 @@ -class Champs::RNAController < ApplicationController - before_action :authenticate_logged_user! - +class Champs::RNAController < Champs::ChampController def show - @champ = policy_scope(Champ).find(params[:champ_id]) - rna = read_param_value(@champ.input_name, 'value') + champs_attributes = params.dig(:dossier, :champs_public_attributes) || params.dig(:dossier, :champs_private_attributes) + rna = champs_attributes.values.first[:value] unless @champ.fetch_association!(rna) @error = @champ.association_fetch_error_key diff --git a/app/controllers/champs/siret_controller.rb b/app/controllers/champs/siret_controller.rb index 23e4ab2ee..83d98f60c 100644 --- a/app/controllers/champs/siret_controller.rb +++ b/app/controllers/champs/siret_controller.rb @@ -1,10 +1,9 @@ -class Champs::SiretController < ApplicationController - before_action :authenticate_logged_user! - +class Champs::SiretController < Champs::ChampController def show - @champ = policy_scope(Champ).find(params[:champ_id]) + champs_attributes = params.dig(:dossier, :champs_public_attributes) || params.dig(:dossier, :champs_private_attributes) + siret = champs_attributes.values.first[:value] - if @champ.fetch_etablissement!(read_param_value(@champ.input_name, 'value'), current_user) + if @champ.fetch_etablissement!(siret, current_user) @siret = @champ.etablissement.siret else @siret = @champ.etablissement_fetch_error_key diff --git a/app/models/champs/repetition_champ.rb b/app/models/champs/repetition_champ.rb index 6bb189c49..7035488db 100644 --- a/app/models/champs/repetition_champ.rb +++ b/app/models/champs/repetition_champ.rb @@ -25,6 +25,15 @@ class Champs::RepetitionChamp < Champ added_champs end + def remove_row(row_id) + dossier.champs.where(row_id:).destroy_all + dossier.champs.reload + end + + def focusable_input_id + rows.last&.first&.focusable_input_id + end + def blank? champs.empty? end diff --git a/app/views/champs/repetition/remove.turbo_stream.haml b/app/views/champs/repetition/remove.turbo_stream.haml index 5fb0fe0be..d7be234a4 100644 --- a/app/views/champs/repetition/remove.turbo_stream.haml +++ b/app/views/champs/repetition/remove.turbo_stream.haml @@ -1,6 +1,2 @@ -= turbo_stream.remove "safe-row-selector-#{@row_id}" - -- if @champ.rows.size > 0 && @champ.rows.last&.first&.present? - = turbo_stream.focus @champ.rows.last&.first.focusable_input_id -- else - = turbo_stream.focus dom_id(@champ, :create_repetition) += turbo_stream.remove @to_remove += turbo_stream.focus @to_focus diff --git a/config/routes.rb b/config/routes.rb index 0ab8203f2..caf5a3358 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -195,9 +195,21 @@ Rails.application.routes.draw do namespace :champs do post ':dossier_id/:stable_id/repetition', to: 'repetition#add', as: :repetition delete ':dossier_id/:stable_id/repetition', to: 'repetition#remove' - post ':champ_id/repetition', to: 'repetition#add' - delete ':champ_id/repetition', to: 'repetition#remove' + get ':dossier_id/:stable_id/siret', to: 'siret#show' + get ':dossier_id/:stable_id/rna', to: 'rna#show' + delete ':dossier_id/:stable_id/options', to: 'options#remove' + + get ':dossier_id/:stable_id/carte/features', to: 'carte#index' + post ':dossier_id/:stable_id/carte/features', to: 'carte#create' + patch ':dossier_id/:stable_id/carte/features/:id', to: 'carte#update' + delete ':dossier_id/:stable_id/carte/features/:id', to: 'carte#destroy' + + get ':dossier_id/:stable_id/piece_justificative', to: 'piece_justificative#show' + put ':dossier_id/:stable_id/piece_justificative', to: 'piece_justificative#update' + get ':dossier_id/:stable_id/piece_justificative/template', to: 'piece_justificative#template' + + # TODO: remove after migration is ower get ':champ_id/siret', to: 'siret#show', as: :siret get ':champ_id/rna', to: 'rna#show', as: :rna delete ':champ_id/options', to: 'options#remove', as: :options diff --git a/spec/controllers/champs/repetition_controller_spec.rb b/spec/controllers/champs/repetition_controller_spec.rb index 2f6fed04e..3b80b5c32 100644 --- a/spec/controllers/champs/repetition_controller_spec.rb +++ b/spec/controllers/champs/repetition_controller_spec.rb @@ -5,8 +5,9 @@ describe Champs::RepetitionController, type: :controller do before { sign_in dossier.user } it 'removes repetition' do - rows, repetitions = dossier.champs.partition { _1.parent_id.present? } - expect { delete :remove, params: { champ_id: repetitions.first.id, row_id: rows.first.row_id }, format: :turbo_stream } + rows, repetitions = dossier.champs.partition(&:child?) + repetition = repetitions.first + expect { delete :remove, params: { dossier_id: repetition.dossier, stable_id: repetition.stable_id, row_id: rows.first.row_id }, format: :turbo_stream } .to change { dossier.reload.champs.size }.from(3).to(1) end end diff --git a/spec/controllers/champs/rna_controller_spec.rb b/spec/controllers/champs/rna_controller_spec.rb index c85d49685..1c738f794 100644 --- a/spec/controllers/champs/rna_controller_spec.rb +++ b/spec/controllers/champs/rna_controller_spec.rb @@ -7,8 +7,8 @@ describe Champs::RNAController, type: :controller do let(:champ) { dossier.champs_public.first } let(:champs_public_attributes) do - champ_attributes = [] - champ_attributes[champ.id] = { value: rna } + champ_attributes = {} + champ_attributes[champ.public_id] = { value: rna } champ_attributes end let(:params) do diff --git a/spec/controllers/champs/siret_controller_spec.rb b/spec/controllers/champs/siret_controller_spec.rb index 67a1ac8de..4e5212e36 100644 --- a/spec/controllers/champs/siret_controller_spec.rb +++ b/spec/controllers/champs/siret_controller_spec.rb @@ -7,13 +7,14 @@ describe Champs::SiretController, type: :controller do let(:champ) { dossier.champs_public.first } let(:champs_public_attributes) do - champ_attributes = [] - champ_attributes[champ.id] = { value: siret } + champ_attributes = {} + champ_attributes[champ.public_id] = { value: siret } champ_attributes end let(:params) do { - champ_id: champ.id, + dossier_id: champ.dossier_id, + stable_id: champ.stable_id, dossier: { champs_public_attributes: champs_public_attributes }