From 0d703d0d433cc08a416ab2b541f1fa3627975f9c Mon Sep 17 00:00:00 2001 From: f Date: Tue, 21 Jul 2020 19:34:15 -0300 Subject: [PATCH] no tener que definir to_param en cada Metadata --- app/models/metadata_array.rb | 4 ---- app/models/metadata_belongs_to.rb | 5 ----- app/models/metadata_document_date.rb | 6 ++---- app/models/metadata_file.rb | 4 ---- app/models/metadata_geo.rb | 4 ---- app/models/metadata_lang.rb | 2 +- app/models/metadata_locales.rb | 5 +---- app/models/metadata_slug.rb | 2 +- app/models/metadata_template.rb | 10 +++++++++- 9 files changed, 14 insertions(+), 28 deletions(-) diff --git a/app/models/metadata_array.rb b/app/models/metadata_array.rb index 343081c2..3e84a8a1 100644 --- a/app/models/metadata_array.rb +++ b/app/models/metadata_array.rb @@ -7,10 +7,6 @@ class MetadataArray < MetadataTemplate [] end - def to_param - { name => [] } - end - private def sanitize(values) diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb index b2d0ef7c..b94fbc29 100644 --- a/app/models/metadata_belongs_to.rb +++ b/app/models/metadata_belongs_to.rb @@ -13,11 +13,6 @@ class MetadataBelongsTo < MetadataRelatedPosts '' end - # @see MetadataTemplate - def to_param - name - end - def validate super diff --git a/app/models/metadata_document_date.rb b/app/models/metadata_document_date.rb index f49aaa7d..036877ae 100644 --- a/app/models/metadata_document_date.rb +++ b/app/models/metadata_document_date.rb @@ -10,11 +10,9 @@ class MetadataDocumentDate < MetadataTemplate # El valor puede ser un Date, Time o una String en el formato # "yyyy-mm-dd" def value - return self[:value] = document.date || default_value if self[:value].nil? + return (self[:value] = document.date || default_value) if self[:value].nil? - if self[:value].is_a? String - self[:value] = Date.iso8601(self[:value]).to_time - end + self[:value] = Date.iso8601(self[:value]).to_time if self[:value].is_a? String self[:value] end diff --git a/app/models/metadata_file.rb b/app/models/metadata_file.rb index bb7ce844..0b505238 100644 --- a/app/models/metadata_file.rb +++ b/app/models/metadata_file.rb @@ -50,10 +50,6 @@ class MetadataFile < MetadataTemplate true end - def to_param - { name => %i[description path] } - end - # Almacena el archivo en el sitio y lo devuelve # # @return ActiveStorage::Attachment diff --git a/app/models/metadata_geo.rb b/app/models/metadata_geo.rb index 17fb9718..1fb3febc 100644 --- a/app/models/metadata_geo.rb +++ b/app/models/metadata_geo.rb @@ -10,10 +10,6 @@ class MetadataGeo < MetadataTemplate value == default_value end - def to_param - { name => %i[lat lng] } - end - def save return true if empty? diff --git a/app/models/metadata_lang.rb b/app/models/metadata_lang.rb index 96ba23c8..f4a6640d 100644 --- a/app/models/metadata_lang.rb +++ b/app/models/metadata_lang.rb @@ -7,7 +7,7 @@ class MetadataLang < MetadataTemplate end def value - self[:value] || document.collection.label || default_value + self[:value] ||= document.collection.label || default_value end def values diff --git a/app/models/metadata_locales.rb b/app/models/metadata_locales.rb index 5843b04a..6498ed77 100644 --- a/app/models/metadata_locales.rb +++ b/app/models/metadata_locales.rb @@ -9,6 +9,7 @@ class MetadataLocales < MetadataTemplate # Todos los valores posibles para cada idioma disponible # # TODO: Optimizar? + # TODO: Mantener sincronizados # # @return { lang: { title: uuid } } def values @@ -20,8 +21,4 @@ class MetadataLocales < MetadataTemplate } end.inject(:merge) end - - def to_param - { name => [] } - end end diff --git a/app/models/metadata_slug.rb b/app/models/metadata_slug.rb index ea58c8d2..f11fb29f 100644 --- a/app/models/metadata_slug.rb +++ b/app/models/metadata_slug.rb @@ -33,7 +33,7 @@ class MetadataSlug < MetadataTemplate end def value - self[:value] || document.data.fetch('slug', default_value) + self[:value] ||= document.data.fetch('slug', default_value) end private diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 8c016b1d..80c99e45 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -53,8 +53,16 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, errors.empty? end + # Usa el valor por defecto para generar el formato de StrongParam + # necesario. + # + # @return [Symbol,Hash] def to_param - name + case default_value + when Hash then { name => default_value.keys.map(&:to_sym) } + when Array then { name => [] } + else name + end end def to_s