5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 22:16:07 +00:00
panel/app/models/post.rb

314 lines
8.4 KiB
Ruby
Raw Normal View History

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
# para modificarlos y crear nuevos.
#
# rubocop:disable Metrics/ClassLength
2019-08-07 15:10:14 +00:00
# rubocop:disable Style/MethodMissingSuper
# rubocop:disable Style/MissingRespondToMissing
class Post < OpenStruct
# Atributos por defecto
DEFAULT_ATTRIBUTES = %i[site document layout].freeze
# Otros atributos que no vienen en los metadatos
PRIVATE_ATTRIBUTES = %i[path slug attributes errors].freeze
PUBLIC_ATTRIBUTES = %i[lang date].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
#
def initialize(**args)
default_attributes_missing(args)
super(args)
# Genera un método con todos los atributos disponibles
self.attributes = layout.metadata.keys.map(&:to_sym) + PUBLIC_ATTRIBUTES
self.errors = {}
# 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,
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,
type: template['type'],
label: template['label'],
help: template['help'],
required: template['required'])
end
2019-08-07 21:35:37 +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!
2019-08-07 21:35:37 +00:00
# Leer el documento
read
end
def id
path.basename
end
# 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
raise ArgumentError, i18n unless args[attr].present?
end
2018-01-29 22:19:10 +00:00
end
# 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
2019-08-07 21:35:37 +00:00
# Definir los attribute_*
new_attribute_was(mid)
new_attribute_changed(mid)
# OpenStruct
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)
end
# Detecta si es un atributo válido o no, a partir de la tabla de la
# plantilla
def attribute?(mid)
attrs = DEFAULT_ATTRIBUTES + PRIVATE_ATTRIBUTES + PUBLIC_ATTRIBUTES
if singleton_class.method_defined? :attributes
(attrs + attributes).include? attribute_name(mid)
else
attrs.include? attribute_name(mid)
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
# Genera el post con metadatos en YAML
def full_content
2019-08-13 23:33:57 +00:00
body = ''
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 }
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
# Y que no se procese liquid
yaml['liquid'] = false
2019-08-07 15:10:14 +00:00
2019-08-13 23:33:57 +00:00
"#{yaml.to_yaml}---\n\n#{body}"
end
# 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
2019-08-16 23:12:22 +00:00
# TODO: Devolver self en lugar de todo el array
site.posts(lang: lang.value).reject! do |post|
2019-08-08 18:28:23 +00:00
post.path.absolute == path.absolute
end
2018-02-22 19:01:11 +00:00
end
alias destroy! destroy
2018-02-22 19:01:11 +00:00
2019-08-07 15:10:14 +00:00
# Guarda los cambios
2019-08-22 01:09:29 +00:00
# rubocop:disable Metrics/CyclomaticComplexity
2018-01-29 22:19:10 +00:00
def save
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-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
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
# Actualiza la ruta del documento y lo lee
2019-08-07 21:35:37 +00:00
def read
return unless written?
document.path = path.absolute
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
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
# Detecta si el artículo es válido para guardar
def valid?
validate
errors.blank?
end
# Requisitos para que el post sea válido
def validate
self.errors = {}
2018-02-02 22:20:31 +00:00
layout.metadata.keys.map(&:to_sym).each do |metadata|
template = send(metadata)
2018-07-23 19:48:34 +00:00
errors[metadata] = template.errors unless template.valid?
end
end
alias validate! validate
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,
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
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
alias update update_attributes
2019-08-13 23:33:57 +00:00
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
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
end
2019-08-07 21:35:37 +00:00
# Obtiene el nombre del atributo a partir del nombre del método
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)
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
def load_lang!
self.lang = MetadataLang.new(document: document, site: site,
layout: layout, name: :lang,
type: :lang, 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
# rubocop:enable Metrics/ClassLength
2019-08-07 15:10:14 +00:00
# rubocop:enable Style/MethodMissingSuper
# rubocop:enable Style/MissingRespondToMissing