[#1421] Use delegation rather than STI for TypeDeChamp
This commit is contained in:
parent
4bb00e9875
commit
9beaa293cd
26 changed files with 82 additions and 65 deletions
|
@ -1,5 +1,5 @@
|
|||
class Champs::LinkedDropDownListChamp < Champ
|
||||
delegate :primary_options, :secondary_options, to: :type_de_champ
|
||||
delegate :primary_options, :secondary_options, to: 'type_de_champ.dynamic_type'
|
||||
|
||||
def primary_value
|
||||
if value.present?
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
class TypeDeChamp < ApplicationRecord
|
||||
# TODO drop next line when `type` column has been dropped from `types_de_champ` table
|
||||
self.inheritance_column = nil
|
||||
|
||||
enum type_champs: {
|
||||
text: 'text',
|
||||
textarea: 'textarea',
|
||||
|
@ -27,6 +30,10 @@ class TypeDeChamp < ApplicationRecord
|
|||
|
||||
belongs_to :procedure
|
||||
|
||||
after_initialize :set_dynamic_type
|
||||
|
||||
attr_reader :dynamic_type
|
||||
|
||||
scope :public_only, -> { where(private: false) }
|
||||
scope :private_only, -> { where(private: true) }
|
||||
scope :ordered, -> { order(order_place: :asc) }
|
||||
|
@ -52,6 +59,15 @@ class TypeDeChamp < ApplicationRecord
|
|||
before_validation :check_mandatory
|
||||
before_save :remove_piece_justificative_template, if: -> { type_champ_changed? }
|
||||
|
||||
def set_dynamic_type
|
||||
@dynamic_type = type_champ.present? ? self.class.type_champ_to_class_name(type_champ).constantize.new(self) : nil
|
||||
end
|
||||
|
||||
def type_champ=(value)
|
||||
super(value)
|
||||
set_dynamic_type
|
||||
end
|
||||
|
||||
def params_for_champ
|
||||
{
|
||||
private: private?,
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::CheckboxTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::CheckboxTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::CiviliteTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::CiviliteTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::DateTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::DateTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::DatetimeTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::DatetimeTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::DossierLinkTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::DossierLinkTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::DropDownListTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::DropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::HeaderSectionTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::HeaderSectionTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::LinkedDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
PRIMARY_PATTERN = /^--(.*)--$/
|
||||
|
||||
delegate :drop_down_list, to: :@type_de_champ
|
||||
|
||||
def primary_options
|
||||
primary_options = unpack_options.map(&:first)
|
||||
if primary_options.present?
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::MultipleDropDownListTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::NumberTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::NumberTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::PieceJustificativeTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::PieceJustificativeTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::SiretTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::SiretTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
class TypesDeChamp::TextTypeDeChamp < TypeDeChamp
|
||||
class TypesDeChamp::TextTypeDeChamp < TypesDeChamp::TypeDeChampBase
|
||||
end
|
||||
|
|
5
app/models/types_de_champ/type_de_champ_base.rb
Normal file
5
app/models/types_de_champ/type_de_champ_base.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class TypesDeChamp::TypeDeChampBase
|
||||
def initialize(type_de_champ)
|
||||
@type_de_champ = type_de_champ
|
||||
end
|
||||
end
|
|
@ -19,10 +19,6 @@ class TypesDeChampService
|
|||
|
||||
parameters[attributes].each do |index, param|
|
||||
param[:private] = private
|
||||
if param[:type_champ]
|
||||
param[:type] = TypeDeChamp.type_champ_to_class_name(param[:type_champ])
|
||||
end
|
||||
|
||||
if param[:libelle].empty?
|
||||
parameters[attributes].delete(index.to_s)
|
||||
end
|
||||
|
|
|
@ -46,7 +46,6 @@
|
|||
|
||||
.form-group
|
||||
= ff.hidden_field :order_place, value: ff.index
|
||||
= ff.hidden_field :type
|
||||
= ff.hidden_field :id
|
||||
|
||||
- if ff.object.id.present?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue