diff --git a/app/models/post.rb b/app/models/post.rb index 4a4cff06..ace24875 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -160,21 +160,27 @@ class Post < OpenStruct # Vuelve a leer el post para tomar los cambios read - true + written? end # rubocop:enable Metrics/CyclomaticComplexity alias save! save - # Lee el documento a menos que estemos trabajando con un documento en - # blanco + # Actualiza la ruta del documento y lo lee def read - document.read unless new? + return unless written? + + document.path = path.absolute + document.read end def new? document.path.blank? end + def written? + File.exist? path.absolute + end + # Actualizar la ubicación del archivo si cambió de lugar y si no # existe el destino def update_path! diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index dec79d39..4fa206f4 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -25,6 +25,12 @@ class PostRelation < Array post end + def create(**args) + post = build(args) + post.save + post + end + def find(id) super() do |p| p.id == id diff --git a/test/models/post_test.rb b/test/models/post_test.rb index ed15f0a2..b52f5d9c 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -4,15 +4,12 @@ require 'test_helper' class PostTest < ActiveSupport::TestCase setup do - # Trabajamos con el sitio de sutty porque tiene artículos - # - # TODO: Cambiar a skel cuando publiquemos los códigos y privacidad - @site = create :site, name: 'sutty.nl' - @post = @site.posts.sample + @site = create :site + @post = @site.posts.create(title: SecureRandom.hex) end teardown do - # @site.destroy + @site.destroy end test 'se puede acceder a los valores' do @@ -28,20 +25,13 @@ class PostTest < ActiveSupport::TestCase end 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_not File.exist?(@post.path.absolute) assert_not @site.posts.include?(@post) - - FileUtils.mv tmp, @post.path.absolute end test 'se puede ver el contenido completo después de guardar' do assert @post.save - @post.read # Queremos saber si todos los atributos del post terminaron en el # archivo @@ -98,6 +88,7 @@ class PostTest < ActiveSupport::TestCase end test 'se puede cambiar el slug' do + @post.title.value = SecureRandom.hex assert_equal @post.slug_was, @post.slug.value assert_not @post.slug_changed? assert @post.slug.valid? @@ -128,27 +119,29 @@ class PostTest < ActiveSupport::TestCase test 'al cambiar slug o fecha cambia el archivo de ubicacion' do hoy = 2.days.from_now.to_time path_was = @post.path.absolute - @post.slug.value = 'test' + @post.slug.value = title = SecureRandom.hex @post.date.value = hoy assert @post.path_changed? - assert_equal "_es/#{hoy.strftime('%F')}-test.markdown", + assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown", @post.path.relative assert @post.save - assert_equal "_es/#{hoy.strftime('%F')}-test.markdown", + assert_equal "_es/#{hoy.strftime('%F')}-#{title}.markdown", @post.document.relative_path assert_not File.exist?(path_was) assert File.exist?(@post.path.absolute) 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 - # Elegir un post al azar que no sea el que usamos - # rubocop:disable Lint/Loop - begin - post = @site.posts.sample - end until post != @post - # rubocop:enable Lint/Loop + post = @site.posts.create title: SecureRandom.hex @post.slug.value = post.slug.value @post.date.value = post.date.value @@ -160,8 +153,8 @@ class PostTest < ActiveSupport::TestCase test 'se pueden crear nuevos' do post = @site.posts.build(layout: :post) - post.title.value = 'test' - post.content.value = 'test' + post.title.value = SecureRandom.hex + post.content.value = SecureRandom.hex assert post.new? assert post.save @@ -169,10 +162,11 @@ class PostTest < ActiveSupport::TestCase end 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 'test', post.content.value + assert_equal title, post.title.value + assert_equal title, post.content.value assert post.save end end