feature: type_de_champ_controller

This commit is contained in:
simon lehericey 2022-07-11 21:18:08 +02:00
parent 2da0aa6525
commit 2fee0ddfd4
9 changed files with 181 additions and 47 deletions

View file

@ -7,6 +7,9 @@ module Administrateurs
if type_de_champ.valid?
@coordinate = draft.coordinate_for(type_de_champ)
@created = champ_component_from(@coordinate, focused: true)
@morphed = champ_components_starting_at(@coordinate.position + 1)
reset_procedure
flash.notice = "Formulaire enregistré"
else
@ -18,9 +21,9 @@ module Administrateurs
type_de_champ = draft.find_and_ensure_exclusive_use(params[:stable_id])
if type_de_champ.update(type_de_champ_update_params)
if params[:should_render]
@coordinate = draft.coordinate_for(type_de_champ)
end
@coordinate = draft.coordinate_for(type_de_champ)
@morphed = champ_components_starting_at(@coordinate.position)
reset_procedure
flash.notice = "Formulaire enregistré"
else
@ -36,21 +39,47 @@ module Administrateurs
def move_up
flash.notice = "Formulaire enregistré"
@coordinate = draft.move_up_type_de_champ(params[:stable_id])
@destroyed = @coordinate
@created = champ_component_from(@coordinate)
# update the one component below
@morphed = champ_components_starting_at(@coordinate.position + 1).take(1)
end
def move_down
flash.notice = "Formulaire enregistré"
@coordinate = draft.move_down_type_de_champ(params[:stable_id])
@destroyed = @coordinate
@created = champ_component_from(@coordinate)
# update the one component above
@morphed = champ_components_starting_at(@coordinate.position - 1).take(1)
end
def destroy
@coordinate = draft.remove_type_de_champ(params[:stable_id])
reset_procedure
flash.notice = "Formulaire enregistré"
@destroyed = @coordinate
@morphed = champ_components_starting_at(@coordinate.position)
end
private
def champ_components_starting_at(position)
draft
.coordinates_starting_at(position)
.lazy
.map { |c| champ_component_from(c) }
end
def champ_component_from(coordinate, focused: false)
TypesDeChampEditor::ChampComponent.new(
coordinate: coordinate,
upper_coordinates: draft.upper_coordinates(coordinate.position),
focused: focused
)
end
def type_de_champ_create_params
params
.required(:type_de_champ)

View file

@ -197,6 +197,14 @@ class ProcedureRevision < ApplicationRecord
revision_types_de_champ.find_by!(type_de_champ: tdc)
end
def upper_coordinates(position)
revision_types_de_champ_public.filter { |c| c.position < position }
end
def coordinates_starting_at(position)
revision_types_de_champ_public.reload.filter { |c| position <= c.position }
end
private
def compute_estimated_fill_duration
@ -482,7 +490,8 @@ class ProcedureRevision < ApplicationRecord
types_de_champ_public
.map.with_index
.filter_map { |tdc, i| tdc.condition.present? ? [tdc, i] : nil }
.flat_map { |tdc, i| tdc.condition.errors(stable_ids.take(i)) }
.each { |message| errors.add(:condition, message) }
.map { |tdc, i| [tdc, tdc.condition.errors(stable_ids.take(i))] }
.filter { |_tdc, errors| errors.present? }
.each { |tdc, message| errors.add(:condition, message, type_de_champ: tdc) }
end
end

View file

@ -1,14 +1,19 @@
- sibling = coordinate.previous_sibling
- if @destroyed.present?
= turbo_stream.remove dom_id(@destroyed, :type_de_champ_editor)
- if @created.present?
- if @created.coordinate.previous_sibling.present?
= turbo_stream.after dom_id(@created.coordinate.previous_sibling, :type_de_champ_editor) do
= render @created
- else
= turbo_stream.prepend dom_id(@created.coordinate.block, :types_de_champ_editor_block) do
= render @created
- @morphed&.each do |champ_component|
= turbo_stream.morph dom_id(champ_component.coordinate, :type_de_champ_editor) do
= render champ_component
= turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do
= render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?)
- if sibling.present?
= turbo_stream.after dom_id(sibling, :type_de_champ_editor) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true)
- else
= turbo_stream.prepend dom_id(coordinate.block, :types_de_champ_editor_block) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: coordinate, upper_coordinates: upper_coordinates, focused: true)
= turbo_stream.dispatch 'sortable:sort'
- if @lower_coordinates_and_dep.present?
- @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates|
= turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates)

View file

@ -1,2 +1 @@
- if @coordinate&.type_de_champ&.valid?
= render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates }
= render partial: 'insert'

View file

@ -1,9 +1 @@
= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor)
= turbo_stream.dispatch 'sortable:sort'
= turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do
= render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?)
- if @lower_coordinates_and_dep.present?
- @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates|
= turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates)
= render partial: 'insert'

View file

@ -1,2 +1 @@
= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor)
= render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates }
= render partial: 'insert'

View file

@ -1,2 +1 @@
= turbo_stream.remove dom_id(@coordinate, :type_de_champ_editor)
= render partial: 'insert', locals: { coordinate: @coordinate, upper_coordinates: @upper_coordinates }
= render partial: 'insert'

View file

