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?}",
|
||||
id: @champ.input_group_id,
|
||||
data: { controller: stimulus_controller }
|
||||
data: { controller: stimulus_controller, block: @champ.block? }
|
||||
}
|
||||
end
|
||||
|
||||
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.
|
||||
controllers = []
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
.editable-champ{ html_options }
|
||||
- if @champ.repetition?
|
||||
- if @champ.block?
|
||||
%h3.header-subsection= @champ.libelle
|
||||
- if @champ.description.present?
|
||||
%p.notice= string_to_html(@champ.description, false)
|
||||
|
@ -9,5 +9,5 @@
|
|||
- 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
|
||||
|
||||
= @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)
|
||||
|
|
|
@ -71,7 +71,7 @@
|
|||
= 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}")
|
||||
= t(".layers.#{name}")
|
||||
- if type_de_champ.repetition?
|
||||
- if type_de_champ.block?
|
||||
.flex.justify-start.section.ml-1
|
||||
.editor-block.flex-grow.cell
|
||||
= render TypesDeChampEditor::BlockComponent.new(block: coordinate, coordinates: coordinate.revision_types_de_champ)
|
||||
|
|
|
@ -428,7 +428,7 @@ module Users
|
|||
@dossier.assign_attributes(champs_params[:dossier])
|
||||
# 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.
|
||||
@dossier.champs.filter(&:repetition?).each do |champ|
|
||||
@dossier.champs.filter(&:block?).each do |champ|
|
||||
champ.champs = champ.champs.filter(&:persisted?)
|
||||
end
|
||||
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
|
||||
|
||||
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)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -200,7 +200,7 @@ export class AutosaveController extends ApplicationController {
|
|||
)
|
||||
].filter((element) => !element.disabled);
|
||||
|
||||
const parent = this.element.closest('.editable-champ-repetition');
|
||||
const parent = this.element.closest('[data-block]');
|
||||
if (parent) {
|
||||
return [
|
||||
...inputs,
|
||||
|
|
|
@ -45,6 +45,7 @@ class Champ < ApplicationRecord
|
|||
:exclude_from_export?,
|
||||
:exclude_from_view?,
|
||||
:repetition?,
|
||||
:block?,
|
||||
:dossier_link?,
|
||||
:titre_identite?,
|
||||
:header_section?,
|
||||
|
|
|
@ -984,7 +984,7 @@ class Dossier < ApplicationRecord
|
|||
end
|
||||
|
||||
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?)
|
||||
.map do |champ|
|
||||
"Le champ #{champ.libelle.truncate(200)} doit être rempli."
|
||||
|
|
|
@ -87,7 +87,7 @@ class DossierPreloader
|
|||
end
|
||||
|
||||
# Load children champs
|
||||
champs.filter(&:repetition?).each do |parent_champ|
|
||||
champs.filter(&:block?).each do |parent_champ|
|
||||
champs = children_by_parent[parent_champ.id] || []
|
||||
parent_champ.association(:dossier).target = dossier
|
||||
|
||||
|
|
|
@ -261,11 +261,11 @@ class Procedure < ApplicationRecord
|
|||
validates :administrateurs, presence: true
|
||||
validates :lien_site_web, presence: true, if: :publiee?
|
||||
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,
|
||||
if: :validate_for_publication?
|
||||
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,
|
||||
if: :validate_for_publication?
|
||||
validate :check_juridique
|
||||
|
|
|
@ -132,7 +132,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
before_validation :check_mandatory
|
||||
before_save :remove_piece_justificative_template, 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
|
||||
piece_justificative_template.purge_later
|
||||
|
@ -220,6 +220,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
|
||||
end
|
||||
|
||||
def block?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:repetition)
|
||||
end
|
||||
|
||||
def header_section?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:header_section)
|
||||
end
|
||||
|
@ -405,8 +409,8 @@ class TypeDeChamp < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def remove_repetition
|
||||
if !repetition? && procedure.present?
|
||||
def remove_block
|
||||
if !block? && procedure.present?
|
||||
procedure
|
||||
.draft_revision # action occurs only on draft
|
||||
.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