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
|
|
|
|
def values
|
2020-05-23 15:38:03 +00:00
|
|
|
site.everything_of(name, lang: post.try(:lang).try(:value))
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|
|
|
|
|
2020-07-21 22:19:40 +00:00
|
|
|
# Valor actual o por defecto. Al memoizarlo podemos modificarlo
|
|
|
|
# usando otros métodos que el de asignación.
|
2019-08-06 17:54:17 +00:00
|
|
|
def value
|
2020-07-21 22:19:40 +00:00
|
|
|
self[:value] ||= document.data.fetch(name.to_s, default_value)
|
2019-08-06 23:17:29 +00:00
|
|
|
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 = []
|
|
|
|
|
2020-07-02 14:26:00 +00:00
|
|
|
errors << I18n.t('metadata.cant_be_empty') unless can_be_empty?
|
2019-08-06 23:17:29 +00:00
|
|
|
|
|
|
|
errors.empty?
|
|
|
|
end
|
|
|
|
|
2020-07-21 22:34:15 +00:00
|
|
|
# Usa el valor por defecto para generar el formato de StrongParam
|
|
|
|
# necesario.
|
|
|
|
#
|
|
|
|
# @return [Symbol,Hash]
|
2019-08-13 23:33:57 +00:00
|
|
|
def to_param
|
2020-07-21 22:34:15 +00:00
|
|
|
case default_value
|
|
|
|
when Hash then { name => default_value.keys.map(&:to_sym) }
|
|
|
|
when Array then { name => [] }
|
|
|
|
else name
|
|
|
|
end
|
2019-08-13 23:33:57 +00:00
|
|
|
end
|
|
|
|
|
2020-05-23 18:42:12 +00:00
|
|
|
def to_s
|
|
|
|
value.to_s
|
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
# 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
|
|
|
|
|
2020-07-22 23:35:43 +00:00
|
|
|
def related_posts?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def related_methods
|
|
|
|
raise NotImplementedError
|
|
|
|
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)
|
2020-06-16 22:20:22 +00:00
|
|
|
return if string.nil?
|
2020-02-12 21:24:54 +00:00
|
|
|
return string unless string.is_a? String
|
|
|
|
|
2020-08-15 14:58:28 +00:00
|
|
|
sanitizer.sanitize(string.tr("\r", ''),
|
2020-02-12 21:24:54 +00:00
|
|
|
tags: allowed_tags,
|
2020-03-17 17:49:39 +00:00
|
|
|
attributes: allowed_attributes + %w[data-trix-attachment],
|
2020-02-12 21:24:54 +00:00
|
|
|
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
|