From 5470141ca428d183c14349d65a04a77b99629cca Mon Sep 17 00:00:00 2001 From: Paul Chavard Date: Wed, 14 Feb 2018 11:25:54 +0100 Subject: [PATCH] =?UTF-8?q?Moulinette=E2=84=A2=20STI=20Champ=20et=20TypeDe?= =?UTF-8?q?Champ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/type_de_champ.rb | 7 ++++++- app/services/types_de_champ_service.rb | 4 ++++ config/initializers/inflections.rb | 2 +- .../2018_02_13_fill_champ_private_and_type.rake | 14 ++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 lib/tasks/2018_02_13_fill_champ_private_and_type.rake diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 20c4639a8..ecc42691a 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -50,7 +50,8 @@ class TypeDeChamp < ActiveRecord::Base def params_for_champ { - private: private? + private: private?, + type: "Champs::#{type_champ.classify}Champ" } end @@ -77,4 +78,8 @@ class TypeDeChamp < ActiveRecord::Base def public? !private? end + + def self.type_champ_to_class_name(type_champ) + "TypesDeChamp::#{type_champ.classify}TypeDeChamp" + end end diff --git a/app/services/types_de_champ_service.rb b/app/services/types_de_champ_service.rb index d4eb53a9b..614e6b5fe 100644 --- a/app/services/types_de_champ_service.rb +++ b/app/services/types_de_champ_service.rb @@ -18,6 +18,10 @@ class TypesDeChampService parameters[attributes].each do |index, param| param[:private] = private + if param[:type_champ] + param[:type] = TypeDeChamp.type_champ_to_class_name(param[:type_champ]) + end + if param[:libelle].empty? parameters[attributes].delete(index.to_s) end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index fc49561ab..bcdbc6893 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -15,7 +15,7 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.irregular 'type_de_champ', 'types_de_champ' inflect.irregular 'type_de_champ_private', 'types_de_champ_private' inflect.irregular 'assign_to', 'assign_tos' - inflect.irregular('avis', 'avis') + inflect.uncountable(['avis', 'pays']) end # From https://github.com/davidcelis/inflections diff --git a/lib/tasks/2018_02_13_fill_champ_private_and_type.rake b/lib/tasks/2018_02_13_fill_champ_private_and_type.rake new file mode 100644 index 000000000..86a2da1ec --- /dev/null +++ b/lib/tasks/2018_02_13_fill_champ_private_and_type.rake @@ -0,0 +1,14 @@ +namespace :'2018_02_13_fill_champ_private_and_type' do + task set: :environment do + Champ.includes(:type_de_champ).find_each do |champ| + champ.update_columns(champ.type_de_champ.params_for_champ) + end + + TypeDeChamp.find_each do |type_de_champ| + type_de_champ.update_columns( + private: type_de_champ.private?, + type: TypeDeChamp.type_champ_to_class_name(type_de_champ.type_champ) + ) + end + end +end