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
|
|
|
require 'jekyll/utils'
|
|
|
|
|
|
|
|
# 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/MethodMissingSuper
|
|
|
|
# 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-08-13 19:09:23 +00:00
|
|
|
PRIVATE_ATTRIBUTES = %i[lang path slug attributes errors].freeze
|
|
|
|
PUBLIC_ATTRIBUTES = %i[date].freeze
|
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)
|
|
|
|
default_attributes_missing(args)
|
|
|
|
super(args)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
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,
|
|
|
|
name: name,
|
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
|
|
|
|
|
|
|
load_slug!
|
|
|
|
load_date!
|
2019-08-08 18:28:23 +00:00
|
|
|
load_path!
|
2019-08-07 21:35:37 +00:00
|
|
|
|
|
|
|
# Leer el documento
|
|
|
|
read
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 19:09:23 +00:00
|
|
|
def id
|
|
|
|
path.basename
|
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +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")
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
raise ArgumentError, i18n unless args[attr].present?
|
|
|
|
end
|
2018-01-29 22:19:10 +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)
|
|
|
|
unless attribute? mid
|
|
|
|
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_*
|
|
|
|
new_attribute_was(mid)
|
|
|
|
new_attribute_changed(mid)
|
|
|
|
|
|
|
|
# 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)
|
2019-08-13 19:09:23 +00:00
|
|
|
attrs = DEFAULT_ATTRIBUTES + PRIVATE_ATTRIBUTES + PUBLIC_ATTRIBUTES
|
2019-08-06 23:17:29 +00:00
|
|
|
if singleton_class.method_defined? :attributes
|
2019-08-13 19:09:23 +00:00
|
|
|
(attrs + attributes).include? attribute_name(mid)
|
2019-08-06 23:17:29 +00:00
|
|
|
else
|
2019-08-13 19:09:23 +00:00
|
|
|
attrs.include? attribute_name(mid)
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
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
|
|
|
|
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
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# Asegurarse que haya un layout
|
|
|
|
yaml['layout'] = layout.name.to_s
|
|
|
|
|
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
|
|
|
|
#
|
|
|
|
# XXX Commit
|
|
|
|
def destroy
|
2019-08-08 18:28:23 +00:00
|
|
|
FileUtils.rm_f path.absolute
|
2018-02-26 18:58:56 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
site.posts(lang: lang).delete_if do |post|
|
2019-08-08 18:28:23 +00:00
|
|
|
post.path.absolute == path.absolute
|
2019-08-06 23:17:29 +00:00
|
|
|
end
|
2019-03-26 15:32:20 +00:00
|
|
|
|
2019-08-08 18:28:23 +00:00
|
|
|
!File.exist?(path.absolute) && !site.posts(lang: lang).include?(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
|
2018-01-29 22:19:10 +00:00
|
|
|
def save
|
2018-02-03 22:37:09 +00:00
|
|
|
return false unless 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-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
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
true
|
|
|
|
end
|
2019-03-26 15:32:20 +00:00
|
|
|
alias save! save
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-08 19:26:47 +00:00
|
|
|
# Lee el documento a menos que estemos trabajando con un documento en
|
|
|
|
# blanco
|
2019-08-07 21:35:37 +00:00
|
|
|
def read
|
2019-08-08 19:26:47 +00:00
|
|
|
document.read unless new?
|
|
|
|
end
|
|
|
|
|
|
|
|
def new?
|
|
|
|
document.path.blank?
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
2019-08-07 21:35:37 +00:00
|
|
|
|
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?
|
|
|
|
validate
|
|
|
|
errors.blank?
|
2018-02-24 21:24:11 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Requisitos para que el post sea válido
|
|
|
|
def validate
|
|
|
|
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
|
2018-06-25 20:44:47 +00:00
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
alias validate! validate
|
2018-06-25 20:44:47 +00:00
|
|
|
|
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
|
|
|
|
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-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-08-07 15:10:14 +00:00
|
|
|
private
|
|
|
|
|
2019-08-07 21:35:37 +00:00
|
|
|
def new_attribute_was(method)
|
|
|
|
attr_was = (attribute_name(method).to_s + '_was').to_sym
|
|
|
|
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)
|
|
|
|
attr_changed = (attribute_name(method).to_s + '_changed?').to_sym
|
|
|
|
|
|
|
|
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
|
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/MethodMissingSuper
|
|
|
|
# rubocop:enable Style/MissingRespondToMissing
|