From 31d638ae2ad6d48d2ac27c1fc5aa2eedf99c5268 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Tue, 13 Feb 2018 15:38:26 +0100 Subject: [PATCH] Remove TypeDeChampPublic/TypeDeChampPrivate STI --- app/controllers/root_controller.rb | 2 +- app/facades/admin_types_de_champ_facades.rb | 2 +- app/models/procedure.rb | 4 +- app/models/type_de_champ.rb | 7 ++- app/models/type_de_champ_private.rb | 2 - app/models/type_de_champ_public.rb | 2 - app/services/types_de_champ_service.rb | 12 ++--- .../admin/types_de_champ_controller_spec.rb | 3 +- .../types_de_champ_private_controller_spec.rb | 6 +-- spec/factories/type_de_champ_private.rb | 3 +- spec/factories/type_de_champ_public.rb | 3 +- spec/models/type_de_champ_private_spec.rb | 9 ++-- spec/models/type_de_champ_public_spec.rb | 7 --- spec/services/types_de_champ_service_spec.rb | 53 ++++++++++--------- 14 files changed, 58 insertions(+), 57 deletions(-) delete mode 100644 app/models/type_de_champ_private.rb delete mode 100644 app/models/type_de_champ_public.rb delete mode 100644 spec/models/type_de_champ_public_spec.rb diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index febfb25d1..3d16871fa 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -19,7 +19,7 @@ class RootController < ApplicationController description = 'a not so long description' all_champs = TypeDeChamp.type_champs - .map { |name, _| TypeDeChamp.new(type_champ: name, libelle: name, description: description, mandatory: true) } + .map { |name, _| TypeDeChamp.new(type_champ: name, private: false, libelle: name, description: description, mandatory: true) } .map.with_index { |type_de_champ, i| type_de_champ.champ.build(id: i) } all_champs diff --git a/app/facades/admin_types_de_champ_facades.rb b/app/facades/admin_types_de_champ_facades.rb index a8f7fe578..eefe7e57c 100644 --- a/app/facades/admin_types_de_champ_facades.rb +++ b/app/facades/admin_types_de_champ_facades.rb @@ -23,7 +23,7 @@ class AdminTypesDeChampFacades end def new_type_de_champ - @private ? TypeDeChampPrivate.new.decorate : TypeDeChampPublic.new.decorate + TypeDeChamp.new(private: @private).decorate end def fields_for_var diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 7cf0c3d67..5441fbc3d 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -1,7 +1,7 @@ class Procedure < ActiveRecord::Base has_many :types_de_piece_justificative, -> { order "order_place ASC" }, dependent: :destroy - has_many :types_de_champ, class_name: 'TypeDeChampPublic', dependent: :destroy - has_many :types_de_champ_private, dependent: :destroy + has_many :types_de_champ, -> { public_only }, dependent: :destroy + has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy has_many :dossiers has_one :procedure_path, dependent: :destroy diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 6df25d6a0..f6bf5a36d 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -1,4 +1,6 @@ class TypeDeChamp < ActiveRecord::Base + self.inheritance_column = :_type_disabled + enum type_champs: { text: 'text', textarea: 'textarea', @@ -24,6 +26,9 @@ class TypeDeChamp < ActiveRecord::Base belongs_to :procedure + scope :public_only, -> { where.not(type: 'TypeDeChampPrivate').or(where(private: [false, nil])) } + scope :private_only, -> { where(type: 'TypeDeChampPrivate').or(where(private: true)) } + has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do def build(params = {}) super(params.merge(proxy_association.owner.params_for_champ)) @@ -65,7 +70,7 @@ class TypeDeChamp < ActiveRecord::Base end def private? - type == 'TypeDeChampPrivate' + super || type == 'TypeDeChampPrivate' end def public? diff --git a/app/models/type_de_champ_private.rb b/app/models/type_de_champ_private.rb deleted file mode 100644 index ba6e89a82..000000000 --- a/app/models/type_de_champ_private.rb +++ /dev/null @@ -1,2 +0,0 @@ -class TypeDeChampPrivate < TypeDeChamp -end diff --git a/app/models/type_de_champ_public.rb b/app/models/type_de_champ_public.rb deleted file mode 100644 index 3be990e13..000000000 --- a/app/models/type_de_champ_public.rb +++ /dev/null @@ -1,2 +0,0 @@ -class TypeDeChampPublic < TypeDeChamp -end diff --git a/app/services/types_de_champ_service.rb b/app/services/types_de_champ_service.rb index 7f8cfe1e9..d4eb53a9b 100644 --- a/app/services/types_de_champ_service.rb +++ b/app/services/types_de_champ_service.rb @@ -13,17 +13,17 @@ class TypesDeChampService :type_champ, :id, :mandatory, - :type, drop_down_list_attributes: [:value, :id] ]) - parameters[attributes].each do |param_first, param_second| - if param_second[:libelle].empty? - parameters[attributes].delete(param_first.to_s) + parameters[attributes].each do |index, param| + param[:private] = private + if param[:libelle].empty? + parameters[attributes].delete(index.to_s) end - if param_second['drop_down_list_attributes'] && param_second['drop_down_list_attributes']['value'] - param_second['drop_down_list_attributes']['value'] = self.clean_value (param_second['drop_down_list_attributes']['value']) + if param['drop_down_list_attributes'] && param['drop_down_list_attributes']['value'] + param['drop_down_list_attributes']['value'] = self.clean_value (param['drop_down_list_attributes']['value']) end end diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index d3b6fc8fa..a1520d05e 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -57,8 +57,7 @@ describe Admin::TypesDeChampController, type: :controller do description: '', order_place: '1', id: '', - mandatory: false, - type: 'TypeDeChampPublic' + mandatory: false } } } diff --git a/spec/controllers/admin/types_de_champ_private_controller_spec.rb b/spec/controllers/admin/types_de_champ_private_controller_spec.rb index 61ab44966..07d388183 100644 --- a/spec/controllers/admin/types_de_champ_private_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_private_controller_spec.rb @@ -49,8 +49,7 @@ describe Admin::TypesDeChampPrivateController, type: :controller do description: description, order_place: order_place, id: types_de_champ_id, - mandatory: mandatory, - type: 'TypeDeChampPrivate' + mandatory: mandatory }, '1' => { libelle: '', @@ -58,8 +57,7 @@ describe Admin::TypesDeChampPrivateController, type: :controller do description: '', order_place: '1', id: '', - mandatory: false, - type: 'TypeDeChampPrivate' + mandatory: false } } } diff --git a/spec/factories/type_de_champ_private.rb b/spec/factories/type_de_champ_private.rb index d804dd42b..11a434765 100644 --- a/spec/factories/type_de_champ_private.rb +++ b/spec/factories/type_de_champ_private.rb @@ -1,5 +1,6 @@ FactoryBot.define do - factory :type_de_champ_private do + factory :type_de_champ_private, class: 'TypeDeChamp' do + private true sequence(:libelle) { |n| "Libelle champ privé #{n}" } sequence(:description) { |n| "description du champ privé #{n}" } type_champ 'text' diff --git a/spec/factories/type_de_champ_public.rb b/spec/factories/type_de_champ_public.rb index 7f63092cf..9654959e1 100644 --- a/spec/factories/type_de_champ_public.rb +++ b/spec/factories/type_de_champ_public.rb @@ -1,5 +1,6 @@ FactoryBot.define do - factory :type_de_champ_public do + factory :type_de_champ_public, class: 'TypeDeChamp' do + private false sequence(:libelle) { |n| "Libelle du champ #{n}" } sequence(:description) { |n| "description du champ #{n}" } type_champ 'text' diff --git a/spec/models/type_de_champ_private_spec.rb b/spec/models/type_de_champ_private_spec.rb index 6f0b1ce8a..19b8a631b 100644 --- a/spec/models/type_de_champ_private_spec.rb +++ b/spec/models/type_de_champ_private_spec.rb @@ -1,7 +1,10 @@ require 'spec_helper' -describe TypeDeChampPrivate do - require 'models/type_de_champ_shared_example' +describe TypeDeChamp do + describe '#private?' do + let(:type_de_champ) { build(:type_de_champ_private) } - it_should_behave_like "type_de_champ_spec" + it { expect(type_de_champ.private?).to be_truthy } + it { expect(type_de_champ.public?).to be_falsey } + end end diff --git a/spec/models/type_de_champ_public_spec.rb b/spec/models/type_de_champ_public_spec.rb deleted file mode 100644 index fc855566f..000000000 --- a/spec/models/type_de_champ_public_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'spec_helper' - -describe TypeDeChampPublic do - require 'models/type_de_champ_shared_example' - - it_should_behave_like "type_de_champ_spec" -end diff --git a/spec/services/types_de_champ_service_spec.rb b/spec/services/types_de_champ_service_spec.rb index dbe847cc0..f08072542 100644 --- a/spec/services/types_de_champ_service_spec.rb +++ b/spec/services/types_de_champ_service_spec.rb @@ -9,7 +9,7 @@ describe TypesDeChampService do describe 'the drop down list attributes' do let(:types_de_champ_attributes) do { - "0" => { + "0": { libelle: 'top', drop_down_list_attributes: { value: "un\r\n deux\r\n -- commentaire --\r\n trois", @@ -28,29 +28,34 @@ describe TypesDeChampService do describe 'reorder the fields' do let(:types_de_champ_attributes) do { - '0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '1' }, - '1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' } + '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '1' }, + '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' } } end subject { result['types_de_champ_attributes'].to_unsafe_hash } - it { is_expected.to match({ "0" => { 'libelle' => 'a', 'order_place' => '0' }, "1" => { 'libelle' => 'b', 'order_place' => '1' } }) } + it do + is_expected.to match({ + '0': { 'libelle': 'a', 'order_place': '0', 'private': false }, + '1': { 'libelle': 'b', 'order_place': '1', 'private': false } + }) + end context 'when the user specifies a position on one element' do let(:types_de_champ_attributes) do { - '0' => { 'libelle' => 'a', 'order_place' => '1', 'custom_order_place' => '1' }, - '1' => { 'libelle' => 'b', 'order_place' => '10', 'custom_order_place' => '10' }, - '2' => { 'libelle' => 'c', 'order_place' => '11', 'custom_order_place' => '2' } + '0': { 'libelle': 'a', 'order_place': '1', 'custom_order_place': '1' }, + '1': { 'libelle': 'b', 'order_place': '10', 'custom_order_place': '10' }, + '2': { 'libelle': 'c', 'order_place': '11', 'custom_order_place': '2' } } end it do is_expected.to match({ - '0' => { 'libelle' => 'a', 'order_place' => '0' }, - '1' => { 'libelle' => 'c', 'order_place' => '1' }, - '2' => { 'libelle' => 'b', 'order_place' => '2' } + '0': { 'libelle': 'a', 'order_place': '0', 'private': false }, + '1': { 'libelle': 'c', 'order_place': '1', 'private': false }, + '2': { 'libelle': 'b', 'order_place': '2', 'private': false } }) end end @@ -58,17 +63,17 @@ describe TypesDeChampService do context 'when the user puts a champ down' do let(:types_de_champ_attributes) do { - '0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '2' }, - '1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' }, - '2' => { 'libelle' => 'c', 'order_place' => '2', 'custom_order_place' => '3' } + '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '2' }, + '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' }, + '2': { 'libelle': 'c', 'order_place': '2', 'custom_order_place': '3' } } end it do is_expected.to match({ - '0' => { 'libelle' => 'b', 'order_place' => '0' }, - '1' => { 'libelle' => 'a', 'order_place' => '1' }, - '2' => { 'libelle' => 'c', 'order_place' => '2' } + '0': { 'libelle': 'b', 'order_place': '0', 'private': false }, + '1': { 'libelle': 'a', 'order_place': '1', 'private': false }, + '2': { 'libelle': 'c', 'order_place': '2', 'private': false } }) end end @@ -76,19 +81,19 @@ describe TypesDeChampService do context 'when the user uses not a number' do let(:types_de_champ_attributes) do { - '0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '1' }, - '1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' }, - '2' => { 'libelle' => 'c', 'order_place' => '2', 'custom_order_place' => '' }, - '3' => { 'libelle' => 'd', 'order_place' => '3', 'custom_order_place' => 'a' } + '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '1' }, + '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' }, + '2': { 'libelle': 'c', 'order_place': '2', 'custom_order_place': '' }, + '3': { 'libelle': 'd', 'order_place': '3', 'custom_order_place': 'a' } } end it 'does not change the natural order' do is_expected.to match({ - '0' => { 'libelle' => 'a', 'order_place' => '0' }, - '1' => { 'libelle' => 'b', 'order_place' => '1' }, - '2' => { 'libelle' => 'c', 'order_place' => '2' }, - '3' => { 'libelle' => 'd', 'order_place' => '3' } + '0': { 'libelle': 'a', 'order_place': '0', 'private': false }, + '1': { 'libelle': 'b', 'order_place': '1', 'private': false }, + '2': { 'libelle': 'c', 'order_place': '2', 'private': false }, + '3': { 'libelle': 'd', 'order_place': '3', 'private': false } }) end end