Remove TypeDeChampPublic/TypeDeChampPrivate STI

This commit is contained in:
Paul Chavard 2018-02-13 15:38:26 +01:00
parent 75b2e16cc0
commit 31d638ae2a
14 changed files with 58 additions and 57 deletions

View file

@ -19,7 +19,7 @@ class RootController < ApplicationController
description = 'a not so long description' description = 'a not so long description'
all_champs = TypeDeChamp.type_champs 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) } .map.with_index { |type_de_champ, i| type_de_champ.champ.build(id: i) }
all_champs all_champs

View file

@ -23,7 +23,7 @@ class AdminTypesDeChampFacades
end end
def new_type_de_champ def new_type_de_champ
@private ? TypeDeChampPrivate.new.decorate : TypeDeChampPublic.new.decorate TypeDeChamp.new(private: @private).decorate
end end
def fields_for_var def fields_for_var

View file

@ -1,7 +1,7 @@
class Procedure < ActiveRecord::Base class Procedure < ActiveRecord::Base
has_many :types_de_piece_justificative, -> { order "order_place ASC" }, dependent: :destroy 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, -> { public_only }, dependent: :destroy
has_many :types_de_champ_private, dependent: :destroy has_many :types_de_champ_private, -> { private_only }, class_name: 'TypeDeChamp', dependent: :destroy
has_many :dossiers has_many :dossiers
has_one :procedure_path, dependent: :destroy has_one :procedure_path, dependent: :destroy

View file

@ -1,4 +1,6 @@
class TypeDeChamp < ActiveRecord::Base class TypeDeChamp < ActiveRecord::Base
self.inheritance_column = :_type_disabled
enum type_champs: { enum type_champs: {
text: 'text', text: 'text',
textarea: 'textarea', textarea: 'textarea',
@ -24,6 +26,9 @@ class TypeDeChamp < ActiveRecord::Base
belongs_to :procedure 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 has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
def build(params = {}) def build(params = {})
super(params.merge(proxy_association.owner.params_for_champ)) super(params.merge(proxy_association.owner.params_for_champ))
@ -65,7 +70,7 @@ class TypeDeChamp < ActiveRecord::Base
end end
def private? def private?
type == 'TypeDeChampPrivate' super || type == 'TypeDeChampPrivate'
end end
def public? def public?

View file

@ -1,2 +0,0 @@
class TypeDeChampPrivate < TypeDeChamp
end

View file

@ -1,2 +0,0 @@
class TypeDeChampPublic < TypeDeChamp
end

View file

@ -13,17 +13,17 @@ class TypesDeChampService
:type_champ, :type_champ,
:id, :id,
:mandatory, :mandatory,
:type,
drop_down_list_attributes: [:value, :id] drop_down_list_attributes: [:value, :id]
]) ])
parameters[attributes].each do |param_first, param_second| parameters[attributes].each do |index, param|
if param_second[:libelle].empty? param[:private] = private
parameters[attributes].delete(param_first.to_s) if param[:libelle].empty?
parameters[attributes].delete(index.to_s)
end end
if param_second['drop_down_list_attributes'] && param_second['drop_down_list_attributes']['value'] if param['drop_down_list_attributes'] && param['drop_down_list_attributes']['value']
param_second['drop_down_list_attributes']['value'] = self.clean_value (param_second['drop_down_list_attributes']['value']) param['drop_down_list_attributes']['value'] = self.clean_value (param['drop_down_list_attributes']['value'])
end end
end end

View file

@ -57,8 +57,7 @@ describe Admin::TypesDeChampController, type: :controller do
description: '', description: '',
order_place: '1', order_place: '1',
id: '', id: '',
mandatory: false, mandatory: false
type: 'TypeDeChampPublic'
} }
} }
} }

View file

@ -49,8 +49,7 @@ describe Admin::TypesDeChampPrivateController, type: :controller do
description: description, description: description,
order_place: order_place, order_place: order_place,
id: types_de_champ_id, id: types_de_champ_id,
mandatory: mandatory, mandatory: mandatory
type: 'TypeDeChampPrivate'
}, },
'1' => { '1' => {
libelle: '', libelle: '',
@ -58,8 +57,7 @@ describe Admin::TypesDeChampPrivateController, type: :controller do
description: '', description: '',
order_place: '1', order_place: '1',
id: '', id: '',
mandatory: false, mandatory: false
type: 'TypeDeChampPrivate'
} }
} }
} }

View file

@ -1,5 +1,6 @@
FactoryBot.define do 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(:libelle) { |n| "Libelle champ privé #{n}" }
sequence(:description) { |n| "description du champ privé #{n}" } sequence(:description) { |n| "description du champ privé #{n}" }
type_champ 'text' type_champ 'text'

View file

@ -1,5 +1,6 @@
FactoryBot.define do 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(:libelle) { |n| "Libelle du champ #{n}" }
sequence(:description) { |n| "description du champ #{n}" } sequence(:description) { |n| "description du champ #{n}" }
type_champ 'text' type_champ 'text'

View file

