2018-01-29 22:19:10 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-26 15:32:20 +00:00
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Esta clase representa un post en un sitio jekyll e incluye métodos
|
2019-08-06 23:17:29 +00:00
|
|
|
# para modificarlos y crear nuevos.
|
2018-02-03 22:37:09 +00:00
|
|
|
#
|
2019-08-06 23:17:29 +00:00
|
|
|
# rubocop:disable Metrics/ClassLength
|
2019-08-07 15:10:14 +00:00
|
|
|
# rubocop:disable Style/MissingRespondToMissing
|
2019-08-06 23:17:29 +00:00
|
|
|
class Post < OpenStruct
|
|
|
|
# Atributos por defecto
|
|
|
|
DEFAULT_ATTRIBUTES = %i[site document layout].freeze
|
|
|
|
# Otros atributos que no vienen en los metadatos
|
2019-09-17 21:27:51 +00:00
|
|
|
PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze
|
2020-01-02 23:29:04 +00:00
|
|
|
PUBLIC_ATTRIBUTES = %i[lang date uuid].freeze
|
2019-08-06 23:17:29 +00:00
|
|
|
|
2020-01-02 23:20:41 +00:00
|
|
|
class << self
|
|
|
|
# Obtiene el layout sin leer el Document
|
2020-06-16 22:21:38 +00:00
|
|
|
#
|
|
|
|
# TODO: Reemplazar cuando leamos el contenido del Document
|
|
|
|
# a demanda?
|
2020-01-02 23:20:41 +00:00
|
|
|
def find_layout(doc)
|
2020-06-16 22:21:38 +00:00
|
|
|
IO.foreach(doc.path).lazy.grep(/^layout: /).take(1).first
|
|
|
|
.try(:split, ' ').try(:last).try(:to_sym)
|
2020-01-02 23:20:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Redefinir el inicializador de OpenStruct
|
|
|
|
#
|
|
|
|
# @param site: [Site] el sitio en Sutty
|
|
|
|
# @param document: [Jekyll::Document] el documento leído por Jekyll
|
|
|
|
# @param layout: [Layout] la plantilla
|
|
|
|
#
|
|
|
|
def initialize(**args)
|
2020-09-29 21:22:28 +00:00
|
|
|
default_attributes_missing(**args)
|
|
|
|
super(**args)
|
2019-08-06 23:17:29 +00:00
|
|
|
|
|
|
|
# Genera un método con todos los atributos disponibles
|
2019-08-13 19:09:23 +00:00
|
|
|
self.attributes = layout.metadata.keys.map(&:to_sym) + PUBLIC_ATTRIBUTES
|
|
|
|
self.errors = {}
|
2019-08-06 23:17:29 +00:00
|
|
|
|
|
|
|
# Genera un atributo por cada uno de los campos de la plantilla,
|
|
|
|
# MetadataFactory devuelve un tipo de campo por cada campo. A
|
|
|
|
# partir de ahí se pueden obtener los valores actuales y una lista
|
|
|
|
# de valores por defecto.
|
2020-01-02 23:20:41 +00:00
|
|
|
#
|
|
|
|
# XXX: En el primer intento de hacerlo más óptimo, movimos esta
|
|
|
|
# lógica a instanciación bajo demanda, pero no solo no logramos
|
|
|
|
# optimizar sino que aumentamos el tiempo de carga :/
|
2019-08-06 23:17:29 +00:00
|
|
|
layout.metadata.each_pair do |name, template|
|
|
|
|
send "#{name}=".to_sym,
|
|
|
|
MetadataFactory.build(document: document,
|
2019-08-08 18:28:23 +00:00
|
|
|
post: self,
|
2019-08-06 23:17:29 +00:00
|
|
|
site: site,
|
2020-07-23 19:06:29 +00:00
|
|
|
name: name.to_sym,
|
2019-08-13 23:33:57 +00:00
|
|
|
value: args[name.to_sym],
|
2019-08-07 21:35:37 +00:00
|
|
|
layout: layout,
|
2019-08-06 23:17:29 +00:00
|
|
|
type: template['type'],
|
|
|
|
label: template['label'],
|
|
|
|
help: template['help'],
|
|
|
|
required: template['required'])
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
|
2020-01-02 23:20:41 +00:00
|
|
|
# TODO: Llamar dinámicamente
|
2019-09-17 21:27:51 +00:00
|
|
|
load_lang!
|
2019-08-07 21:35:37 +00:00
|
|
|
load_slug!
|
|
|
|
load_date!
|
2019-08-08 18:28:23 +00:00
|
|
|
load_path!
|
2020-01-02 23:29:04 +00:00
|
|
|
load_uuid!
|
2019-08-07 21:35:37 +00:00
|
|
|
|
2020-01-02 23:20:41 +00:00
|
|
|
# XXX: No usamos Post#read porque a esta altura todavía no sabemos
|
|
|
|
# nada del Document
|
|
|
|
document.read! if File.exist? document.path
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
2020-06-16 22:21:38 +00:00
|
|
|
def inspect
|
|
|
|
"#<Post id=\"#{id}\">"
|
|
|
|
end
|
|
|
|
|
2020-05-11 20:28:38 +00:00
|
|
|
# Devuelve una llave para poder guardar el post en una cache
|
|
|
|
def cache_key
|
|
|
|
'posts/' + uuid.value
|
|
|
|
end
|
|
|
|
|
2020-05-12 15:50:22 +00:00
|
|
|
def cache_version
|
|
|
|
updated_at.utc.to_s(:usec)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Agregar el timestamp para saber si cambió, siguiendo el módulo
|
|
|
|
# ActiveRecord::Integration
|
|
|
|
def cache_key_with_version
|
|
|
|
cache_key + '-' + cache_version
|
|
|
|
end
|
|
|
|
|
2020-01-02 23:29:04 +00:00
|
|
|
# TODO: Convertir a UUID?
|
2019-08-13 19:09:23 +00:00
|
|
|
def id
|
|
|
|
path.basename
|
|
|
|
end
|
2020-05-12 15:50:22 +00:00
|
|
|
alias to_param id
|
2019-08-13 19:09:23 +00:00
|
|
|
|
2020-01-02 23:29:04 +00:00
|
|
|
def updated_at
|
|
|
|
File.mtime(path.absolute)
|
2019-11-06 22:35:48 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Solo ejecuta la magia de OpenStruct si el campo existe en la
|
|
|
|
# plantilla
|
|
|
|
#
|
|
|
|
# XXX: Reemplazarlo por nuestro propio método, mantener todo lo demás
|
|
|
|
# compatible con OpenStruct
|
|
|
|
#
|
|
|
|
# XXX: rubocop dice que tenemos que usar super cuando ya lo estamos
|
|
|
|
# usando...
|
|
|
|
def method_missing(mid, *args)
|
2020-01-02 23:29:53 +00:00
|
|
|
# Limpiar el nombre del atributo, para que todos los ayudantes
|
|
|
|
# reciban el método en limpio
|
|
|
|
name = attribute_name mid
|
|
|
|
|
|
|
|
unless attribute? name
|
2019-08-06 23:17:29 +00:00
|
|
|
raise NoMethodError, I18n.t('exceptions.post.no_method',
|
|
|
|
method: mid)
|
|
|
|
end
|
2018-12-14 15:27:50 +00:00
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
# Definir los attribute_*
|
2020-01-02 23:29:53 +00:00
|
|
|
new_attribute_was(name)
|
|
|
|
new_attribute_changed(name)
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
# OpenStruct
|
2019-08-06 23:17:29 +00:00
|
|
|
super(mid, *args)
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
# Devolver lo mismo que devuelve el método después de definirlo
|
|
|
|
send(mid, *args)
|
2018-12-14 15:27:50 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Detecta si es un atributo válido o no, a partir de la tabla de la
|
|
|
|
# plantilla
|
|
|
|
def attribute?(mid)
|
2020-01-02 18:08:45 +00:00
|
|
|
included = DEFAULT_ATTRIBUTES.include?(mid) ||
|
|
|
|
PRIVATE_ATTRIBUTES.include?(mid) ||
|
|
|
|
PUBLIC_ATTRIBUTES.include?(mid)
|
|
|
|
|
2020-06-16 22:21:38 +00:00
|
|
|
included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes)
|
2020-01-02 18:08:45 +00:00
|
|
|
|
|
|
|
included
|
2018-04-27 18:48:26 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
# Devuelve los strong params para el layout
|
|
|
|
def params
|
|
|
|
attributes.map do |attr|
|
|
|
|
send(attr).to_param
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Genera el post con metadatos en YAML
|
2020-06-16 22:21:38 +00:00
|
|
|
#
|
|
|
|
# TODO: Cachear por un minuto
|
2019-08-06 23:17:29 +00:00
|
|
|
def full_content
|
2019-08-13 23:33:57 +00:00
|
|
|
body = ''
|
2019-08-06 23:17:29 +00:00
|
|
|
yaml = layout.metadata.keys.map(&:to_sym).map do |metadata|
|
|
|
|
template = send(metadata)
|
2018-04-27 18:48:26 +00:00
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
unless template.front_matter?
|
|
|
|
body += "\n\n"
|
|
|
|
body += template.value
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
next if template.empty?
|
|
|
|
|
|
|
|
{ metadata.to_s => template.value }
|
2019-08-06 23:17:29 +00:00
|
|
|
end.compact.inject(:merge)
|
2018-02-22 19:01:11 +00:00
|
|
|
|
2020-01-02 23:29:04 +00:00
|
|
|
# TODO: Convertir a Metadata?
|
2019-08-07 15:10:14 +00:00
|
|
|
# Asegurarse que haya un layout
|
|
|
|
yaml['layout'] = layout.name.to_s
|
2020-01-02 23:29:04 +00:00
|
|
|
yaml['uuid'] = uuid.value
|
2019-08-16 23:32:17 +00:00
|
|
|
# Y que no se procese liquid
|
|
|
|
yaml['liquid'] = false
|
2019-09-25 22:31:33 +00:00
|
|
|
yaml['usuaries'] = usuaries.map(&:id).uniq
|
2019-08-07 15:10:14 +00:00
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
"#{yaml.to_yaml}---\n\n#{body}"
|
2018-02-24 21:24:11 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Eliminar el artículo del repositorio y de la lista de artículos del
|
|
|
|
# sitio
|
|
|
|
def destroy
|
2019-08-08 18:28:23 +00:00
|
|
|
FileUtils.rm_f path.absolute
|
2018-02-26 18:58:56 +00:00
|
|
|
|
2020-02-12 21:24:54 +00:00
|
|
|
site.delete_post self
|
2018-02-22 19:01:11 +00:00
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
alias destroy! destroy
|
2018-02-22 19:01:11 +00:00
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# Guarda los cambios
|
2020-06-16 22:21:38 +00:00
|
|
|
# TODO: Agregar una forma de congelar todos los valores y solo guardar
|
|
|
|
# uno, para no incorporar modificaciones
|
2019-08-22 01:09:29 +00:00
|
|
|
# rubocop:disable Metrics/CyclomaticComplexity
|
2019-12-11 20:05:31 +00:00
|
|
|
def save(validate: true)
|
|
|
|
return false if validate && !valid?
|
2019-08-08 18:28:23 +00:00
|
|
|
# Salir si tenemos que cambiar el nombre del archivo y no pudimos
|
2019-08-08 19:26:47 +00:00
|
|
|
return false if !new? && path_changed? && !update_path!
|
2019-08-22 01:09:29 +00:00
|
|
|
return false unless save_attributes!
|
2019-08-07 15:10:14 +00:00
|
|
|
return false unless write
|
2018-01-29 22:19:10 +00:00
|
|
|
|
|
|
|
# Vuelve a leer el post para tomar los cambios
|
2019-08-08 18:28:23 +00:00
|
|
|
read
|
2019-08-07 15:10:14 +00:00
|
|
|
|
2019-09-16 17:56:34 +00:00
|
|
|
written?
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2019-08-22 01:09:29 +00:00
|
|
|
# rubocop:enable Metrics/CyclomaticComplexity
|
2019-03-26 15:32:20 +00:00
|
|
|
alias save! save
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-09-16 17:56:34 +00:00
|
|
|
# Actualiza la ruta del documento y lo lee
|
2019-08-07 21:35:37 +00:00
|
|
|
def read
|
2019-09-16 17:56:34 +00:00
|
|
|
return unless written?
|
|
|
|
|
|
|
|
document.path = path.absolute
|
2020-01-02 23:20:41 +00:00
|
|
|
document.read!
|
2019-08-08 19:26:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new?
|
|
|
|
document.path.blank?
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
|
2019-09-16 17:56:34 +00:00
|
|
|
def written?
|
|
|
|
File.exist? path.absolute
|
|
|
|
end
|
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
# Actualizar la ubicación del archivo si cambió de lugar y si no
|
|
|
|
# existe el destino
|
|
|
|
def update_path!
|
|
|
|
!File.exist?(path.absolute) &&
|
|
|
|
FileUtils.mv(path_was, path.absolute) &&
|
|
|
|
document.path = path.absolute
|
2019-08-07 21:35:37 +00:00
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Detecta si el artículo es válido para guardar
|
|
|
|
def valid?
|
|
|
|
self.errors = {}
|
2018-02-02 22:20:31 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
layout.metadata.keys.map(&:to_sym).each do |metadata|
|
|
|
|
template = send(metadata)
|
2018-07-23 19:48:34 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
errors[metadata] = template.errors unless template.valid?
|
2018-07-27 13:45:32 +00:00
|
|
|
end
|
2019-12-11 20:05:31 +00:00
|
|
|
|
|
|
|
errors.blank?
|
2018-06-25 20:44:47 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Guarda los cambios en el archivo destino
|
|
|
|
def write
|
2019-08-07 15:10:14 +00:00
|
|
|
return true if persisted?
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
Site::Writer.new(site: site, file: path.absolute,
|
2019-08-09 18:45:08 +00:00
|
|
|
content: full_content).save
|
2019-08-07 15:10:14 +00:00
|
|
|
end
|
2018-02-02 22:20:31 +00:00
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# Verifica si hace falta escribir cambios
|
2020-06-16 22:21:38 +00:00
|
|
|
#
|
|
|
|
# TODO: Cachear el resultado o usar otro método, por ejemplo guardando
|
|
|
|
# la fecha de modificación al leer y compararla al hacer cambios sin
|
|
|
|
# escribirlos.
|
2019-08-07 15:10:14 +00:00
|
|
|
def persisted?
|
2019-08-08 18:28:23 +00:00
|
|
|
File.exist?(path.absolute) && full_content == File.read(path.absolute)
|
2018-02-02 22:20:31 +00:00
|
|
|
end
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2019-08-16 23:12:22 +00:00
|
|
|
def destroyed?
|
|
|
|
!File.exist?(path.absolute)
|
|
|
|
end
|
|
|
|
|
2019-08-13 23:33:57 +00:00
|
|
|
def update_attributes(hashable)
|
|
|
|
hashable.to_hash.each do |name, value|
|
|
|
|
self[name].value = value
|
|
|
|
end
|
|
|
|
|
|
|
|
save
|
|
|
|
end
|
2019-09-17 21:27:51 +00:00
|
|
|
alias update update_attributes
|
2019-08-13 23:33:57 +00:00
|
|
|
|
2019-12-11 20:10:01 +00:00
|
|
|
# El Document guarda un Array de los ids de Usuarie. Si está vacío,
|
|
|
|
# no hacemos una consulta vacía. Si no, traemos todes les Usuaries
|
|
|
|
# por su id y convertimos a Array para poder agregar o quitar luego
|
|
|
|
# sin pasar por ActiveRecord.
|
2019-09-25 22:31:33 +00:00
|
|
|
def usuaries
|
2019-12-11 20:10:01 +00:00
|
|
|
@usuaries ||= if (d = document_usuaries).empty?
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
Usuarie.where(id: d).to_a
|
|
|
|
end
|
2019-09-25 22:31:33 +00:00
|
|
|
end
|
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
private
|
|
|
|
|
2020-01-02 18:08:45 +00:00
|
|
|
# Levanta un error si al construir el artículo no pasamos un atributo.
|
|
|
|
def default_attributes_missing(**args)
|
|
|
|
DEFAULT_ATTRIBUTES.each do |attr|
|
|
|
|
i18n = I18n.t("exceptions.post.#{attr}_missing")
|
|
|
|
|
|
|
|
raise ArgumentError, i18n unless args[attr].present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-25 22:31:33 +00:00
|
|
|
def document_usuaries
|
|
|
|
document.data.fetch('usuaries', [])
|
|
|
|
end
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
def new_attribute_was(method)
|
2020-01-02 23:29:53 +00:00
|
|
|
attr_was = "#{method}_was".to_sym
|
2019-08-07 21:35:37 +00:00
|
|
|
return attr_was if singleton_class.method_defined? attr_was
|
|
|
|
|
|
|
|
define_singleton_method(attr_was) do
|
|
|
|
name = attribute_name(attr_was)
|
2019-08-08 18:28:23 +00:00
|
|
|
if document.respond_to?(name)
|
|
|
|
document.send(name)
|
|
|
|
else
|
|
|
|
document.data[name.to_s]
|
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
end
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
# Pregunta si el atributo cambió
|
|
|
|
def new_attribute_changed(method)
|
2020-01-02 23:29:53 +00:00
|
|
|
attr_changed = "#{method}_changed?".to_sym
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
return attr_changed if singleton_class.method_defined? attr_changed
|
|
|
|
|
|
|
|
define_singleton_method(attr_changed) do
|
|
|
|
name = attribute_name(attr_changed)
|
|
|
|
name_was = (name.to_s + '_was').to_sym
|
|
|
|
|
|
|
|
(send(name).try(:value) || send(name)) != send(name_was)
|
|
|
|
end
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
2018-05-02 18:38:46 +00:00
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
# Obtiene el nombre del atributo a partir del nombre del método
|
2019-08-06 23:17:29 +00:00
|
|
|
def attribute_name(attr)
|
2019-08-07 21:35:37 +00:00
|
|
|
# XXX: Los simbolos van al final
|
|
|
|
%w[_was _changed? ? =].reduce(attr.to_s) do |a, suffix|
|
|
|
|
a.chomp suffix
|
|
|
|
end.to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_slug!
|
|
|
|
self.slug = MetadataSlug.new(document: document, site: site,
|
2019-08-08 18:28:23 +00:00
|
|
|
layout: layout, name: :slug, type: :slug,
|
|
|
|
post: self,
|
2019-08-07 21:35:37 +00:00
|
|
|
required: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_date!
|
|
|
|
self.date = MetadataDocumentDate.new(document: document, site: site,
|
|
|
|
layout: layout, name: :date,
|
2019-08-08 18:28:23 +00:00
|
|
|
type: :document_date,
|
|
|
|
post: self,
|
2019-08-07 21:35:37 +00:00
|
|
|
required: true)
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
2019-08-08 18:28:23 +00:00
|
|
|
|
|
|
|
def load_path!
|
|
|
|
self.path = MetadataPath.new(document: document, site: site,
|
|
|
|
layout: layout, name: :path,
|
|
|
|
type: :path, post: self,
|
|
|
|
required: true)
|
|
|
|
end
|
2019-08-22 01:09:29 +00:00
|
|
|
|
2019-09-17 21:27:51 +00:00
|
|
|
def load_lang!
|
|
|
|
self.lang = MetadataLang.new(document: document, site: site,
|
|
|
|
layout: layout, name: :lang,
|
|
|
|
type: :lang, post: self,
|
|
|
|
required: true)
|
|
|
|
end
|
|
|
|
|
2020-01-02 23:29:04 +00:00
|
|
|
def load_uuid!
|
|
|
|
self.uuid = MetadataUuid.new(document: document, site: site,
|
|
|
|
layout: layout, name: :uuid,
|
|
|
|
type: :uuid, post: self,
|
|
|
|
required: true)
|
|
|
|
end
|
|
|
|
|
2019-08-22 01:09:29 +00:00
|
|
|
# Ejecuta la acción de guardado en cada atributo
|
|
|
|
def save_attributes!
|
|
|
|
attributes.map do |attr|
|
|
|
|
send(attr).save
|
|
|
|
end.all?
|
|
|
|
end
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
# rubocop:enable Metrics/ClassLength
|
2019-08-07 15:10:14 +00:00
|
|
|
# rubocop:enable Style/MissingRespondToMissing
|