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

View file

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

View file

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

View file

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

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,
: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

View file

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

View file

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

View file

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

View file

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

View file

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

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