refactor(champs): add new champs controllers urls with stable_id and row_id

This commit is contained in:
Paul Chavard 2024-04-15 15:06:05 +02:00
parent 1021a31f7b
commit 741712141a
14 changed files with 66 additions and 88 deletions

View file

@ -430,22 +430,6 @@ class ApplicationController < ActionController::Base
controller_instance.try(:nav_bar_profile) controller_instance.try(:nav_bar_profile)
end 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) def cast_bool(value)
ActiveRecord::Type::Boolean.new.deserialize(value) ActiveRecord::Type::Boolean.new.deserialize(value)
end end

View file

@ -1,19 +1,15 @@
class Champs::CarteController < ApplicationController class Champs::CarteController < Champs::ChampController
before_action :authenticate_logged_user!
def index def index
@champ = policy_scope(Champ).find(params[:champ_id])
@focus = params[:focus].present? @focus = params[:focus].present?
end end
def create def create
champ = policy_scope(Champ).find(params[:champ_id])
geo_area = if params_source == GeoArea.sources.fetch(:cadastre) 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 end
if geo_area.nil? 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) if save_feature(geo_area, create_params_feature)
render json: { feature: geo_area.to_feature }, status: :created render json: { feature: geo_area.to_feature }, status: :created
@ -26,8 +22,7 @@ class Champs::CarteController < ApplicationController
end end
def update 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) if save_feature(geo_area, update_params_feature)
head :no_content head :no_content
@ -37,9 +32,8 @@ class Champs::CarteController < ApplicationController
end end
def destroy def destroy
champ = policy_scope(Champ).find(params[:champ_id]) @champ.geo_areas.find(params[:id]).destroy!
champ.geo_areas.find(params[:id]).destroy! @champ.touch
champ.touch
head :no_content head :no_content
end end
@ -82,7 +76,7 @@ class Champs::CarteController < ApplicationController
geo_area.properties.merge!(feature[:properties]) geo_area.properties.merge!(feature[:properties])
end end
if geo_area.save if geo_area.save
geo_area.champ.touch @champ.touch
true true
end end
end end

View file

@ -11,11 +11,15 @@ class Champs::ChampController < ApplicationController
.find(params[:champ_id]) .find(params[:champ_id])
else else
dossier = policy_scope(Dossier).includes(:champs, revision: [:types_de_champ]).find(params[:dossier_id]) 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]) type_de_champ = dossier.find_type_de_champ_by_stable_id(params[:stable_id])
dossier.champ_for_update(type_de_champ, params[:row_id]) dossier.champ_for_update(type_de_champ, params_row_id)
end end
end end
def params_row_id
params[:row_id]
end
def set_champ def set_champ
@champ = find_champ @champ = find_champ
end end

View file

@ -1,13 +1,10 @@
class Champs::OptionsController < ApplicationController class Champs::OptionsController < Champs::ChampController
include TurboChampsConcern include TurboChampsConcern
before_action :authenticate_logged_user!
def remove def remove
champ = policy_scope(Champ).includes(:champs).find(params[:champ_id]) @champ.remove_option([params[:option]].compact, true)
champ.remove_option([params[:option]].compact, true) @dossier = @champ.private? ? nil : @champ.dossier
champs = champ.private? ? champ.dossier.champs_private_all : champ.dossier.champs_public_all champs_attributes = params[:champ_id].present? ? { @champ.id => true } : { @champ.public_id => { with_public_id: true } }
@dossier = champ.private? ? nil : champ.dossier @to_show, @to_hide, @to_update = champs_to_turbo_update(champs_attributes, @champ.dossier.champs)
@to_show, @to_hide, @to_update = champs_to_turbo_update({ params[:champ_id] => true }, champs)
end end
end end

View file

@ -1,7 +1,4 @@
class Champs::PieceJustificativeController < ApplicationController class Champs::PieceJustificativeController < Champs::ChampController
before_action :authenticate_logged_user!
before_action :set_champ
def show def show
respond_to do |format| respond_to do |format|
format.turbo_stream format.turbo_stream
@ -23,10 +20,6 @@ class Champs::PieceJustificativeController < ApplicationController
private private
def set_champ
@champ = policy_scope(Champ).find(params[:champ_id])
end
def attach_piece_justificative def attach_piece_justificative
save_succeed = nil save_succeed = nil

View file

