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
|
|
|
|
# XXX: Volver document opcional cuando estemos creando
|
|
|
|
DEFAULT_ATTRIBUTES = %i[site document layout].freeze
|
|
|
|
# Otros atributos que no vienen en los metadatos
|
|
|
|
ATTRIBUTES = %i[content lang date slug attributes errors].freeze
|
|
|
|
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
# rubocop:disable Metrics/AbcSize
|
|
|
|
# rubocop:disable Metrics/MethodLength
|
|
|
|
def initialize(**args)
|
|
|
|
default_attributes_missing(args)
|
|
|
|
super(args)
|
|
|
|
|
|
|
|
# Genera un método con todos los atributos disponibles
|
|
|
|
self.attributes = DEFAULT_ATTRIBUTES +
|
|
|
|
ATTRIBUTES +
|
|
|
|
layout.metadata.keys.map(&:to_sym)
|
|
|
|
|
|
|
|
# El contenido
|
2019-08-07 15:10:14 +00:00
|
|
|
# TODO: Mover a su propia clase para poder hacer limpiezas
|
|
|
|
# independientemente
|
2019-08-06 23:17:29 +00:00
|
|
|
self.content = document.content
|
|
|
|
self.date = document.date
|
2019-08-07 15:10:14 +00:00
|
|
|
# TODO: idem content
|
2019-08-06 23:17:29 +00:00
|
|
|
self.slug = document.data['slug']
|
|
|
|
|
|
|
|
# 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,
|
|
|
|
site: site,
|
|
|
|
name: name,
|
|
|
|
type: template['type'],
|
|
|
|
label: template['label'],
|
|
|
|
help: template['help'],
|
|
|
|
required: template['required'])
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
end
|
2019-08-06 23:17:29 +00:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
|
|
|
# rubocop:enable Metrics/MethodLength
|
2018-02-03 22:37:09 +00:00
|
|
|
|
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-06 23:17:29 +00:00
|
|
|
super(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)
|
|
|
|
if singleton_class.method_defined? :attributes
|
|
|
|
attributes.include? attribute_name(mid)
|
|
|
|
else
|
|
|
|
(DEFAULT_ATTRIBUTES + ATTRIBUTES).include? attribute_name(mid)
|
|
|
|
end
|
2018-04-27 18:48:26 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Genera el post con metadatos en YAML
|
2019-08-07 15:10:14 +00:00
|
|
|
# rubocop:disable Metrics/AbcSize
|
2019-08-06 23:17:29 +00:00
|
|
|
def full_content
|
|
|
|
yaml = layout.metadata.keys.map(&:to_sym).map do |metadata|
|
|
|
|
template = send(metadata)
|
2018-04-27 18:48:26 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
{ metadata.to_s => template.value } unless template.empty?
|
|
|
|
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-06 23:17:29 +00:00
|
|
|
"#{yaml.to_yaml}---\n\n#{content}"
|
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
|
|
|
|
FileUtils.rm_f path
|
2018-02-26 18:58:56 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
site.posts(lang: lang).delete_if do |post|
|
|
|
|
post.path == path
|
|
|
|
end
|
2019-03-26 15:32:20 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
!File.exist?(path) && !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
|
2019-08-07 15:10:14 +00:00
|
|
|
# rubocop:enable Metrics/AbcSize
|
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-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-06 23:17:29 +00:00
|
|
|
document.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
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
# Devuelve la ruta del post, si se cambió alguno de los datos,
|
|
|
|
# generamos una ruta nueva para tener siempre la ruta actualizada.
|
2018-01-29 22:19:10 +00:00
|
|
|
def path
|
2019-08-06 23:17:29 +00:00
|
|
|
document.path
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
def basename_changed?
|
|
|
|
@post.try(:basename) != basename_from_front_matter
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
def slug_changed?
|
|
|
|
new? || @post.data.dig('slug') != slug
|
2018-01-30 15:20:19 +00:00
|
|
|
end
|
|
|
|
|
2018-06-21 18:15:03 +00:00
|
|
|
# devuelve las plantillas como strong params, primero los valores
|
|
|
|
# simples, luego los arrays y al final los hashes
|
2018-05-14 21:45:52 +00:00
|
|
|
def template_params
|
2019-08-06 23:17:29 +00:00
|
|
|
@template_params ||= template_fields.map(&:to_param).sort_by do |s|
|
2018-06-21 18:15:03 +00:00
|
|
|
s.is_a?(Symbol) ? 0 : 1
|
2018-05-11 20:00:45 +00:00
|
|
|
end
|
2018-05-14 19:27:29 +00:00
|
|
|
end
|
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
# Solo agregar el post al sitio una vez que lo guardamos
|
|
|
|
#
|
|
|
|
# TODO no sería la forma correcta de hacerlo en Rails
|
2019-08-07 15:10:14 +00:00
|
|
|
# def add_post_to_site!
|
|
|
|
# @site.jekyll.collections[@collection].docs << @post
|
|
|
|
# @site.jekyll.collections[@collection].docs.sort!
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# unless @site.collections[@collection].include? self
|
|
|
|
# @site.collections[@collection] << self
|
|
|
|
# @site.collections[@collection].sort!
|
|
|
|
# end
|
|
|
|
# end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
|
|
|
# Cambiar el nombre del archivo si cambió el título o la fecha.
|
|
|
|
# Como Jekyll no tiene métodos para modificar un Document, lo
|
|
|
|
# engañamos eliminando la instancia de @post y recargando otra.
|
|
|
|
def detect_file_rename!
|
|
|
|
return true unless basename_changed?
|
2018-02-03 22:37:09 +00:00
|
|
|
# No eliminamos el archivo a menos que ya exista el reemplazo!
|
|
|
|
return false unless File.exist? path
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
Rails.logger.info I18n.t('posts.logger.rm', path: path)
|
|
|
|
FileUtils.rm @post.path
|
2019-08-07 15:10:14 +00:00
|
|
|
# replace_post!
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
# Reemplaza el post en el sitio por uno nuevo
|
2019-08-07 15:10:14 +00:00
|
|
|
# TODO: refactorizar
|
|
|
|
# def replace_post!
|
|
|
|
# @old_post = @site.jekyll.collections[@lang].docs.delete @post
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# new_post
|
|
|
|
# end
|
2018-01-29 22:19:10 +00:00
|
|
|
|
2018-02-03 22:37:09 +00:00
|
|
|
def cleanup!
|
2018-07-23 19:48:51 +00:00
|
|
|
default_date_is_today!
|
2018-02-03 22:37:09 +00:00
|
|
|
clean_content!
|
|
|
|
slugify_title!
|
2018-02-19 20:17:52 +00:00
|
|
|
end
|
|
|
|
|
2018-01-29 22:19:10 +00:00
|
|
|
# Aplica limpiezas básicas del contenido
|
|
|
|
def clean_content!
|
2019-08-06 23:17:29 +00:00
|
|
|
content.try(:delete!, "\r")
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# 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-07 15:10:14 +00:00
|
|
|
Site::Writer.new(site: site, file: path,
|
|
|
|
content: full_content, usuarie: usuarie,
|
|
|
|
message: title.value).save
|
2018-01-29 22:19:10 +00:00
|
|
|
end
|
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
# Devuelve le autore de un artículo
|
|
|
|
# XXX: Si se cambia le autore en el editor también cambia quién hace
|
|
|
|
# un cambio y se le puede asignar a cualquier otre.
|
|
|
|
# TODO: Mover la escritura a un servicio que combine escritura, commit
|
|
|
|
# y current_usuarie
|
|
|
|
def usuarie
|
|
|
|
OpenStruct.new(email: author.value.first,
|
|
|
|
name: author.value.first.split('@', 2).first)
|
|
|
|
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?
|
|
|
|
File.exist?(path) && full_content == File.read(path)
|
2018-02-02 22:20:31 +00:00
|
|
|
end
|
2018-02-03 22:37:09 +00:00
|
|
|
|
2019-08-07 15:10:14 +00:00
|
|
|
private
|
|
|
|
|
2018-07-23 19:48:51 +00:00
|
|
|
def default_date_is_today!
|
2019-08-07 15:10:14 +00:00
|
|
|
self.date ||= Time.now
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def slugify_title!
|
2019-08-06 23:17:29 +00:00
|
|
|
self.slug = Jekyll::Utils.slugify(title) if slug.blank?
|
2018-02-03 22:37:09 +00:00
|
|
|
end
|
2018-05-02 18:38:46 +00:00
|
|
|
|
2019-08-06 23:17:29 +00:00
|
|
|
# Obtiene el nombre del atributo sin
|
|
|
|
def attribute_name(attr)
|
|
|
|
attr.to_s.split('=').first.to_sym
|
|
|
|
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
|