Store drop_down_list values as jsonb
This commit is contained in:
parent
29d3d58645
commit
97f91513c8
19 changed files with 184 additions and 133 deletions
|
@ -11,7 +11,22 @@ class Champ < ApplicationRecord
|
|||
belongs_to :etablissement, dependent: :destroy
|
||||
has_many :champs, -> { ordered }, foreign_key: :parent_id, inverse_of: :parent, dependent: :destroy
|
||||
|
||||
delegate :libelle, :type_champ, :procedure, :order_place, :mandatory?, :description, :drop_down_list, :exclude_from_export?, :exclude_from_view?, :repetition?, :dossier_link?, to: :type_de_champ
|
||||
delegate :libelle,
|
||||
:type_champ,
|
||||
:procedure,
|
||||
:order_place,
|
||||
:mandatory?,
|
||||
:description,
|
||||
:drop_down_list,
|
||||
:drop_down_list_options,
|
||||
:drop_down_list_options?,
|
||||
:drop_down_list_disabled_options,
|
||||
:drop_down_list_enabled_non_empty_options,
|
||||
:exclude_from_export?,
|
||||
:exclude_from_view?,
|
||||
:repetition?,
|
||||
:dossier_link?,
|
||||
to: :type_de_champ
|
||||
|
||||
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
|
||||
scope :public_only, -> { where(private: false) }
|
||||
|
|
|
@ -2,6 +2,22 @@ class Champs::DropDownListChamp < Champ
|
|||
THRESHOLD_NB_OPTIONS_AS_RADIO = 5
|
||||
|
||||
def render_as_radios?
|
||||
drop_down_list.enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
|
||||
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_RADIO
|
||||
end
|
||||
|
||||
def options?
|
||||
drop_down_list_options?
|
||||
end
|
||||
|
||||
def options
|
||||
drop_down_list_options
|
||||
end
|
||||
|
||||
def disabled_options
|
||||
drop_down_list_disabled_options
|
||||
end
|
||||
|
||||
def enabled_non_empty_options
|
||||
drop_down_list_enabled_non_empty_options
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
class Champs::LinkedDropDownListChamp < Champ
|
||||
delegate :primary_options, :secondary_options, to: 'type_de_champ.dynamic_type'
|
||||
|
||||
def options?
|
||||
drop_down_list_options?
|
||||
end
|
||||
|
||||
def primary_value
|
||||
if value.present?
|
||||
JSON.parse(value)[0]
|
||||
|
|
|
@ -1,6 +1,22 @@
|
|||
class Champs::MultipleDropDownListChamp < Champ
|
||||
before_save :format_before_save
|
||||
|
||||
def options?
|
||||
drop_down_list_options?
|
||||
end
|
||||
|
||||
def options
|
||||
drop_down_list_options
|
||||
end
|
||||
|
||||
def disabled_options
|
||||
drop_down_list_disabled_options
|
||||
end
|
||||
|
||||
def enabled_non_empty_options
|
||||
drop_down_list_enabled_non_empty_options
|
||||
end
|
||||
|
||||
THRESHOLD_NB_OPTIONS_AS_CHECKBOX = 5
|
||||
|
||||
def search_terms
|
||||
|
@ -24,7 +40,7 @@ class Champs::MultipleDropDownListChamp < Champ
|
|||
end
|
||||
|
||||
def render_as_checkboxes?
|
||||
drop_down_list.enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
|
||||
enabled_non_empty_options.size <= THRESHOLD_NB_OPTIONS_AS_CHECKBOX
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,30 +1,8 @@
|
|||
class DropDownList < ApplicationRecord
|
||||
belongs_to :type_de_champ
|
||||
|
||||
before_validation :clean_value
|
||||
|
||||
def options
|
||||
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
|
||||
result.blank? ? [] : [''] + result
|
||||
end
|
||||
|
||||
def enabled_non_empty_options
|
||||
(options - disabled_options).reject(&:empty?)
|
||||
end
|
||||
|
||||
def disabled_options
|
||||
options.filter { |v| (v =~ /^--.*--$/).present? }
|
||||
end
|
||||
|
||||
def multiple
|
||||
type_de_champ.type_champ == TypeDeChamp.type_champs.fetch(:multiple_drop_down_list)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_value
|
||||
value = read_attribute(:value)
|
||||
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
|
||||
write_attribute(:value, value)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -35,7 +35,7 @@ class TypeDeChamp < ApplicationRecord
|
|||
belongs_to :parent, class_name: 'TypeDeChamp'
|
||||
has_many :types_de_champ, -> { ordered }, foreign_key: :parent_id, class_name: 'TypeDeChamp', inverse_of: :parent, dependent: :destroy
|
||||
|
||||
store_accessor :options, :cadastres, :quartiers_prioritaires, :parcelles_agricoles, :old_pj
|
||||
store_accessor :options, :cadastres, :quartiers_prioritaires, :parcelles_agricoles, :old_pj, :drop_down_options
|
||||
delegate :tags_for_template, to: :dynamic_type
|
||||
|
||||
class WithIndifferentAccess
|
||||
|
@ -53,7 +53,6 @@ class TypeDeChamp < ApplicationRecord
|
|||
after_initialize :set_dynamic_type
|
||||
after_create :populate_stable_id
|
||||
before_save :setup_procedure
|
||||
before_validation :set_default_drop_down_list
|
||||
|
||||
attr_reader :dynamic_type
|
||||
|
||||
|
@ -83,6 +82,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? }
|
||||
|
||||
def valid?(context = nil)
|
||||
super
|
||||
|
@ -146,6 +146,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
])
|
||||
end
|
||||
|
||||
def linked_drop_down_list?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
|
||||
end
|
||||
|
||||
def exclude_from_view?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:explication)
|
||||
end
|
||||
|
@ -158,6 +162,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
type_champ == TypeDeChamp.type_champs.fetch(:dossier_link)
|
||||
end
|
||||
|
||||
def piece_justificative?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:piece_justificative)
|
||||
end
|
||||
|
||||
def legacy_number?
|
||||
type_champ == TypeDeChamp.type_champs.fetch(:number)
|
||||
end
|
||||
|
@ -183,11 +191,31 @@ class TypeDeChamp < ApplicationRecord
|
|||
end
|
||||
|
||||
def drop_down_list_value
|
||||
drop_down_list&.value
|
||||
if drop_down_list_options.present?
|
||||
drop_down_list_options.reject(&:empty?).join("\r\n")
|
||||
else
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
def drop_down_list_value=(value)
|
||||
self.drop_down_list_attributes = { value: value }
|
||||
self.drop_down_options = parse_drop_down_list_value(value)
|
||||
end
|
||||
|
||||
def drop_down_list_options?
|
||||
drop_down_list_options.any?
|
||||
end
|
||||
|
||||
def drop_down_list_options
|
||||
drop_down_options.presence || drop_down_list&.options || []
|
||||
end
|
||||
|
||||
def drop_down_list_disabled_options
|
||||
drop_down_list_options.filter { |v| (v =~ /^--.*--$/).present? }
|
||||
end
|
||||
|
||||
def drop_down_list_enabled_non_empty_options
|
||||
(drop_down_list_options - drop_down_list_disabled_options).reject(&:empty?)
|
||||
end
|
||||
|
||||
def to_typed_id
|
||||
|
@ -244,10 +272,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
private
|
||||
|
||||
def set_default_drop_down_list
|
||||
if drop_down_list? && !drop_down_list
|
||||
self.drop_down_list_attributes = { value: '' }
|
||||
end
|
||||
def parse_drop_down_list_value(value)
|
||||
value = value ? value.split("\r\n").map(&:strip).join("\r\n") : ''
|
||||
result = value.split(/[\r\n]|[\r]|[\n]|[\n\r]/).reject(&:empty?)
|
||||
result.blank? ? [] : [''] + result
|
||||
end
|
||||
|
||||
def setup_procedure
|
||||
|
@ -263,8 +291,15 @@ class TypeDeChamp < ApplicationRecord
|
|||
end
|
||||
|
||||
def remove_piece_justificative_template
|
||||
if type_champ != TypeDeChamp.type_champs.fetch(:piece_justificative) && piece_justificative_template.attached?
|
||||
if !piece_justificative? && piece_justificative_template.attached?
|
||||
piece_justificative_template.purge_later
|
||||
end
|
||||
end
|
||||
|
||||
def remove_drop_down_list
|
||||
if !drop_down_list?
|
||||
self.drop_down_list = nil
|
||||
self.drop_down_options = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,26 +1,9 @@
|
|||
class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
PRIMARY_PATTERN = /^--(.*)--$/
|
||||
|
||||
delegate :drop_down_list, to: :@type_de_champ
|
||||
|
||||
delegate :drop_down_list_options, to: :@type_de_champ
|
||||
validate :check_presence_of_primary_options
|
||||
|
||||
def primary_options
|
||||
primary_options = unpack_options.map(&:first)
|
||||
if primary_options.present?
|
||||
primary_options.unshift('')
|
||||
end
|
||||
primary_options
|
||||
end
|
||||
|
||||
def secondary_options
|
||||
secondary_options = unpack_options.to_h
|
||||
if secondary_options.present?
|
||||
secondary_options[''] = []
|
||||
end
|
||||
secondary_options
|
||||
end
|
||||
|
||||
def tags_for_template
|
||||
tags = super
|
||||
tdc = @type_de_champ
|
||||
|
@ -49,16 +32,26 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
|||
tags
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def check_presence_of_primary_options
|
||||
if !PRIMARY_PATTERN.match?(drop_down_list.options.second)
|
||||
errors.add(libelle.presence || "La liste", "doit commencer par une entrée de menu primaire de la forme <code style='white-space: pre-wrap;'>--texte--</code>")
|
||||
def primary_options
|
||||
primary_options = unpack_options.map(&:first)
|
||||
if primary_options.present?
|
||||
primary_options.unshift('')
|
||||
end
|
||||
primary_options
|
||||
end
|
||||
|
||||
def secondary_options
|
||||
secondary_options = unpack_options.to_h
|
||||
if secondary_options.present?
|
||||
secondary_options[''] = []
|
||||
end
|
||||
secondary_options
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unpack_options
|
||||
_, *options = drop_down_list.options
|
||||
_, *options = drop_down_list_options
|
||||
chunked = options.slice_before(PRIMARY_PATTERN)
|
||||
chunked.map do |chunk|
|
||||
primary, *secondary = chunk
|
||||
|
@ -66,4 +59,10 @@ class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBas
|
|||
[PRIMARY_PATTERN.match(primary)&.[](1), secondary]
|
||||
end
|
||||
end
|
||||
|
||||
def check_presence_of_primary_options
|
||||
if !PRIMARY_PATTERN.match?(drop_down_list_options.second)
|
||||
errors.add(libelle.presence || "La liste", "doit commencer par une entrée de menu primaire de la forme <code style='white-space: pre-wrap;'>--texte--</code>")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue