From 824226aa844b47117d052327e3fc4847e5d8c19f Mon Sep 17 00:00:00 2001 From: f Date: Wed, 11 Nov 2020 15:29:12 -0300 Subject: [PATCH] traer valores por defecto (excepto para boolean) --- app/models/metadata_array.rb | 2 +- app/models/metadata_content.rb | 2 +- app/models/metadata_event.rb | 2 +- app/models/metadata_file.rb | 2 +- app/models/metadata_geo.rb | 2 +- app/models/metadata_has_and_belongs_to_many.rb | 2 +- app/models/metadata_lang.rb | 2 +- app/models/metadata_locales.rb | 2 +- app/models/metadata_number.rb | 2 +- app/models/metadata_order.rb | 2 +- app/models/metadata_path.rb | 4 ---- app/models/metadata_related_posts.rb | 5 ----- app/models/metadata_string.rb | 2 +- app/models/metadata_template.rb | 10 ++++++++-- 14 files changed, 19 insertions(+), 22 deletions(-) diff --git a/app/models/metadata_array.rb b/app/models/metadata_array.rb index 3e84a8a..a1ff8c7 100644 --- a/app/models/metadata_array.rb +++ b/app/models/metadata_array.rb @@ -4,7 +4,7 @@ class MetadataArray < MetadataTemplate # El valor por defecto es una array vacía def default_value - [] + super || [] end private diff --git a/app/models/metadata_content.rb b/app/models/metadata_content.rb index a14e19f..5e0da09 100644 --- a/app/models/metadata_content.rb +++ b/app/models/metadata_content.rb @@ -4,7 +4,7 @@ # requieran texto largo. class MetadataContent < MetadataTemplate def default_value - '' + super || '' end def value diff --git a/app/models/metadata_event.rb b/app/models/metadata_event.rb index 3213d87..2d5a379 100644 --- a/app/models/metadata_event.rb +++ b/app/models/metadata_event.rb @@ -18,7 +18,7 @@ class MetadataEvent < MetadataTemplate # # @return [Hash] def default_value - {} + super || {} end def to_param diff --git a/app/models/metadata_file.rb b/app/models/metadata_file.rb index b77fed1..ecebb03 100644 --- a/app/models/metadata_file.rb +++ b/app/models/metadata_file.rb @@ -6,7 +6,7 @@ require 'filemagic' class MetadataFile < MetadataTemplate # Una ruta vacía a la imagen con una descripción vacía def default_value - { 'path' => nil, 'description' => nil } + super || { 'path' => nil, 'description' => nil } end def empty? diff --git a/app/models/metadata_geo.rb b/app/models/metadata_geo.rb index f106dc7..ba11f33 100644 --- a/app/models/metadata_geo.rb +++ b/app/models/metadata_geo.rb @@ -3,7 +3,7 @@ # Define un campo de coordenadas geográficas class MetadataGeo < MetadataTemplate def default_value - { 'lat' => nil, 'lng' => nil } + super || { 'lat' => nil, 'lng' => nil } end def empty? diff --git a/app/models/metadata_has_and_belongs_to_many.rb b/app/models/metadata_has_and_belongs_to_many.rb index 905000e..b32ce7c 100644 --- a/app/models/metadata_has_and_belongs_to_many.rb +++ b/app/models/metadata_has_and_belongs_to_many.rb @@ -12,7 +12,7 @@ # diferencia entre has_many y belongs_to. class MetadataHasAndBelongsToMany < MetadataBelongsTo def default_value - [] + super || [] end # Posts a los que pertenece. Memoizamos por value para obtener diff --git a/app/models/metadata_lang.rb b/app/models/metadata_lang.rb index f4a6640..da13d90 100644 --- a/app/models/metadata_lang.rb +++ b/app/models/metadata_lang.rb @@ -3,7 +3,7 @@ # Un campo de idioma class MetadataLang < MetadataTemplate def default_value - I18n.locale + super || I18n.locale end def value diff --git a/app/models/metadata_locales.rb b/app/models/metadata_locales.rb index 8958110..4f0d4f8 100644 --- a/app/models/metadata_locales.rb +++ b/app/models/metadata_locales.rb @@ -3,7 +3,7 @@ # Los valores de este metadato son artículos en otros idiomas class MetadataLocales < MetadataTemplate def default_value - [] + super || [] end # Todos los valores posibles para cada idioma disponible diff --git a/app/models/metadata_number.rb b/app/models/metadata_number.rb index cb61dbf..057e110 100644 --- a/app/models/metadata_number.rb +++ b/app/models/metadata_number.rb @@ -4,7 +4,7 @@ class MetadataNumber < MetadataTemplate # Nada def default_value - nil + super || nil end def save diff --git a/app/models/metadata_order.rb b/app/models/metadata_order.rb index 078061b..1b33a38 100644 --- a/app/models/metadata_order.rb +++ b/app/models/metadata_order.rb @@ -5,7 +5,7 @@ class MetadataOrder < MetadataTemplate # El valor según la posición del post en la relación ordenada por # fecha, a fecha más alta, posición más alta def default_value - site.posts(lang: post.lang.value).sort_by(:date).index(post) + super || site.posts(lang: lang).sort_by(:date).index(post) end def save diff --git a/app/models/metadata_path.rb b/app/models/metadata_path.rb index 5dc25c8..6dede4b 100644 --- a/app/models/metadata_path.rb +++ b/app/models/metadata_path.rb @@ -31,10 +31,6 @@ class MetadataPath < MetadataTemplate document.data['ext'].blank? ? '.markdown' : document.data['ext'] end - def lang - post.lang.value - end - def slug post.slug.value end diff --git a/app/models/metadata_related_posts.rb b/app/models/metadata_related_posts.rb index 5148a9f..6712ce2 100644 --- a/app/models/metadata_related_posts.rb +++ b/app/models/metadata_related_posts.rb @@ -27,11 +27,6 @@ class MetadataRelatedPosts < MetadataArray post&.title&.value || post&.slug&.value end - # TODO: Traer el idioma actual de otra forma - def lang - post&.lang&.value || I18n.locale - end - # Encuentra el filtro def filter layout.metadata.dig(name, 'filter')&.to_h&.symbolize_keys || {} diff --git a/app/models/metadata_string.rb b/app/models/metadata_string.rb index d100874..b56702d 100644 --- a/app/models/metadata_string.rb +++ b/app/models/metadata_string.rb @@ -4,7 +4,7 @@ class MetadataString < MetadataTemplate # Una string vacía def default_value - '' + super || '' end private diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index 521a02a..0e1f593 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -21,15 +21,21 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, !value_was.nil? && value_was != value end + # Trae el idioma actual del sitio o del panel + # @return [String] + def lang + @lang ||= post&.lang&.value || I18n.locale.to_s + end + # El valor por defecto def default_value - raise NotImplementedError + layout.metadata.dig(name, 'default', lang) end # Valores posibles, busca todos los valores actuales en otros # artículos del mismo sitio def values - site.everything_of(name, lang: post&.lang&.value) + site.everything_of(name, lang: lang) end # Valor actual o por defecto. Al memoizarlo podemos modificarlo