@ -1,10 +1 @@
- if @coordinate&.type_de_champ&.valid?
= turbo_stream.morph dom_id(@coordinate, :type_de_champ_editor) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: @coordinate, upper_coordinates: @upper_coordinates)
= turbo_stream.morph dom_id(@coordinate.revision, :estimated_fill_duration) do
= render TypesDeChampEditor::EstimatedFillDurationComponent.new(revision: @coordinate.revision, is_annotation: @coordinate.private?)
- if @lower_coordinates_and_dep.present?
- @lower_coordinates_and_dep.each do |other_coordinate, other_upper_coordinates|
= turbo_stream.morph dom_id(other_coordinate, :type_de_champ_editor) do
= render TypesDeChampEditor::ChampComponent.new(coordinate: other_coordinate, upper_coordinates: other_upper_coordinates)
= render partial: 'insert'

View file

@ -1,5 +1,23 @@
describe Administrateurs::TypesDeChampController, type: :controller do
let(:procedure) { create(:procedure) }
let(:procedure) do
create(:procedure).tap do |p|
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l1')
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l2')
p.draft_revision.add_type_de_champ(type_champ: :integer_number, libelle: 'l3')
p.draft_revision.add_type_de_champ(type_champ: :yes_no, libelle: 'bon dossier', private: true)
end
end
def first_coordinate = procedure.draft_revision.revision_types_de_champ_public.first
def second_coordinate = procedure.draft_revision.revision_types_de_champ_public.reload.second
def third_coordinate = procedure.draft_revision.revision_types_de_champ_public.reload.third
def extract_libelle(champ_component) = [champ_component.coordinate.libelle, champ_component.upper_coordinates.map(&:libelle)]
def morpheds
assigns(:morphed)
.map { |component| extract_libelle(component) }.to_a
end
before { sign_in(procedure.administrateurs.first.user) }
@ -11,9 +29,9 @@ describe Administrateurs::TypesDeChampController, type: :controller do
procedure_id: procedure.id,
type_de_champ: {
type_champ: type_champ,
libelle: 'Nouveau champ',
private: false,
placeholder: "custom placeholder"
libelle: 'l1.5',
placeholder: "custom placeholder",
after_stable_id: first_coordinate.stable_id
}
}
end
@ -23,7 +41,15 @@ describe Administrateurs::TypesDeChampController, type: :controller do
context "create type_de_champ text" do
let(:type_champ) { TypeDeChamp.type_champs.fetch(:text) }
it { is_expected.to have_http_status(:ok) }
# l1, l2, l3 => l1, l1.5, l2, l3
# created: (l1.5, [l1]), morphed: (l2, [l1, l1.5]), (l3, [l1, l1.5, l2])
it do
is_expected.to have_http_status(:ok)
expect(flash.alert).to eq(nil)
expect(assigns(:coordinate)).to eq(second_coordinate)
expect(extract_libelle(assigns(:created))).to eq(['l1.5', ['l1']])
expect(morpheds).to eq([['l2', ['l1', 'l1.5']], ['l3', ['l1', 'l1.5', 'l2']]])
end
end
context "validate type_de_champ linked_drop_down_list" do
@ -45,4 +71,89 @@ describe Administrateurs::TypesDeChampController, type: :controller do
end
end
end
describe '#update' do
let(:params) do
{
procedure_id: procedure.id,
stable_id: second_coordinate.stable_id,
type_de_champ: {
libelle: 'updated'
}
}
end
subject { post :update, params: params, format: :turbo_stream }
# l1, l2, l3 => l1, updated, l3
# morphed: (updated, [l1]), (l3, [l1, updated])
it do
is_expected.to have_http_status(:ok)
expect(flash.alert).to eq(nil)
expect(second_coordinate.libelle).to eq('updated')
expect(assigns(:coordinate)).to eq(second_coordinate)
expect(morpheds).to eq([['updated', ['l1']], ['l3', ['l1', 'updated']]])
end
end
# l1, l2, l3 => l1, l3, l2
# destroyed: l3, created: (l3, [l1]), morphed: (l2, [l1, l3])
describe '#move_up' do
let(:params) do
{ procedure_id: procedure.id, stable_id: third_coordinate.stable_id }
end
subject { patch :move_up, params: params, format: :turbo_stream }
it do
is_expected.to have_http_status(:ok)
expect(flash.alert).to eq(nil)
expect(second_coordinate.libelle).to eq('l3')
expect(assigns(:coordinate)).to eq(second_coordinate)
expect(assigns(:destroyed).libelle).to eq('l3')
expect(extract_libelle(assigns(:created))).to eq(['l3', ['l1']])
expect(morpheds).to eq([['l2', ['l1', 'l3']]])
end
end
# l1, l2, l3 => l2, l1, l3
# destroyed: l1, created: (l1, [l2]), morphed: (l2, [])
describe '#move_down' do
let(:params) do
{ procedure_id: procedure.id, stable_id: first_coordinate.stable_id }
end
subject { patch :move_down, params: params, format: :turbo_stream }
it do
is_expected.to have_http_status(:ok)
expect(flash.alert).to eq(nil)
expect(assigns(:coordinate)).to eq(second_coordinate)
expect(assigns(:destroyed).libelle).to eq('l1')
expect(extract_libelle(assigns(:created))).to eq(['l1', ['l2']])
expect(morpheds).to eq([['l2', []]])
end
end
# l1, l2, l3 => l1, l3
# destroyed: l2, morphed: (l3, [l1])
describe '#destroy' do
let(:params) do
{ procedure_id: procedure.id, stable_id: second_coordinate.stable_id }
end
subject { delete :destroy, params: params, format: :turbo_stream }
it do
used_to_be_second_coordinate = second_coordinate
is_expected.to have_http_status(:ok)
expect(flash.alert).to eq(nil)
expect(assigns(:coordinate)).to eq(used_to_be_second_coordinate)
expect(assigns(:destroyed).libelle).to eq('l2')
expect(morpheds).to eq([['l3', ['l1']]])
end
end
end