diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 1ab8c6b26..8f20fad7b 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -3,6 +3,7 @@ # Table name: types_de_champ # # id :integer not null, primary key +# condition :jsonb # description :text # libelle :string # mandatory :boolean default(FALSE) @@ -77,6 +78,22 @@ class TypeDeChamp < ApplicationRecord serialize :options, WithIndifferentAccess + class ConditionSerializer + def self.load(condition) + if condition.present? + Logic.from_h(condition) + end + end + + def self.dump(condition) + if condition.present? + condition.to_h + end + end + end + + serialize :condition, ConditionSerializer + after_initialize :set_dynamic_type after_create :populate_stable_id diff --git a/db/migrate/20220531100040_add_condition_column_to_type_de_champ.rb b/db/migrate/20220531100040_add_condition_column_to_type_de_champ.rb new file mode 100644 index 000000000..a7bf9f255 --- /dev/null +++ b/db/migrate/20220531100040_add_condition_column_to_type_de_champ.rb @@ -0,0 +1,5 @@ +class AddConditionColumnToTypeDeChamp < ActiveRecord::Migration[6.1] + def change + add_column :types_de_champ, :condition, :jsonb + end +end diff --git a/db/schema.rb b/db/schema.rb index 75fa2e458..727ce85b3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_05_20_173939) do +ActiveRecord::Schema.define(version: 2022_05_31_100040) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -787,6 +787,7 @@ ActiveRecord::Schema.define(version: 2022_05_20_173939) do end create_table "types_de_champ", id: :serial, force: :cascade do |t| + t.jsonb "condition" t.datetime "created_at" t.text "description" t.string "libelle" diff --git a/spec/models/type_de_champ_spec.rb b/spec/models/type_de_champ_spec.rb index a1d3ff9fc..145517153 100644 --- a/spec/models/type_de_champ_spec.rb +++ b/spec/models/type_de_champ_spec.rb @@ -11,4 +11,15 @@ describe TypeDeChamp do expect(procedure.types_de_champ_private.count).to eq(1) end end + + describe 'condition' do + let(:type_de_champ) { create(:type_de_champ) } + let(:condition) { Logic::Eq.new(Logic::Constant.new(true), Logic::Constant.new(true)) } + + it 'saves and reload the condition' do + type_de_champ.update(condition: condition) + type_de_champ.reload + expect(type_de_champ.condition).to eq(condition) + end + end end