From b8f71bd52cc9d0e52a196723d6db92eaa6d571c8 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 6 May 2021 12:27:34 +0200 Subject: [PATCH 1/2] i18n: fix enum values for Dossier.state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to Rails i18n guide, enum values should be localized as `/`, not as sub-values to the attribute. This fixes an exception in administrate when displaying a Dossier in the Manager. Note: we need to change the way GraphQL attributes are generated, because `AASM::Core::State#display_name` doesn’t honor the `model/attribute.value` convention (and instead tries to localize as `model.attribute/value`). So instead we lookup the localized name using ActiveRecord. --- app/graphql/schema.graphql | 2 +- app/graphql/types/dossier_type.rb | 2 +- app/helpers/dossier_helper.rb | 2 +- config/locales/models/dossier/fr.yml | 26 +++++++++++--------------- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/app/graphql/schema.graphql b/app/graphql/schema.graphql index c81a8d142..6e0df05e6 100644 --- a/app/graphql/schema.graphql +++ b/app/graphql/schema.graphql @@ -1088,7 +1088,7 @@ enum DossierState { refuse """ - Sans suite + Classé sans suite """ sans_suite } diff --git a/app/graphql/types/dossier_type.rb b/app/graphql/types/dossier_type.rb index 57811a75e..3e9ecec5a 100644 --- a/app/graphql/types/dossier_type.rb +++ b/app/graphql/types/dossier_type.rb @@ -2,7 +2,7 @@ module Types class DossierType < Types::BaseObject class DossierState < Types::BaseEnum Dossier.aasm.states.reject { |state| state.name == :brouillon }.each do |state| - value(state.name.to_s, state.display_name, value: state.name.to_s) + value(state.name.to_s, Dossier.human_attribute_name("state.#{state.name}"), value: state.name.to_s) end end diff --git a/app/helpers/dossier_helper.rb b/app/helpers/dossier_helper.rb index 3812af890..6d6b85884 100644 --- a/app/helpers/dossier_helper.rb +++ b/app/helpers/dossier_helper.rb @@ -51,7 +51,7 @@ module DossierHelper def dossier_display_state(dossier_or_state, lower: false) state = dossier_or_state.is_a?(Dossier) ? dossier_or_state.state : dossier_or_state - display_state = I18n.t(state, scope: [:activerecord, :attributes, :dossier, :state]) + display_state = Dossier.human_attribute_name("state.#{state}") lower ? display_state.downcase : display_state end diff --git a/config/locales/models/dossier/fr.yml b/config/locales/models/dossier/fr.yml index d79072067..1fb503dff 100644 --- a/config/locales/models/dossier/fr.yml +++ b/config/locales/models/dossier/fr.yml @@ -9,20 +9,16 @@ fr: montant_projet: 'Le montant du projet' montant_aide_demande: "Le montant d’aide demandée" date_previsionnelle: "La date de début prévisionnelle" - state: &state - brouillon: "Brouillon" - en_construction: "En construction" - en_instruction: "En instruction" - accepte: "Accepté" - refuse: "Refusé" - sans_suite: "Classé sans suite" + state: "État" autorisation_donnees: Acceptation des CGU - state/brouillon: Brouillon - state/en_construction: En construction - state/en_instruction: En instruction - state/accepte: Accepté - state/refuse: Refusé - state/sans_suite: Sans suite + dossier/state: &state + brouillon: "Brouillon" + en_construction: "En construction" + en_instruction: "En instruction" + accepte: "Accepté" + refuse: "Refusé" + sans_suite: "Classé sans suite" traitement: - state: - <<: *state + state: "État" + traitement/state: + <<: *state From b398485d9719268eae2d5ab8b6689e76d5fd667b Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Thu, 6 May 2021 10:29:52 +0000 Subject: [PATCH 2/2] manager: localize dossier enums In the previous commit, we cleaned up the localization of the Dossier.state enum. This prevented administrate from crashing, but now the dossier enum are no longer localized in the Manager. By using a `Field::Enum` type, we instruct the administrate plugin to look up our localized name for the Dossier state enum. --- Gemfile | 1 + Gemfile.lock | 3 +++ app/dashboards/dossier_dashboard.rb | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 6c50d0d32..b75222113 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ gem 'active_model_serializers' gem 'activestorage-openstack' gem 'active_storage_validations' gem 'administrate', git: 'https://github.com/thoughtbot/administrate.git', ref: 'refs/pull/1972/head' # Provides an administration UI (pull request #1972 has fixes for Rails 6.1.3.2) +gem 'administrate-field-enum' # Allow using Field::Enum in administrate gem 'after_party' gem 'anchored' gem 'bcrypt' diff --git a/Gemfile.lock b/Gemfile.lock index adf2a9b6e..1643c8608 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -102,6 +102,8 @@ GEM zeitwerk (~> 2.3) addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) + administrate-field-enum (0.0.9) + administrate (~> 0.12) aes_key_wrap (1.1.0) after_party (1.11.2) anchored (1.1.0) @@ -776,6 +778,7 @@ DEPENDENCIES active_storage_validations activestorage-openstack administrate! + administrate-field-enum after_party anchored annotate diff --git a/app/dashboards/dossier_dashboard.rb b/app/dashboards/dossier_dashboard.rb index 698106704..6c102b209 100644 --- a/app/dashboards/dossier_dashboard.rb +++ b/app/dashboards/dossier_dashboard.rb @@ -10,7 +10,7 @@ class DossierDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number.with_options(searchable: true), procedure: Field::HasOne, - state: Field::String, + state: Field::Enum, user: Field::BelongsTo, text_summary: Field::String.with_options(searchable: false), created_at: Field::DateTime,