From 2e4ada98e675c83bd1f82866ffb42030d31ffc89 Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Fri, 9 Feb 2018 17:21:46 +0100 Subject: [PATCH] Add private columns to Champ and TypeDeChamp --- app/models/champ.rb | 6 +++--- app/models/type_de_champ.rb | 18 ++++++++---------- ...2_add_private_to_champ_and_type_de_champ.rb | 11 +++++++++++ db/schema.rb | 6 +++++- 4 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20180209133452_add_private_to_champ_and_type_de_champ.rb diff --git a/app/models/champ.rb b/app/models/champ.rb index 65f53ee9b..4daf0e05d 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -12,8 +12,8 @@ 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') } + 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 @@ -24,7 +24,7 @@ class Champ < ActiveRecord::Base end def private? - type == 'ChampPrivate' + super || type == 'ChampPrivate' end def same_hour? num diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index aed2c6225..4a792d1cd 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -25,20 +25,12 @@ class TypeDeChamp < ActiveRecord::Base belongs_to :procedure 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)) + super(params.merge(proxy_association.owner.params_for_champ)) end def create(params = {}) - super(build_params(params)) + super(params.merge(proxy_association.owner.params_for_champ)) end end has_one :drop_down_list @@ -50,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 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|