@ -1,8 +1,5 @@
class Champs::RepetitionController < ApplicationController class Champs::RepetitionController < Champs::ChampController
before_action :authenticate_logged_user!
def add def add
@champ = find_champ
row = @champ.add_row(@champ.dossier.revision) row = @champ.add_row(@champ.dossier.revision)
@first_champ_id = row.map(&:focusable_input_id).compact.first @first_champ_id = row.map(&:focusable_input_id).compact.first
@row_id = row.first&.row_id @row_id = row.first&.row_id
@ -10,21 +7,14 @@ class Champs::RepetitionController < ApplicationController
end end
def remove def remove
@champ = find_champ @champ.remove_row(params[:row_id])
@champ.champs.where(row_id: params[:row_id]).destroy_all @to_remove = "safe-row-selector-#{params[:row_id]}"
@champ.reload @to_focus = @champ.focusable_input_id || helpers.dom_id(@champ, :create_repetition)
@row_id = params[:row_id]
end end
private private
def find_champ def params_row_id
if params[:champ_id].present? nil
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
end end
end end

View file

@ -1,9 +1,7 @@
class Champs::RNAController < ApplicationController class Champs::RNAController < Champs::ChampController
before_action :authenticate_logged_user!
def show def show
@champ = policy_scope(Champ).find(params[:champ_id]) champs_attributes = params.dig(:dossier, :champs_public_attributes) || params.dig(:dossier, :champs_private_attributes)
rna = read_param_value(@champ.input_name, 'value') rna = champs_attributes.values.first[:value]
unless @champ.fetch_association!(rna) unless @champ.fetch_association!(rna)
@error = @champ.association_fetch_error_key @error = @champ.association_fetch_error_key

View file

@ -1,10 +1,9 @@
class Champs::SiretController < ApplicationController class Champs::SiretController < Champs::ChampController
before_action :authenticate_logged_user!
def show 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 @siret = @champ.etablissement.siret
else else
@siret = @champ.etablissement_fetch_error_key @siret = @champ.etablissement_fetch_error_key

View file

@ -25,6 +25,15 @@ class Champs::RepetitionChamp < Champ
added_champs added_champs
end 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? def blank?
champs.empty? champs.empty?
end end

View file

@ -1,6 +1,2 @@
= turbo_stream.remove "safe-row-selector-#{@row_id}" = turbo_stream.remove @to_remove
= turbo_stream.focus @to_focus
- 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)

View file

@ -195,9 +195,21 @@ Rails.application.routes.draw do
namespace :champs do namespace :champs do
post ':dossier_id/:stable_id/repetition', to: 'repetition#add', as: :repetition post ':dossier_id/:stable_id/repetition', to: 'repetition#add', as: :repetition
delete ':dossier_id/:stable_id/repetition', to: 'repetition#remove' 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/siret', to: 'siret#show', as: :siret
get ':champ_id/rna', to: 'rna#show', as: :rna get ':champ_id/rna', to: 'rna#show', as: :rna
delete ':champ_id/options', to: 'options#remove', as: :options delete ':champ_id/options', to: 'options#remove', as: :options

View file

@ -5,8 +5,9 @@ describe Champs::RepetitionController, type: :controller do
before { sign_in dossier.user } before { sign_in dossier.user }
it 'removes repetition' do it 'removes repetition' do
rows, repetitions = dossier.champs.partition { _1.parent_id.present? } rows, repetitions = dossier.champs.partition(&:child?)
expect { delete :remove, params: { champ_id: repetitions.first.id, row_id: rows.first.row_id }, format: :turbo_stream } 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) .to change { dossier.reload.champs.size }.from(3).to(1)
end end
end end

View file

@ -7,8 +7,8 @@ describe Champs::RNAController, type: :controller do
let(:champ) { dossier.champs_public.first } let(:champ) { dossier.champs_public.first }
let(:champs_public_attributes) do let(:champs_public_attributes) do
champ_attributes = [] champ_attributes = {}
champ_attributes[champ.id] = { value: rna } champ_attributes[champ.public_id] = { value: rna }
champ_attributes champ_attributes
end end
let(:params) do let(:params) do

View file

@ -7,13 +7,14 @@ describe Champs::SiretController, type: :controller do
let(:champ) { dossier.champs_public.first } let(:champ) { dossier.champs_public.first }
let(:champs_public_attributes) do let(:champs_public_attributes) do
champ_attributes = [] champ_attributes = {}
champ_attributes[champ.id] = { value: siret } champ_attributes[champ.public_id] = { value: siret }
champ_attributes champ_attributes
end end
let(:params) do let(:params) do
{ {
champ_id: champ.id, dossier_id: champ.dossier_id,
stable_id: champ.stable_id,
dossier: { dossier: {
champs_public_attributes: champs_public_attributes champs_public_attributes: champs_public_attributes
} }