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
|
2020-02-12 21:24:54 +00:00
|
|
|
include ActionText::ContentHelper
|
|
|
|
|
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
|
2019-12-11 20:05:31 +00:00
|
|
|
#
|
|
|
|
# TODO: Implementar lang!
|
2019-08-06 17:54:17 +00:00
|
|
|
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
|
2020-02-12 21:24:54 +00:00
|
|
|
#
|
|
|
|
# XXX: No estamos sanitizando la entrada, cada tipo tiene que
|
|
|
|
# auto-sanitizarse.
|
2019-08-06 17:54:17 +00:00
|
|
|
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
|
2020-02-12 21:24:54 +00:00
|
|
|
self[:value] = sanitize value
|
2019-08-22 01:09:29 +00:00
|
|
|
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
|
2020-02-12 21:24:54 +00:00
|
|
|
|
|
|
|
# No usamos sanitize_action_text_content porque espera un ActionText
|
|
|
|
#
|
|
|
|
# Ver ActionText::ContentHelper#sanitize_action_text_content
|
|
|
|
def sanitize(string)
|
|
|
|
return unless string
|
|
|
|
return string unless string.is_a? String
|
|
|
|
|
|
|
|
sanitizer.sanitize(string,
|
|
|
|
tags: allowed_tags,
|
|
|
|
attributes: allowed_attributes,
|
|
|
|
scrubber: scrubber).strip.html_safe
|
|
|
|
end
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
# rubocop:enable Metrics/BlockLength
|