diff --git a/app/models/post.rb b/app/models/post.rb index 461733f9..d5f77092 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -230,12 +230,14 @@ class Post template = public_send attr unless template.front_matter? - body += "\n\n" + body += "\n\n" if body.present? body += template.value next end - next if template.empty? + # Queremos mantener los Array en el resultado final para que + # siempre respondan a {% for %} en Liquid. + next if template.empty? && !template.value.is_a?(Array) [attr.to_s, template.value] end.compact.to_h @@ -285,6 +287,7 @@ class Post return false unless write # Vuelve a leer el post para tomar los cambios + document.reset read written? diff --git a/config/initializers/core_extensions.rb b/config/initializers/core_extensions.rb index 8bbcbe71..d43a5116 100644 --- a/config/initializers/core_extensions.rb +++ b/config/initializers/core_extensions.rb @@ -52,7 +52,7 @@ module Jekyll # Solo lee los datos def read_data - @site.data = DataReader.new(site).read(site.config["data_dir"]) + @site.data = DataReader.new(site).read(site.config['data_dir']) end # Lee todos los artículos del sitio @@ -73,6 +73,15 @@ module Jekyll Document.class_eval do alias_method :read!, :read def read; end + + # Permitir restablecer el documento sin crear uno nuevo + def reset + @path = @extname = @has_yaml_header = @relative_path = nil + @basename_without_ext = @data = @basename = nil + @renderer = @url_placeholders = @url = nil + @to_liquid = @write_p = @excerpt_separator = @id = nil + @related_posts = @cleaned_relative_path = self.content = nil + end end # Prevenir la instanciación de Time diff --git a/test/models/metadata_belongs_to_test.rb b/test/models/metadata_belongs_to_test.rb index bf3f7b67..09b9714f 100644 --- a/test/models/metadata_belongs_to_test.rb +++ b/test/models/metadata_belongs_to_test.rb @@ -12,6 +12,11 @@ class MetadataBelongsToTest < ActiveSupport::TestCase assert_equal post, reply.in_reply_to.belongs_to assert_includes post.posts.has_many, reply + + assert post.save + + assert_equal reply.document.data['in_reply_to'], post.document.data['uuid'] + assert_includes post.document.data['posts'], reply.document.data['uuid'] end test 'se puede eliminar la relación' do @@ -19,12 +24,17 @@ class MetadataBelongsToTest < ActiveSupport::TestCase reply = @site.posts.create(layout: :post, title: SecureRandom.hex, in_reply_to: post.uuid.value) reply.in_reply_to.value = '' - reply.save + assert reply.save assert_not_equal post, reply.in_reply_to.belongs_to assert_equal post, reply.in_reply_to.belonged_to assert_nil reply.in_reply_to.belongs_to assert_not_includes post.posts.has_many, reply + + assert post.save + + assert_nil reply.document.data['in_reply_to'] + assert_not_includes post.document.data['posts'], reply.document.data['uuid'] end test 'se puede cambiar la relación' do @@ -33,7 +43,7 @@ class MetadataBelongsToTest < ActiveSupport::TestCase reply = @site.posts.create(layout: :post, title: SecureRandom.hex, in_reply_to: post1.uuid.value) reply.in_reply_to.value = post2.uuid.value - reply.save + assert reply.save assert_not_equal post1, reply.in_reply_to.belongs_to assert_equal post1, reply.in_reply_to.belonged_to @@ -41,5 +51,12 @@ class MetadataBelongsToTest < ActiveSupport::TestCase assert_equal post2, reply.in_reply_to.belongs_to assert_includes post2.posts.has_many, reply + + assert post1.save + assert post2.save + + assert_equal post2.document.data['uuid'], reply.document.data['in_reply_to'] + assert_includes post2.document.data['posts'], reply.document.data['uuid'] + assert_not_includes post1.document.data['posts'], reply.document.data['uuid'] end end diff --git a/test/models/metadata_has_and_belongs_to_many_test.rb b/test/models/metadata_has_and_belongs_to_many_test.rb index 1404e145..4887a96e 100644 --- a/test/models/metadata_has_and_belongs_to_many_test.rb +++ b/test/models/metadata_has_and_belongs_to_many_test.rb @@ -15,6 +15,11 @@ class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase assert_includes author.posts.has_many, post assert_includes post.authors.has_many, author + + assert author.save + + assert_includes author.document.data['posts'], post.document.data['uuid'] + assert_includes post.document.data['authors'], author.document.data['uuid'] end test 'se puede eliminar la relación' do @@ -25,7 +30,6 @@ class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase assert_includes author.posts.value, post.uuid.value post.authors.value = [] - ENV['HOLA'] = 'hola' assert post.save assert_not_includes author.posts.has_many, post @@ -33,6 +37,11 @@ class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase assert_includes author.posts.had_many, post assert_includes post.authors.had_many, author + + assert author.save + + assert_not_includes author.document.data['posts'], post.document.data['uuid'] + assert_not_includes post.document.data['authors'], author.document.data['uuid'] end test 'se puede cambiar la relación' do @@ -54,5 +63,14 @@ class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase assert_includes author.posts.has_many, post2 assert_includes post2.authors.has_many, author + + assert post1.save + assert post2.save + + assert_not_includes author.document.data['posts'], post1.document.data['uuid'] + assert_not_includes post1.document.data['authors'], author.document.data['uuid'] + + assert_includes author.document.data['posts'], post2.document.data['uuid'] + assert_includes post2.document.data['authors'], author.document.data['uuid'] end end diff --git a/test/models/metadata_has_many_test.rb b/test/models/metadata_has_many_test.rb index 637c9425..38b4d46a 100644 --- a/test/models/metadata_has_many_test.rb +++ b/test/models/metadata_has_many_test.rb @@ -12,6 +12,11 @@ class MetadataHasManyTest < ActiveSupport::TestCase assert_equal post, reply.in_reply_to.belongs_to assert_includes post.posts.has_many, reply + + assert reply.save + + assert_equal reply.document.data['in_reply_to'], post.document.data['uuid'] + assert_includes post.document.data['posts'], reply.document.data['uuid'] end test 'se puede eliminar la relación' do @@ -19,12 +24,17 @@ class MetadataHasManyTest < ActiveSupport::TestCase post = @site.posts.create(layout: :post, title: SecureRandom.hex, posts: [reply.uuid.value]) post.posts.value = [] - post.save + assert post.save assert_not_equal post, reply.in_reply_to.belongs_to assert_equal post, reply.in_reply_to.belonged_to assert_nil reply.in_reply_to.belongs_to assert_not_includes post.posts.has_many, reply + + assert reply.save + + assert_nil reply.document.data['in_reply_to'] + assert_not_includes post.document.data['posts'], reply.document.data['uuid'] end test 'se puede cambiar la relación' do @@ -33,7 +43,7 @@ class MetadataHasManyTest < ActiveSupport::TestCase post2 = @site.posts.create(layout: :post, title: SecureRandom.hex) reply.in_reply_to.value = post2.uuid.value - reply.save + assert reply.save assert_not_equal post1, reply.in_reply_to.belongs_to assert_equal post1, reply.in_reply_to.belonged_to @@ -41,5 +51,12 @@ class MetadataHasManyTest < ActiveSupport::TestCase assert_equal post2, reply.in_reply_to.belongs_to assert_includes post2.posts.has_many, reply + + assert post1.save + assert post2.save + + assert_equal post2.document.data['uuid'], reply.document.data['in_reply_to'] + assert_includes post2.document.data['posts'], reply.document.data['uuid'] + assert_not_includes post1.document.data['posts'], reply.document.data['uuid'] end end