5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 19:56:21 +00:00

guardar fechas

This commit is contained in:
f 2020-12-24 16:13:29 -03:00
parent 3f19e79366
commit c338cb87de
3 changed files with 63 additions and 3 deletions

View file

@ -5,13 +5,16 @@ class MetadataDate < MetadataTemplate
Date.today Date.today
end end
# Ver MetadataDocumentDate # Devuelve una fecha, si no hay ninguna es la fecha de hoy.
#
# @return [Date]
def value def value
return self[:value] if self[:value].is_a? Date return self[:value] if self[:value].is_a? Date
return self[:value] if self[:value].is_a? Time return self[:value] if self[:value].is_a? Time
return document_value || default_value unless self[:value]
begin begin
self[:value] = Date.parse(self[:value] || document.data[name.to_s]) self[:value] = Date.parse self[:value]
rescue ArgumentError, TypeError rescue ArgumentError, TypeError
default_value default_value
end end

View file

@ -34,6 +34,11 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
!value_was.nil? && value_was != value !value_was.nil? && value_was != value
end end
# Obtiene el valor del JekyllDocument
def document_value
document.data[name.to_s]
end
# Trae el idioma actual del sitio o del panel # Trae el idioma actual del sitio o del panel
# @return [String] # @return [String]
def lang def lang
@ -154,7 +159,8 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
end end
def allowed_tags 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 end
# Decifra el valor # Decifra el valor

View file

@ -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