Merge pull request #7595 from tchak/refactor-blocks
refactor(block): use block? instead of repetition?
This commit is contained in:
commit
615c8c370f
13 changed files with 38 additions and 33 deletions
|
@ -20,12 +20,12 @@ class EditableChamp::EditableChampComponent < ApplicationComponent
|
||||||
{
|
{
|
||||||
class: "editable-champ-#{@champ.type_champ} #{'hidden' if !@champ.visible?}",
|
class: "editable-champ-#{@champ.type_champ} #{'hidden' if !@champ.visible?}",
|
||||||
id: @champ.input_group_id,
|
id: @champ.input_group_id,
|
||||||
data: { controller: stimulus_controller }
|
data: { controller: stimulus_controller, block: @champ.block? }
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def stimulus_controller
|
def stimulus_controller
|
||||||
if !@champ.repetition? && @champ.fillable?
|
if !@champ.block? && @champ.fillable?
|
||||||
# This is an editable champ. Lets find what controllers it might need.
|
# This is an editable champ. Lets find what controllers it might need.
|
||||||
controllers = []
|
controllers = []
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
.editable-champ{ html_options }
|
.editable-champ{ html_options }
|
||||||
- if @champ.repetition?
|
- if @champ.block?
|
||||||
%h3.header-subsection= @champ.libelle
|
%h3.header-subsection= @champ.libelle
|
||||||
- if @champ.description.present?
|
- if @champ.description.present?
|
||||||
%p.notice= string_to_html(@champ.description, false)
|
%p.notice= string_to_html(@champ.description, false)
|
||||||
|
@ -9,5 +9,5 @@
|
||||||
- if @champ.type_champ == "titre_identite"
|
- if @champ.type_champ == "titre_identite"
|
||||||
%p.notice Carte nationale d’identité (uniquement le recto), passeport, titre de séjour ou autre justificatif d’identité. Formats acceptés : jpg/png
|
%p.notice Carte nationale d’identité (uniquement le recto), passeport, titre de séjour ou autre justificatif d’identité. Formats acceptés : jpg/png
|
||||||
|
|
||||||
= @form.hidden_field :id, value: @champ.id, data: @champ.repetition? ? { id: true } : {}
|
= @form.hidden_field :id, value: @champ.id, data: @champ.block? ? { id: true } : {}
|
||||||
= render component_class.new(form: @form, champ: @champ, seen_at: @seen_at)
|
= render component_class.new(form: @form, champ: @champ, seen_at: @seen_at)
|
||||||
|
|
|
@ -71,7 +71,7 @@
|
||||||
= form.label name, for: dom_id(type_de_champ, "layer_#{name}") do
|
= form.label name, for: dom_id(type_de_champ, "layer_#{name}") do
|
||||||
= form.check_box name, checked: checked, class: 'small-margin small', id: dom_id(type_de_champ, "layer_#{name}")
|
= form.check_box name, checked: checked, class: 'small-margin small', id: dom_id(type_de_champ, "layer_#{name}")
|
||||||
= t(".layers.#{name}")
|
= t(".layers.#{name}")
|
||||||
- if type_de_champ.repetition?
|
- if type_de_champ.block?
|
||||||
.flex.justify-start.section.ml-1
|
.flex.justify-start.section.ml-1
|
||||||
.editor-block.flex-grow.cell
|
.editor-block.flex-grow.cell
|
||||||
= render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ)
|
= render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ)
|
||||||
|
|
|
@ -428,7 +428,7 @@ module Users
|
||||||
@dossier.assign_attributes(champs_params[:dossier])
|
@dossier.assign_attributes(champs_params[:dossier])
|
||||||
# FIXME: in some cases a removed repetition bloc row is submitted.
|
# FIXME: in some cases a removed repetition bloc row is submitted.
|
||||||
# In this case it will be treated as a new record, and the action will fail.
|
# In this case it will be treated as a new record, and the action will fail.
|
||||||
@dossier.champs.filter(&:repetition?).each do |champ|
|
@dossier.champs.filter(&:block?).each do |champ|
|
||||||
champ.champs = champ.champs.filter(&:persisted?)
|
champ.champs = champ.champs.filter(&:persisted?)
|
||||||
end
|
end
|
||||||
if @dossier.champs.any?(&:changed_for_autosave?)
|
if @dossier.champs.any?(&:changed_for_autosave?)
|
||||||
|
|
|
@ -18,7 +18,7 @@ module Types
|
||||||
field :options, [String], "List des options d’un champ avec selection.", null: true
|
field :options, [String], "List des options d’un champ avec selection.", null: true
|
||||||
|
|
||||||
def champ_descriptors
|
def champ_descriptors
|
||||||
if object.type_de_champ.repetition?
|
if object.type_de_champ.block?
|
||||||
Loaders::Association.for(object.class, revision_types_de_champ: :type_de_champ).load(object)
|
Loaders::Association.for(object.class, revision_types_de_champ: :type_de_champ).load(object)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -200,7 +200,7 @@ export class AutosaveController extends ApplicationController {
|
||||||
)
|
)
|
||||||
].filter((element) => !element.disabled);
|
].filter((element) => !element.disabled);
|
||||||
|
|
||||||
const parent = this.element.closest('.editable-champ-repetition');
|
const parent = this.element.closest('[data-block]');
|
||||||
if (parent) {
|
if (parent) {
|
||||||
return [
|
return [
|
||||||
...inputs,
|
...inputs,
|
||||||
|
|
|
@ -45,6 +45,7 @@ class Champ < ApplicationRecord
|
||||||
:exclude_from_export?,
|
:exclude_from_export?,
|
||||||
:exclude_from_view?,
|
:exclude_from_view?,
|
||||||
:repetition?,
|
:repetition?,
|
||||||
|
:block?,
|
||||||
:dossier_link?,
|
:dossier_link?,
|
||||||
:titre_identite?,
|
:titre_identite?,
|
||||||
:header_section?,
|
:header_section?,
|
||||||
|
|
|
@ -984,7 +984,7 @@ class Dossier < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_mandatory_champs
|
def check_mandatory_champs
|
||||||
(champs + champs.filter(&:repetition?).filter(&:visible?).flat_map(&:champs))
|
(champs + champs.filter(&:block?).filter(&:visible?).flat_map(&:champs))
|
||||||
.filter(&:mandatory_blank_and_visible?)
|
.filter(&:mandatory_blank_and_visible?)
|
||||||
.map do |champ|
|
.map do |champ|
|
||||||
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
|
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
|
||||||
|
|
|
@ -87,7 +87,7 @@ class DossierPreloader
|
||||||
end
|
end
|
||||||
|
|
||||||
# Load children champs
|
# Load children champs
|
||||||
champs.filter(&:repetition?).each do |parent_champ|
|
champs.filter(&:block?).each do |parent_champ|
|
||||||
champs = children_by_parent[parent_champ.id] || []
|
champs = children_by_parent[parent_champ.id] || []
|
||||||
parent_champ.association(:dossier).target = dossier
|
parent_champ.association(:dossier).target = dossier
|
||||||
|
|
||||||
|
|
|
@ -261,11 +261,11 @@ class Procedure < ApplicationRecord
|
||||||
validates :administrateurs, presence: true
|
validates :administrateurs, presence: true
|
||||||
validates :lien_site_web, presence: true, if: :publiee?
|
validates :lien_site_web, presence: true, if: :publiee?
|
||||||
validates :draft_types_de_champ,
|
validates :draft_types_de_champ,
|
||||||
'types_de_champ/no_empty_repetition': true,
|
'types_de_champ/no_empty_block': true,
|
||||||
'types_de_champ/no_empty_drop_down': true,
|
'types_de_champ/no_empty_drop_down': true,
|
||||||
if: :validate_for_publication?
|
if: :validate_for_publication?
|
||||||
validates :draft_types_de_champ_private,
|
validates :draft_types_de_champ_private,
|
||||||
'types_de_champ/no_empty_repetition': true,
|
'types_de_champ/no_empty_block': true,
|
||||||
'types_de_champ/no_empty_drop_down': true,
|
'types_de_champ/no_empty_drop_down': true,
|
||||||
if: :validate_for_publication?
|
if: :validate_for_publication?
|
||||||
validate :check_juridique
|
validate :check_juridique
|
||||||
|
|
|
@ -132,7 +132,7 @@ class TypeDeChamp < ApplicationRecord
|
||||||
before_validation :check_mandatory
|
before_validation :check_mandatory
|
||||||
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
||||||
before_validation :remove_drop_down_list, if: -> { type_champ_changed? }
|
before_validation :remove_drop_down_list, if: -> { type_champ_changed? }
|
||||||
before_save :remove_repetition, if: -> { type_champ_changed? }
|
before_save :remove_block, if: -> { type_champ_changed? }
|
||||||
|
|
||||||
after_save if: -> { @remove_piece_justificative_template } do
|
after_save if: -> { @remove_piece_justificative_template } do
|
||||||
piece_justificative_template.purge_later
|
piece_justificative_template.purge_later
|
||||||
|
@ -220,6 +220,10 @@ class TypeDeChamp < ApplicationRecord
|
||||||
type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
|
type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def block?
|
||||||
|
type_champ == TypeDeChamp.type_champs.fetch(:repetition)
|
||||||
|
end
|
||||||
|
|
||||||
def header_section?
|
def header_section?
|
||||||
type_champ == TypeDeChamp.type_champs.fetch(:header_section)
|
type_champ == TypeDeChamp.type_champs.fetch(:header_section)
|
||||||
end
|
end
|
||||||
|
@ -405,8 +409,8 @@ class TypeDeChamp < ApplicationRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_repetition
|
def remove_block
|
||||||
if !repetition? && procedure.present?
|
if !block? && procedure.present?
|
||||||
procedure
|
procedure
|
||||||
.draft_revision # action occurs only on draft
|
.draft_revision # action occurs only on draft
|
||||||
.remove_children_of(self)
|
.remove_children_of(self)
|
||||||
|
|
18
app/validators/types_de_champ/no_empty_block_validator.rb
Normal file
18
app/validators/types_de_champ/no_empty_block_validator.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
class TypesDeChamp::NoEmptyBlockValidator < ActiveModel::EachValidator
|
||||||
|
def validate_each(procedure, attribute, types_de_champ)
|
||||||
|
types_de_champ.filter(&:block?).each do |repetition|
|
||||||
|
validate_block_not_empty(procedure, attribute, repetition)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def validate_block_not_empty(procedure, attribute, parent)
|
||||||
|
if procedure.draft_revision.children_of(parent).empty?
|
||||||
|
procedure.errors.add(
|
||||||
|
attribute,
|
||||||
|
procedure.errors.generate_message(attribute, :empty_repetition, { value: parent.libelle })
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,18 +0,0 @@
|
||||||
class TypesDeChamp::NoEmptyRepetitionValidator < ActiveModel::EachValidator
|
|
||||||
def validate_each(procedure, attribute, types_de_champ)
|
|
||||||
types_de_champ.filter(&:repetition?).each do |repetition|
|
|
||||||
validate_repetition_not_empty(procedure, attribute, repetition)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def validate_repetition_not_empty(procedure, attribute, repetition)
|
|
||||||
if procedure.draft_revision.children_of(repetition).empty?
|
|
||||||
procedure.errors.add(
|
|
||||||
attribute,
|
|
||||||
procedure.errors.generate_message(attribute, :empty_repetition, { value: repetition.libelle })
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in a new issue