5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-26 08:26:07 +00:00

WIP: crear nuevos posts

This commit is contained in:
f 2019-08-08 16:26:47 -03:00
parent f7527e7ee3
commit 6aab932269
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
5 changed files with 51 additions and 6 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
# Este campo representa el archivo donde se almacenan los datos
class MetadataPath < MetadataTemplate
# :label en este caso es el idioma/colección
@ -18,7 +20,7 @@ class MetadataPath < MetadataTemplate
private
def ext
document.extname || '.markdown'
document.extname.blank? ? '.markdown' : document.extname
end
def lang

View file

@ -39,6 +39,8 @@ class MetadataSlug < MetadataTemplate
private
def title
layout.metadata[:title].try(:value)
return if post.title.try(:value).blank?
post.title.try(:value).blank?
end
end

View file

@ -142,7 +142,7 @@ class Post < OpenStruct
def save
return false unless valid?
# Salir si tenemos que cambiar el nombre del archivo y no pudimos
return false if path_changed? && !update_path!
return false if !new? && path_changed? && !update_path!
return false unless write
# Vuelve a leer el post para tomar los cambios
@ -152,9 +152,14 @@ class Post < OpenStruct
end
alias save! save
# Lee el documento
# Lee el documento a menos que estemos trabajando con un documento en
# blanco
def read
document.read
document.read unless new?
end
def new?
document.path.blank?
end
# Actualizar la ubicación del archivo si cambió de lugar y si no

View file

@ -15,11 +15,39 @@ class PostRelation < Array
# Genera un artículo nuevo con los parámetros que le pasemos y lo suma
# al array
def build(**args)
self << Post.new(site: site, **args)
args[:document] ||= build_document
args[:lang] ||= I18n.locale
args[:layout] = build_layout(args[:layout])
post = Post.new(site: site, **args)
self << post
post
end
# Intenta guardar todos y devuelve true si pudo
def save_all
map(&:save).all?
end
private
def build_layout(layout = :post)
return layout if layout.is_a? Layout
site.layouts[symbol]
end
# Devuelve una colección Jekyll que hace pasar el documento
def build_collection
Jekyll::Collection.new(site.jekyll, 'posts')
end
# Un documento borrador con algunas propiedades por defecto
def build_document
doc = Jekyll::Document.new('', site: site.jekyll,
collection: build_collection)
doc.data['date'] = Date.today.to_time
doc
end
end

View file

@ -161,4 +161,12 @@ class PostTest < ActiveSupport::TestCase
assert File.exist?(@post.path.absolute)
assert File.exist?(@post.path_was)
end
test 'se pueden crear nuevos' do
post = @site.posts.build(layout: :post)
post.title.value = 'test'
assert post.new?
assert post.save
end
end