diff --git a/app/models/metadata_path.rb b/app/models/metadata_path.rb index ec0467e5..c3c0e153 100644 --- a/app/models/metadata_path.rb +++ b/app/models/metadata_path.rb @@ -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 diff --git a/app/models/metadata_slug.rb b/app/models/metadata_slug.rb index 19206fc2..75dbadf5 100644 --- a/app/models/metadata_slug.rb +++ b/app/models/metadata_slug.rb @@ -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 diff --git a/app/models/post.rb b/app/models/post.rb index af88d370..23e5dffc 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index 67cc9237..e19976c8 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -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 diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 10429e15..c33e75be 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -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