5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-06-29 03:16:08 +00:00

escritura de posts mas robusta

no se estaba actualizando la ruta del documento interno de jekyll al
cambiar la ruta, lo que llevó a una refactorización de tests e
implementación de PostRelation.create :)
This commit is contained in:
f 2019-09-16 14:56:34 -03:00
parent 2169291c29
commit 8f22215621
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
3 changed files with 37 additions and 31 deletions

View file

@ -160,21 +160,27 @@ class Post < OpenStruct
# Vuelve a leer el post para tomar los cambios # Vuelve a leer el post para tomar los cambios
read read
true written?
end end
# rubocop:enable Metrics/CyclomaticComplexity # rubocop:enable Metrics/CyclomaticComplexity
alias save! save alias save! save
# Lee el documento a menos que estemos trabajando con un documento en # Actualiza la ruta del documento y lo lee
# blanco
def read def read
document.read unless new? return unless written?
document.path = path.absolute
document.read
end end
def new? def new?
document.path.blank? document.path.blank?
end end
def written?
File.exist? path.absolute
end
# Actualizar la ubicación del archivo si cambió de lugar y si no # Actualizar la ubicación del archivo si cambió de lugar y si no
# existe el destino # existe el destino
def update_path! def update_path!

View file

@ -25,6 +25,12 @@ class PostRelation < Array
post post
end end
def create(**args)
post = build(args)
post.save
post
end
def find(id) def find(id)
super() do |p| super() do |p|
p.id == id p.id == id

View file

@ -4,15 +4,12 @@ require 'test_helper'
class PostTest < ActiveSupport::TestCase class PostTest < ActiveSupport::TestCase
setup do setup do
# Trabajamos con el sitio de sutty porque tiene artículos @site = create :site
# @post = @site.posts.create(title: SecureRandom.hex)
# TODO: Cambiar a skel cuando publiquemos los códigos y privacidad
@site = create :site, name: 'sutty.nl'
@post = @site.posts.sample
end end
teardown do teardown do
# @site.destroy @site.destroy
end end
test 'se puede acceder a los valores' do test 'se puede acceder a los valores' do
@ -28,20 +25,13 @@ class PostTest < ActiveSupport::TestCase
end end
test 'se pueden eliminar' do test 'se pueden eliminar' do
# TODO: cuando esté con git, solo aplicar git reset
tmp = File.join(Rails.root, 'tmp', 'eliminar.md')
FileUtils.cp @post.path.absolute, tmp
assert @post.destroy assert @post.destroy
assert_not File.exist?(@post.path.absolute) assert_not File.exist?(@post.path.absolute)
assert_not @site.posts.include?(@post) assert_not @site.posts.include?(@post)
FileUtils.mv tmp, @post.path.absolute
end end
test 'se puede ver el contenido completo después de guardar' do test 'se puede ver el contenido completo después de guardar' do
assert @post.save assert @post.save
@post.read
# Queremos saber si todos los atributos del post terminaron en el # Queremos saber si todos los atributos del post terminaron en el
# archivo # archivo
@ -98,6 +88,7 @@ class PostTest < ActiveSupport::TestCase
end end
test 'se puede cambiar el slug' do test 'se puede cambiar el slug' do
@post.title.value = SecureRandom.hex
assert_equal @post.slug_was, @post.slug.value assert_equal @post.slug_was, @post.slug.value
assert_not @post.slug_changed? assert_not @post.slug_changed?
assert @post.slug.valid? assert @post.slug.valid?
@ -128,27 +119,29 @@ class PostTest < ActiveSupport::TestCase
test 'al cambiar slug o fecha cambia el archivo de ubicacion' do test 'al cambiar slug o fecha cambia el archivo de ubicacion' do
hoy = 2.days.from_now.to_time hoy = 2.days.from_now.to_time
path_was = @post.path.absolute path_was = @post.path.absolute
@post.slug.value = 'test' @post.slug.value = title = SecureRandom.hex
@post.date.value = hoy @post.date.value = hoy
assert @post.path_changed? assert @post.path_changed?
assert_equal "_es/#{hoy.strftime('%F')}-test.markdown", assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown",
@post.path.relative @post.path.relative
assert @post.save assert @post.save
assert_equal "_es/#{hoy.strftime('%F')}-test.markdown", assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown",
@post.document.relative_path @post.document.relative_path
assert_not File.exist?(path_was) assert_not File.exist?(path_was)
assert File.exist?(@post.path.absolute) assert File.exist?(@post.path.absolute)
end end
test 'new?' do
post = @site.posts.build title: SecureRandom.hex
assert post.new?
assert post.save
assert_not post.new?
end
test 'no podemos pisar otros archivos' do test 'no podemos pisar otros archivos' do
# Elegir un post al azar que no sea el que usamos post = @site.posts.create title: SecureRandom.hex
# rubocop:disable Lint/Loop
begin
post = @site.posts.sample
end until post != @post
# rubocop:enable Lint/Loop
@post.slug.value = post.slug.value @post.slug.value = post.slug.value
@post.date.value = post.date.value @post.date.value = post.date.value
@ -160,8 +153,8 @@ class PostTest < ActiveSupport::TestCase
test 'se pueden crear nuevos' do test 'se pueden crear nuevos' do
post = @site.posts.build(layout: :post) post = @site.posts.build(layout: :post)
post.title.value = 'test' post.title.value = SecureRandom.hex
post.content.value = 'test' post.content.value = SecureRandom.hex
assert post.new? assert post.new?
assert post.save assert post.save
@ -169,10 +162,11 @@ class PostTest < ActiveSupport::TestCase
end end
test 'se pueden inicializar con valores' do test 'se pueden inicializar con valores' do
post = @site.posts.build(title: 'test', content: 'test') title = SecureRandom.hex
post = @site.posts.build(title: title, content: title)
assert_equal 'test', post.title.value assert_equal title, post.title.value
assert_equal 'test', post.content.value assert_equal title, post.content.value
assert post.save assert post.save
end end
end end