From 16c61e7e228d33f1444c5277ad751b46104499fa Mon Sep 17 00:00:00 2001 From: Cadu Ribeiro Date: Tue, 30 May 2023 21:16:48 -0300 Subject: [PATCH] Do not render i18n value of enum if data is nil MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently the lib tries to render the i18n value of the field even when it is nil. The problem with this is that when you have some i18n of it like the following: matrimonial_regimes: community_property: Comunhão universal de bens partial_property: Comunhão parcial de bens separate_property: Separação de bens the lib renders all values on the page as a hash: {:community_property=>"Comunhão universal de bens", :partial_property=>"Comunhão parcial de bens", :separate_property=>"Separação de bens"} This happens because the i18n key used on the view "activerecord.attributes.#{field.resource.class.name.underscore}. will render everything on the top level of my enum translations. This commit changes the views to not try to render if data is nil. --- app/views/fields/enum/_index.html.erb | 4 +++- app/views/fields/enum/_show.html.erb | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/fields/enum/_index.html.erb b/app/views/fields/enum/_index.html.erb index 1d68d02..d72d65b 100755 --- a/app/views/fields/enum/_index.html.erb +++ b/app/views/fields/enum/_index.html.erb @@ -14,4 +14,6 @@ By default, the attribute is rendered as a text tag. %> -<%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> +<% if field.data %> + <%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> +<% end %> diff --git a/app/views/fields/enum/_show.html.erb b/app/views/fields/enum/_show.html.erb index 3bcbd1d..f4ac608 100755 --- a/app/views/fields/enum/_show.html.erb +++ b/app/views/fields/enum/_show.html.erb @@ -14,4 +14,6 @@ By default, the attribute is rendered as a text tag. %> -<%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> +<% if field.data %> + <%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s.pluralize}.#{field.data}", default: field.to_s) %> +<% end %>