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

Merge branch 'vali-date' into 'rails'

validar la fecha

Closes #1572 and #1573

See merge request sutty/sutty!31
This commit is contained in:
Maki 2021-05-17 15:58:59 +00:00
commit 36f256beca
2 changed files with 40 additions and 6 deletions

View file

@ -8,16 +8,52 @@ class MetadataDocumentDate < MetadataTemplate
end
def value_from_document
return nil if post.new?
document.date
end
# Siempre es obligatorio
def required
true
end
def validate
super
errors << I18n.t('metadata.date.invalid_format') unless valid_format?
errors.empty?
end
# El valor puede ser un Date, Time o una String en el formato
# "yyyy-mm-dd"
#
# XXX: Date.iso8601 acepta fechas en el futuro lejano, como 20000,
# pero Jekyll las limita a cuatro cifras, así que vamos a mantener
# eso.
#
# @see {https://github.com/jekyll/jekyll/blob/master/lib/jekyll/document.rb#L15}
def value
return (self[:value] = value_from_document || default_value) if self[:value].nil?
self[:value] =
case self[:value]
when String
begin
Date.iso8601(self[:value]).to_time
rescue Date::Error
value_from_document || default_value
end
else
self[:value] || value_from_document || default_value
end
end
self[:value] = Date.iso8601(self[:value]).to_time if self[:value].is_a? String
private
self[:value]
def valid_format?
return true if self[:value].is_a?(Time)
@valid_format_re ||= /\A\d{2,4}-\d{1,2}-\d{1,2}\z/
@valid_format_re =~ self[:value].to_s
end
end

View file

@ -49,9 +49,7 @@ class Post
public_send(attr)&.value = args[attr] if args.key?(attr)
end
# XXX: No usamos Post#read porque a esta altura todavía no sabemos
# nada del Document
document.read! if File.exist? document.path
document.read! unless new?
end
def inspect