@ -1,7 +1,10 @@
require 'spec_helper' require 'spec_helper'
describe TypeDeChampPrivate do describe TypeDeChamp do
require 'models/type_de_champ_shared_example' 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 end

View file

@ -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

View file

@ -9,7 +9,7 @@ describe TypesDeChampService do
describe 'the drop down list attributes' do describe 'the drop down list attributes' do
let(:types_de_champ_attributes) do let(:types_de_champ_attributes) do
{ {
"0" => { "0": {
libelle: 'top', libelle: 'top',
drop_down_list_attributes: { drop_down_list_attributes: {
value: "un\r\n deux\r\n -- commentaire --\r\n trois", value: "un\r\n deux\r\n -- commentaire --\r\n trois",
@ -28,29 +28,34 @@ describe TypesDeChampService do
describe 'reorder the fields' do describe 'reorder the fields' do
let(:types_de_champ_attributes) do let(:types_de_champ_attributes) do
{ {
'0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '1' }, '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '1' },
'1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' } '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' }
} }
end end
subject { result['types_de_champ_attributes'].to_unsafe_hash } 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 context 'when the user specifies a position on one element' do
let(:types_de_champ_attributes) do let(:types_de_champ_attributes) do
{ {
'0' => { 'libelle' => 'a', 'order_place' => '1', 'custom_order_place' => '1' }, '0': { 'libelle': 'a', 'order_place': '1', 'custom_order_place': '1' },
'1' => { 'libelle' => 'b', 'order_place' => '10', 'custom_order_place' => '10' }, '1': { 'libelle': 'b', 'order_place': '10', 'custom_order_place': '10' },
'2' => { 'libelle' => 'c', 'order_place' => '11', 'custom_order_place' => '2' } '2': { 'libelle': 'c', 'order_place': '11', 'custom_order_place': '2' }
} }
end end
it do it do
is_expected.to match({ is_expected.to match({
'0' => { 'libelle' => 'a', 'order_place' => '0' }, '0': { 'libelle': 'a', 'order_place': '0', 'private': false },
'1' => { 'libelle' => 'c', 'order_place' => '1' }, '1': { 'libelle': 'c', 'order_place': '1', 'private': false },
'2' => { 'libelle' => 'b', 'order_place' => '2' } '2': { 'libelle': 'b', 'order_place': '2', 'private': false }
}) })
end end
end end
@ -58,17 +63,17 @@ describe TypesDeChampService do
context 'when the user puts a champ down' do context 'when the user puts a champ down' do
let(:types_de_champ_attributes) do let(:types_de_champ_attributes) do
{ {
'0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '2' }, '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '2' },
'1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' }, '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' },
'2' => { 'libelle' => 'c', 'order_place' => '2', 'custom_order_place' => '3' } '2': { 'libelle': 'c', 'order_place': '2', 'custom_order_place': '3' }
} }
end end
it do it do
is_expected.to match({ is_expected.to match({
'0' => { 'libelle' => 'b', 'order_place' => '0' }, '0': { 'libelle': 'b', 'order_place': '0', 'private': false },
'1' => { 'libelle' => 'a', 'order_place' => '1' }, '1': { 'libelle': 'a', 'order_place': '1', 'private': false },
'2' => { 'libelle' => 'c', 'order_place' => '2' } '2': { 'libelle': 'c', 'order_place': '2', 'private': false }
}) })
end end
end end
@ -76,19 +81,19 @@ describe TypesDeChampService do
context 'when the user uses not a number' do context 'when the user uses not a number' do
let(:types_de_champ_attributes) do let(:types_de_champ_attributes) do
{ {
'0' => { 'libelle' => 'a', 'order_place' => '0', 'custom_order_place' => '1' }, '0': { 'libelle': 'a', 'order_place': '0', 'custom_order_place': '1' },
'1' => { 'libelle' => 'b', 'order_place' => '1', 'custom_order_place' => '2' }, '1': { 'libelle': 'b', 'order_place': '1', 'custom_order_place': '2' },
'2' => { 'libelle' => 'c', 'order_place' => '2', 'custom_order_place' => '' }, '2': { 'libelle': 'c', 'order_place': '2', 'custom_order_place': '' },
'3' => { 'libelle' => 'd', 'order_place' => '3', 'custom_order_place' => 'a' } '3': { 'libelle': 'd', 'order_place': '3', 'custom_order_place': 'a' }
} }
end end
it 'does not change the natural order' do it 'does not change the natural order' do
is_expected.to match({ is_expected.to match({
'0' => { 'libelle' => 'a', 'order_place' => '0' }, '0': { 'libelle': 'a', 'order_place': '0', 'private': false },
'1' => { 'libelle' => 'b', 'order_place' => '1' }, '1': { 'libelle': 'b', 'order_place': '1', 'private': false },
'2' => { 'libelle' => 'c', 'order_place' => '2' }, '2': { 'libelle': 'c', 'order_place': '2', 'private': false },
'3' => { 'libelle' => 'd', 'order_place' => '3' } '3': { 'libelle': 'd', 'order_place': '3', 'private': false }
}) })
end end
end end