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
|
|
|
#
|
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
|
2021-02-24 20:37:13 +00:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class} site=#{site.name.inspect} post=#{post.id.inspect} value=#{value.inspect}>"
|
|
|
|
end
|
|
|
|
|
2020-11-11 20:00:09 +00:00
|
|
|
# Queremos que los artículos nuevos siempre cacheen, si usamos el UUID
|
|
|
|
# siempre vamos a obtener un item nuevo.
|
|
|
|
def cache_key
|
2020-12-28 20:55:39 +00:00
|
|
|
return layout.value + '/' + name.to_s if post.new?
|
2020-11-11 20:00:09 +00:00
|
|
|
|
|
|
|
@cache_key ||= 'post/' + post.uuid.value + '/' + name.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def cache_version
|
2020-11-16 22:17:40 +00:00
|
|
|
value.hash.to_s + values.hash.to_s
|
2020-11-11 20:00:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cache_key_with_version
|
|
|
|
cache_key + '-' + cache_version
|
|
|
|
end
|
|
|
|
|
2021-02-11 19:45:03 +00:00
|
|
|
# XXX: Deberíamos sanitizar durante la asignación?
|
2020-10-04 01:32:52 +00:00
|
|
|
def value=(new_value)
|
|
|
|
@value_was = value
|
|
|
|
self[:value] = new_value
|
|
|
|
end
|
|
|
|
|
2021-02-11 18:27:44 +00:00
|
|
|
# Siempre obtener el valor actual y solo obtenerlo del documento una
|
|
|
|
# vez.
|
|
|
|
def value_was
|
|
|
|
return @value_was if instance_variable_defined? '@value_was'
|
|
|
|
|
|
|
|
@value_was = value_from_document
|
|
|
|
end
|
|
|
|
|
|
|
|
def value_from_document
|
|
|
|
@value_from_document ||= document.data[name.to_s]
|
|
|
|
end
|
|
|
|
|
2020-10-04 01:32:52 +00:00
|
|
|
def changed?
|
2021-02-11 18:27:44 +00:00
|
|
|
value_was != value
|
2020-10-04 01:32:52 +00:00
|
|
|
end
|
|
|
|
|
2020-12-24 19:13:29 +00:00
|
|
|
# Obtiene el valor del JekyllDocument
|
|
|
|
def document_value
|
|
|
|
document.data[name.to_s]
|
|
|
|
end
|
|
|
|
|
2020-11-11 18:29:12 +00:00
|
|
|
# Trae el idioma actual del sitio o del panel
|
|
|
|
# @return [String]
|
|
|
|
def lang
|
2021-03-03 12:43:15 +00:00
|
|
|
@lang ||= post&.lang&.value || I18n.locale
|
2020-11-11 18:29:12 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 17:54:17 +00:00
|
|
|
# El valor por defecto
|
|
|
|
def default_value
|
2021-03-03 12:43:15 +00:00
|
|
|
layout.metadata.dig(name, 'default', lang.to_s)
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Valores posibles, busca todos los valores actuales en otros
|
|
|
|
# artículos del mismo sitio
|
|
|
|
def values
|
2020-11-11 18:29:12 +00:00
|
|
|
site.everything_of(name, lang: lang)
|
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
|
2021-02-11 18:27:44 +00:00
|
|
|
self[:value] ||= if (data = value_from_document).present?
|
2020-11-26 16:17:44 +00:00
|
|
|
private? ? decrypt(data) : data
|
2020-08-20 23:38:31 +00:00
|
|
|
else
|
2020-11-26 16:17:44 +00:00
|
|
|
default_value
|
2020-08-20 23:38:31 +00:00
|
|
|
end
|
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
|
2021-02-11 19:45:03 +00:00
|
|
|
return true unless changed?
|
|
|
|
|
2020-02-12 21:24:54 +00:00
|
|
|
self[:value] = sanitize value
|
2020-08-20 23:38:31 +00:00
|
|
|
self[:value] = encrypt(value) if private?
|
|
|
|
|
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
|
2021-02-11 18:27:44 +00:00
|
|
|
@related_methods ||= [].freeze
|
2020-07-22 23:35:43 +00:00
|
|
|
end
|
|
|
|
|
2020-08-20 23:38:31 +00:00
|
|
|
# Determina si el campo es privado y debería ser cifrado
|
|
|
|
def private?
|
2021-02-11 19:45:03 +00:00
|
|
|
layout.metadata.dig(name, 'private').present?
|
2020-08-20 23:38:31 +00:00
|
|
|
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-11-16 16:15:23 +00:00
|
|
|
attributes: allowed_attributes).strip.html_safe
|
2020-11-16 16:09:04 +00:00
|
|
|
end
|
2020-11-14 21:02:30 +00:00
|
|
|
|
|
|
|
def sanitizer
|
|
|
|
@sanitizer ||= Rails::Html::Sanitizer.safe_list_sanitizer.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_attributes
|
2021-03-26 13:27:50 +00:00
|
|
|
@allowed_attributes ||= %w[style href src alt controls data-align data-multimedia data-multimedia-inner id
|
|
|
|
name].freeze
|
2020-11-14 21:02:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_tags
|
2020-12-24 19:13:29 +00:00
|
|
|
@allowed_tags ||= %w[strong em del u mark p h1 h2 h3 h4 h5 h6 ul ol li img iframe audio video div figure
|
|
|
|
figcaption a].freeze
|
2020-02-12 21:24:54 +00:00
|
|
|
end
|
2020-08-20 23:38:31 +00:00
|
|
|
|
|
|
|
# Decifra el valor
|
|
|
|
#
|
|
|
|
# XXX: Otros tipos de valores necesitan implementar su propio método
|
|
|
|
# de decifrado (Array).
|
|
|
|
def decrypt(value)
|
|
|
|
return value if value.blank?
|
|
|
|
|
|
|
|
box.decrypt_str value.to_s
|
|
|
|
rescue Lockbox::DecryptionError => e
|
2021-02-11 14:26:17 +00:00
|
|
|
ExceptionNotifier.notify_exception(e, data: { site: site.name, post: post.path.absolute, name: name })
|
2020-08-20 23:38:31 +00:00
|
|
|
|
|
|
|
I18n.t('lockbox.help.decryption_error')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Cifra el valor.
|
|
|
|
#
|
|
|
|
# XXX: Otros tipos de valores necesitan implementar su propio método
|
|
|
|
# de cifrado (Array).
|
|
|
|
def encrypt(value)
|
|
|
|
box.encrypt value.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
# Genera una lockbox a partir de la llave privada del sitio
|
|
|
|
#
|
|
|
|
# @return [Lockbox]
|
|
|
|
def box
|
|
|
|
@box ||= Lockbox.new key: site.private_key, padding: true, encode: true
|
|
|
|
end
|
2019-08-06 17:54:17 +00:00
|
|
|
end
|