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-06 17:54:17 +00:00
|
|
|
MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
|
2019-08-06 23:17:29 +00:00
|
|
|
:value, :help, :required, :errors,
|
2019-08-06 17:54:17 +00:00
|
|
|
keyword_init: true) do
|
|
|
|
# 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
|
|
|
|
|
|
|
|
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
|