From 036387dbe05a0e8290f2ecb48cb8b847a71561f1 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 2 Jul 2020 11:25:04 -0300 Subject: [PATCH] establecer relaciones de pertenencia --- app/models/metadata_belongs_to.rb | 33 +++++++++++++++++++++++++++++++ app/models/metadata_template.rb | 2 +- config/locales/en.yml | 10 +++++----- config/locales/es.yml | 8 +++----- 4 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 app/models/metadata_belongs_to.rb diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb new file mode 100644 index 00000000..89b4676f --- /dev/null +++ b/app/models/metadata_belongs_to.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +# Almacena el UUID de otro Post +class MetadataBelongsTo < MetadataRelatedPosts + # TODO: Convertir algunos tipos de valores en módulos para poder + # implementar varios tipos de campo sin repetir código + # + # @include MetadataString + # + # Una string vacía + def default_value + '' + end + + # @see MetadataTemplate + def to_param + name + end + + def validate + super + + errors << I18n.t('metadata.belongs_to.missing_post') unless !value.blank? && posts.find(sanitize(value), uuid: true) + + errors.empty? + end + + private + + def sanitize(uuid) + uuid.gsub(/[^a-f0-9\-]/, '') + end +end diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 27cc3e75..fcca637f 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -39,7 +39,7 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, def validate self.errors = [] - errors << I18n.t("metadata.#{type}.cant_be_empty") unless can_be_empty? + errors << I18n.t("metadata.cant_be_empty") unless can_be_empty? errors.empty? end diff --git a/config/locales/en.yml b/config/locales/en.yml index f8add921..48b7465c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -31,18 +31,18 @@ en: destroyed: 'Removed "%{title}"' reorder: 'Reorder' metadata: - array: - cant_be_empty: 'This field cannot be empty' - string: - cant_be_empty: 'This field cannot be empty' + cant_be_empty: 'This field cannot be empty' image: - cant_be_empty: 'This field cannot be empty' + not_an_image: 'Not an image' + path_required: 'Missing file for upload' event: zone_missing: 'Timezone is incorrect' date_missing: 'Event date is required' date_non_parseable: 'Time is not in the correct format' time_non_parseable: 'Date is not in the correct format' end_in_the_past: "Event end can't happen before the start" + belongs_to: + missing_post: "Couldn't find the related post" exceptions: post: site_missing: 'Needs an instance of Site' diff --git a/config/locales/es.yml b/config/locales/es.yml index e94d2121..58fb3fa8 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -31,12 +31,8 @@ es: destroyed: 'Eliminado "%{title}"' reorder: 'Reordenados' metadata: - array: - cant_be_empty: 'El campo no puede estar vacío' - string: - cant_be_empty: 'El campo no puede estar vacío' + cant_be_empty: 'El campo no puede estar vacío' image: - cant_be_empty: 'El campo no puede estar vacío' not_an_image: 'No es una imagen' path_required: 'Se necesita un archivo de imagen' event: @@ -45,6 +41,8 @@ es: date_non_parseable: 'La fecha no está en el formato correcto' time_non_parseable: 'La hora no está en el formato correcto' end_in_the_past: 'El fin del evento no puede ser anterior al comienzo' + belongs_to: + missing_post: 'No se pudo encontrar el artículo relacionado' exceptions: post: site_missing: 'Necesita una instancia de Site'