2019-08-06 17:54:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Representa la plantilla de un campo en los metadatos del artículo
|
2019-08-06 23:17:29 +00:00
|
|
|
#
|
|
|
|
# TODO: Validar el tipo de valor pasado a value= según el :type
|
2019-08-13 23:33:57 +00:00
|
|
|
#
|
|
|
|
# rubocop:disable Metrics/BlockLength
|
2019-08-06 17:54:17 +00:00
|
|
|
MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
|
2019-08-08 18:28:23 +00:00
|
|
|
:value, :help, :required, :errors, :post,
|
2019-08-07 21:35:37 +00:00
|
|
|
:layout, keyword_init: true) do
|
2019-08-06 17:54:17 +00:00
|
|
|
# El valor por defecto
|
|
|
|
def default_value
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
# Valores posibles, busca todos los valores actuales en otros
|
|
|
|
# artículos del mismo sitio
|
|
|
|
def values
|
2019-08-06 23:17:29 +00:00
|
|
|
site.everything_of(name)
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Valor actual o por defecto
|
|
|
|
def value
|
2019-08-06 23:17:29 +00:00
|
|
|
self[:value] || document.data.fetch(name.to_s, default_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si el valor está vacío
|
|
|
|
def empty?
|
|
|
|
value.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Comprueba si el metadato es válido
|
|
|
|
def valid?
|
|
|
|
validate
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate
|
|
|
|
self.errors = []
|
|
|
|
|
|
|
|
errors << I18n.t("metadata.#{type}.cant_be_empty") unless can_be_empty?
|
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
def to_param
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Decide si el metadato se coloca en el front_matter o no
|
|
|
|
def front_matter?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-08-16 23:25:07 +00:00
|
|
|
def array?
|
|
|
|
type == 'array'
|
|
|
|
end
|
|
|
|
|
2019-08-22 01:09:29 +00:00
|
|
|
# En caso de que algún campo necesite realizar acciones antes de ser
|
|
|
|
# guardado
|
|
|
|
def save
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# Si es obligatorio no puede estar vacío
|
|
|
|
def can_be_empty?
|
|
|
|
true unless required && empty?
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
# rubocop:enable Metrics/BlockLength
|