Merge pull request #1412 from tchak/public-private-champs

Remove ChampPublic/ChampPrivate STI
This commit is contained in:
LeSim 2018-02-13 14:52:36 +01:00 committed by GitHub
commit 82e7d33e2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 92 additions and 53 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.not(type: 'ChampPrivate').or(where(private: [false, nil])) }
scope :private_only, -> { where(type: 'ChampPrivate').or(where(private: true)) }
def mandatory?
mandatory
end
def public?
!private?
end
def private?
super || 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,15 @@ 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 = {})
super(params.merge(proxy_association.owner.params_for_champ))
end
def create(params = {})
super(params.merge(proxy_association.owner.params_for_champ))
end
end
has_one :drop_down_list
accepts_nested_attributes_for :drop_down_list
@ -34,6 +42,12 @@ class TypeDeChamp < ActiveRecord::Base
before_validation :check_mandatory
def params_for_champ
{
private: private?
}
end
def self.type_de_champs_list_fr
type_champs.map { |champ| [I18n.t("activerecord.attributes.type_de_champ.type_champs.#{champ.last}"), champ.first] }
end
@ -46,4 +60,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

@ -0,0 +1,11 @@
class AddPrivateToChampAndTypeDeChamp < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
add_column :champs, :private, :boolean, index: true
add_column :types_de_champ, :private, :boolean, index: true
add_index :champs, :private, algorithm: :concurrently
add_index :types_de_champ, :private, algorithm: :concurrently
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_02_06_153121) do
ActiveRecord::Schema.define(version: 2018_02_09_133452) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -138,7 +138,9 @@ ActiveRecord::Schema.define(version: 2018_02_06_153121) do
t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "private"
t.index ["dossier_id"], name: "index_champs_on_dossier_id"
t.index ["private"], name: "index_champs_on_private"
t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id"
end
@ -425,6 +427,8 @@ ActiveRecord::Schema.define(version: 2018_02_06_153121) do
t.text "description"
t.boolean "mandatory", default: false
t.string "type"
t.boolean "private"
t.index ["private"], name: "index_types_de_champ_on_private"
end
create_table "types_de_piece_justificative", id: :serial, force: :cascade do |t|

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 }