diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index aa78ee3d0..febfb25d1 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -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' } diff --git a/app/models/champ.rb b/app/models/champ.rb index e4e1555b8..4daf0e05d 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -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 diff --git a/app/models/champ_private.rb b/app/models/champ_private.rb deleted file mode 100644 index 118e58415..000000000 --- a/app/models/champ_private.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ChampPrivate < Champ -end diff --git a/app/models/champ_public.rb b/app/models/champ_public.rb deleted file mode 100644 index c922614ae..000000000 --- a/app/models/champ_public.rb +++ /dev/null @@ -1,2 +0,0 @@ -class ChampPublic < Champ -end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 6c09b5328..5fede6a93 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -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 diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 0d81f0a3a..7cf0c3d67 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -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 diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 86747bc5a..4a792d1cd 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -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 diff --git a/app/serializers/champ_private_serializer.rb b/app/serializers/champ_private_serializer.rb deleted file mode 100644 index b26cf274f..000000000 --- a/app/serializers/champ_private_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -class ChampPrivateSerializer < ActiveModel::Serializer - attributes :value - - has_one :type_de_champ -end diff --git a/app/serializers/champ_public_serializer.rb b/app/serializers/champ_public_serializer.rb deleted file mode 100644 index 66806c982..000000000 --- a/app/serializers/champ_public_serializer.rb +++ /dev/null @@ -1,5 +0,0 @@ -class ChampPublicSerializer < ActiveModel::Serializer - attributes :value - - has_one :type_de_champ -end diff --git a/app/serializers/champ_serializer.rb b/app/serializers/champ_serializer.rb new file mode 100644 index 000000000..981c2b9cc --- /dev/null +++ b/app/serializers/champ_serializer.rb @@ -0,0 +1,5 @@ +class ChampSerializer < ActiveModel::Serializer + attributes :value + + has_one :type_de_champ +end diff --git a/app/views/admin/types_de_champ/_fields.html.haml b/app/views/admin/types_de_champ/_fields.html.haml index 9e651454b..bbadc32d8 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -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 diff --git a/db/migrate/20180209133452_add_private_to_champ_and_type_de_champ.rb b/db/migrate/20180209133452_add_private_to_champ_and_type_de_champ.rb new file mode 100644 index 000000000..5feb76084 --- /dev/null +++ b/db/migrate/20180209133452_add_private_to_champ_and_type_de_champ.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 320d92dc2..8db9e6a21 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb index 04d9586f2..5a08115b3 100644 --- a/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/dossiers_controller_spec.rb @@ -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 diff --git a/spec/models/champ_private_spec.rb b/spec/models/champ_private_spec.rb index 733505696..5e3dee9af 100644 --- a/spec/models/champ_private_spec.rb +++ b/spec/models/champ_private_spec.rb @@ -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 diff --git a/spec/models/champ_public_spec.rb b/spec/models/champ_public_spec.rb deleted file mode 100644 index a981702c7..000000000 --- a/spec/models/champ_public_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'spec_helper' - -describe ChampPublic do - require 'models/champ_shared_example.rb' - - it_should_behave_like "champ_spec" -end diff --git a/spec/models/champ_shared_example.rb b/spec/models/champ_shared_example.rb index 60dc38e9a..6c42ed032 100644 --- a/spec/models/champ_shared_example.rb +++ b/spec/models/champ_shared_example.rb @@ -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" diff --git a/spec/models/champ_spec.rb b/spec/models/champ_spec.rb index 6085a5719..f56a0f390 100644 --- a/spec/models/champ_spec.rb +++ b/spec/models/champ_spec.rb @@ -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 }