feat(dossier): implement refresh_after_update on champs

This commit is contained in:
Paul Chavard 2023-01-17 15:28:41 +01:00
parent 60e3ebd7c7
commit f38ee8f03f
5 changed files with 41 additions and 12 deletions

View file

@ -60,6 +60,10 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent
TypeDeChamp.type_champs
.keys
# FIXME
# We can only refresh after update champs when autosave is enabled. And it is disabled for now in private forms.
# So for new we restrict champs that require refresh after update to public forms.
.filter { type_de_champ.public? || !TypeDeChamp.refresh_after_update?(_1) }
.filter(&method(:filter_type_champ))
.filter(&method(:filter_featured_type_champ))
.filter(&method(:filter_block_type_champ))

View file

@ -201,10 +201,7 @@ module Users
respond_to do |format|
format.html { render :brouillon }
format.turbo_stream do
@to_shows, @to_hides = @dossier.champs_public_all
.filter(&:conditional?)
.partition(&:visible?)
.map { |champs| champs_to_one_selector(champs) }
@to_show, @to_hide, @to_update = champs_to_turbo_update
render(:update, layout: false)
end
@ -222,10 +219,7 @@ module Users
respond_to do |format|
format.html { render :modifier }
format.turbo_stream do
@to_shows, @to_hides = @dossier.champs_public_all
.filter(&:conditional?)
.partition(&:visible?)
.map { |champs| champs_to_one_selector(champs) }
@to_show, @to_hide, @to_update = champs_to_turbo_update
end
end
end
@ -493,6 +487,24 @@ module Users
errors
end
def champs_to_turbo_update
champ_ids = champs_public_params
.fetch(:champs_public_all_attributes)
.keys
.map(&:to_i)
to_update = dossier
.champs_public_all
.filter { _1.id.in?(champ_ids) && _1.refresh_after_update? }
to_show, to_hide = dossier
.champs_public_all
.filter(&:conditional?)
.partition(&:visible?)
.map { champs_to_one_selector(_1 - to_update) }
return to_show, to_hide, to_update
end
def ensure_ownership!
if !current_user.owns?(dossier)
forbidden!

View file

@ -67,6 +67,7 @@ class Champ < ApplicationRecord
:stable_id,
:mandatory?,
:prefillable?,
:refresh_after_update?,
to: :type_de_champ
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }

View file

@ -469,6 +469,14 @@ class TypeDeChamp < ApplicationRecord
model_name: OpenStruct.new(param_key: model_name.param_key))
end
def refresh_after_update?
self.class.refresh_after_update?(type_champ)
end
def self.refresh_after_update?(_type_champ)
false
end
private
DEFAULT_EMPTY = ['']

View file

@ -1,4 +1,8 @@
- if @to_shows.present?
= turbo_stream.show_all(@to_shows)
- if @to_hides.present?
= turbo_stream.hide_all(@to_hides)
- if @to_show.present?
= turbo_stream.show_all(@to_show)
- if @to_hide.present?
= turbo_stream.hide_all(@to_hide)
- @to_update.each do |champ|
= fields_for champ.input_name, champ do |form|
= turbo_stream.morph champ.input_group_id do
= render EditableChamp::EditableChampComponent.new champ:, form: