Remove ChampPublic/ChampPrivate STI

This commit is contained in:
Paul Chavard 2018-02-09 17:38:30 +01:00
parent d432ea7249
commit 52749713ab
16 changed files with 78 additions and 52 deletions

View file

@ -20,11 +20,7 @@ class RootController < ApplicationController
all_champs = TypeDeChamp.type_champs
.map { |name, _| TypeDeChamp.new(type_champ: name, libelle: name, description: description, mandatory: true) }
.map { |type_de_champ| ChampPublic.new(type_de_champ: type_de_champ) }
.map.with_index do |champ, i|
champ.id = i
champ
end
.map.with_index { |type_de_champ, i| type_de_champ.champ.build(id: i) }
all_champs
.select { |champ| champ.type_champ == 'header_section' }

View file

@ -1,6 +1,8 @@
class Champ < ActiveRecord::Base
self.inheritance_column = :_type_disabled
belongs_to :dossier, touch: true
belongs_to :type_de_champ
belongs_to :type_de_champ, inverse_of: :champ
has_many :commentaires
delegate :libelle, :type_champ, :order_place, :mandatory, :description, :drop_down_list, to: :type_de_champ
@ -10,11 +12,21 @@ class Champ < ActiveRecord::Base
before_save :multiple_select_to_string, if: Proc.new { type_champ == 'multiple_drop_down_list' }
scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) }
scope :public_only, -> { where('champs.type != ?', 'ChampPrivate') }
scope :private_only, -> { where(type: 'ChampPrivate') }
def mandatory?
mandatory
end
def public?
!private?
end
def private?
type == 'ChampPrivate'
end
def same_hour? num
same_date? num, '%H'
end

View file

@ -1,2 +0,0 @@
class ChampPrivate < Champ
end

View file

@ -1,2 +0,0 @@
class ChampPublic < Champ
end

View file

@ -20,8 +20,8 @@ class Dossier < ActiveRecord::Base
has_many :cerfa, dependent: :destroy
has_many :pieces_justificatives, dependent: :destroy
has_many :champs, class_name: 'ChampPublic', dependent: :destroy
has_many :champs_private, class_name: 'ChampPrivate', dependent: :destroy
has_many :champs, -> { public_only }, dependent: :destroy
has_many :champs_private, -> { private_only }, class_name: 'Champ', dependent: :destroy
has_many :quartier_prioritaires, dependent: :destroy
has_many :cadastres, dependent: :destroy
has_many :commentaires, dependent: :destroy
@ -89,12 +89,8 @@ class Dossier < ActiveRecord::Base
end
def build_default_champs
procedure.types_de_champ.all.each do |type_de_champ|
ChampPublic.create(type_de_champ: type_de_champ, dossier: self)
end
procedure.types_de_champ_private.all.each do |type_de_champ|
ChampPrivate.create(type_de_champ: type_de_champ, dossier: self)
procedure.all_types_de_champ.each do |type_de_champ|
type_de_champ.champ.create(dossier: self)
end
end

View file

@ -62,6 +62,10 @@ class Procedure < ActiveRecord::Base
types_de_champ_private.order(:order_place)
end
def all_types_de_champ
types_de_champ + types_de_champ_private
end
def self.active id
publiees.find(id)
end

View file

@ -24,7 +24,23 @@ class TypeDeChamp < ActiveRecord::Base
belongs_to :procedure
has_many :champ, dependent: :destroy
has_many :champ, inverse_of: :type_de_champ, dependent: :destroy do
def build_params(params)
if proxy_association.owner.public?
params.merge(type: 'ChampPublic')
else
params.merge(type: 'ChampPrivate')
end
end
def build(params = {})
super(build_params(params))
end
def create(params = {})
super(build_params(params))
end
end
has_one :drop_down_list
accepts_nested_attributes_for :drop_down_list
@ -46,4 +62,12 @@ class TypeDeChamp < ActiveRecord::Base
self.mandatory = false if %w(header_section explication).include?(self.type_champ)
true
end
def private?
type == 'TypeDeChampPrivate'
end
def public?
!private?
end
end

View file

@ -1,5 +0,0 @@
class ChampPrivateSerializer < ActiveModel::Serializer
attributes :value
has_one :type_de_champ
end

View file

@ -1,5 +0,0 @@
class ChampPublicSerializer < ActiveModel::Serializer
attributes :value
has_one :type_de_champ
end

View file

@ -0,0 +1,5 @@
class ChampSerializer < ActiveModel::Serializer
attributes :value
has_one :type_de_champ
end

View file

@ -21,7 +21,7 @@
= fff.hidden_field :id
- hide_mandatory = (ff.object.object.class == TypeDeChampPrivate || type_champ == 'explication')
- hide_mandatory = (ff.object.object.private? || type_champ == 'explication')
.form-group.mandatory{ style: hide_mandatory ? 'visibility: hidden;' : nil }
%h4 Obligatoire ?
.center

View file

@ -355,13 +355,11 @@ describe NewGestionnaire::DossiersController, type: :controller do
describe "#update_annotations" do
let(:champ_multiple_drop_down_list) do
type_de_champ = TypeDeChamp.create(type_champ: 'multiple_drop_down_list', libelle: 'libelle')
ChampPrivate.create(type_de_champ: type_de_champ)
create(:type_de_champ_private, type_champ: 'multiple_drop_down_list', libelle: 'libelle').champ.create
end
let(:champ_datetime) do
type_de_champ = TypeDeChamp.create(type_champ: 'datetime', libelle: 'libelle')
ChampPrivate.create(type_de_champ: type_de_champ)
create(:type_de_champ_private, type_champ: 'datetime', libelle: 'libelle').champ.create
end
let(:dossier) do

View file

@ -1,7 +1,11 @@
require 'spec_helper'
describe ChampPrivate do
require 'models/champ_shared_example.rb'
describe Champ do
describe '#private?' do
let(:type_de_champ) { build(:type_de_champ_private) }
let(:champ) { type_de_champ.champ.build }
it_should_behave_like "champ_spec"
it { expect(champ.private?).to be_truthy }
it { expect(champ.public?).to be_falsey }
end
end

View file

@ -1,7 +0,0 @@
require 'spec_helper'
describe ChampPublic do
require 'models/champ_shared_example.rb'
it_should_behave_like "champ_spec"
end

View file

@ -1,7 +1,7 @@
shared_examples 'champ_spec' do
describe 'mandatory_and_blank?' do
let(:type_de_champ) { TypeDeChamp.new(mandatory: mandatory) }
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
let(:type_de_champ) { build(:type_de_champ_public, mandatory: mandatory) }
let(:champ) { type_de_champ.champ.build(value: value) }
let(:value) { '' }
let(:mandatory) { true }
@ -34,7 +34,7 @@ shared_examples 'champ_spec' do
context "when type_champ=date" do
let(:type_de_champ) { create(:type_de_champ_public, type_champ: "date") }
let(:champ) { create(:champ, type_de_champ: type_de_champ) }
let(:champ) { type_de_champ.champ.create }
it "should convert %d/%m/%Y format to ISO" do
champ.value = "31/12/2017"

View file

@ -5,9 +5,17 @@ describe Champ do
it_should_behave_like "champ_spec"
describe '#public?' do
let(:type_de_champ) { build(:type_de_champ_public) }
let(:champ) { type_de_champ.champ.build }
it { expect(champ.public?).to be_truthy }
it { expect(champ.private?).to be_falsey }
end
describe '#format_datetime' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'datetime') }
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
let(:type_de_champ) { build(:type_de_champ_public, type_champ: 'datetime') }
let(:champ) { type_de_champ.champ.build(value: value) }
before { champ.save }
@ -25,8 +33,8 @@ describe Champ do
end
describe '#multiple_select_to_string' do
let(:type_de_champ) { TypeDeChamp.new(type_champ: 'multiple_drop_down_list') }
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
let(:type_de_champ) { build(:type_de_champ_public, type_champ: 'multiple_drop_down_list') }
let(:champ) { type_de_champ.champ.build(value: value) }
before { champ.save }
@ -64,7 +72,7 @@ describe Champ do
describe 'for_export' do
let(:type_de_champ) { create(:type_de_champ_public, type_champ: type_champ) }
let(:champ) { Champ.new(type_de_champ: type_de_champ, value: value) }
let(:champ) { type_de_champ.champ.build(value: value) }
before { champ.save }