From c338cb87de885251fc364a53b4e5403cc71a23af Mon Sep 17 00:00:00 2001 From: f Date: Thu, 24 Dec 2020 16:13:29 -0300 Subject: [PATCH] guardar fechas --- app/models/metadata_date.rb | 7 +++-- app/models/metadata_template.rb | 8 ++++- test/models/metadata/date_test.rb | 51 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 test/models/metadata/date_test.rb diff --git a/app/models/metadata_date.rb b/app/models/metadata_date.rb index 95612454..9e655d4c 100644 --- a/app/models/metadata_date.rb +++ b/app/models/metadata_date.rb @@ -5,13 +5,16 @@ class MetadataDate < MetadataTemplate Date.today end - # Ver MetadataDocumentDate + # Devuelve una fecha, si no hay ninguna es la fecha de hoy. + # + # @return [Date] def value return self[:value] if self[:value].is_a? Date return self[:value] if self[:value].is_a? Time + return document_value || default_value unless self[:value] begin - self[:value] = Date.parse(self[:value] || document.data[name.to_s]) + self[:value] = Date.parse self[:value] rescue ArgumentError, TypeError default_value end diff --git a/app/models/metadata_template.rb b/app/models/metadata_template.rb index a47906ed..b8be5a10 100644 --- a/app/models/metadata_template.rb +++ b/app/models/metadata_template.rb @@ -34,6 +34,11 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, !value_was.nil? && value_was != value end + # Obtiene el valor del JekyllDocument + def document_value + document.data[name.to_s] + end + # Trae el idioma actual del sitio o del panel # @return [String] def lang @@ -154,7 +159,8 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type, end def allowed_tags - @allowed_tags ||= %w[strong em del u mark p h1 h2 h3 h4 h5 h6 ul ol li img iframe audio video div figure figcaption a].freeze + @allowed_tags ||= %w[strong em del u mark p h1 h2 h3 h4 h5 h6 ul ol li img iframe audio video div figure + figcaption a].freeze end # Decifra el valor diff --git a/test/models/metadata/date_test.rb b/test/models/metadata/date_test.rb new file mode 100644 index 00000000..34b2ca7d --- /dev/null +++ b/test/models/metadata/date_test.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'test_helper' + +module Metadata + class DateTest < ActiveSupport::TestCase + DocumentStub = Struct.new(:data, keyword_init: true) + + attr_reader :document, :metadata + + setup do + @layout = { 'field' => { 'type' => 'date' } } + @document = DocumentStub.new(data: { 'field' => random_date }) + @metadata = MetadataDate.new(document: @document, name: :field) + end + + test 'la fecha por defecto es hoy' do + assert_equal Date.today, metadata.default_value + end + + test 'si el documento tiene un valor usamos ese' do + assert_equal document.data['field'], metadata.value + end + + test 'si el documento no tiene un valor usamos la fecha por defecto' do + document.data.delete 'field' + + assert_equal Date.today, metadata.value + end + + test 'si le damos un fecha mantenemos esa' do + date = random_date + metadata.value = date + + assert_equal date, metadata.value + end + + test 'si le damos un texto obtenemos una fecha' do + date = random_date.to_date + metadata.value = date.strftime('%F') + + assert_equal date, metadata.value + end + + private + + def random_date + (SecureRandom.random_number * 100).to_i.months.ago + end + end +end