diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9c62a5d8..f6ddd687 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -68,6 +68,8 @@ Metrics/BlockLength: # Configuration parameters: CountComments. Metrics/ClassLength: Max: 379 + Exclude: + - 'app/models/post.rb' # Offense count: 6 Metrics/CyclomaticComplexity: diff --git a/app/models/post.rb b/app/models/post.rb index 6971f8c4..84588d67 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -17,7 +17,7 @@ require 'jekyll/utils' class Post attr_accessor :content, :front_matter attr_reader :post, :site, :errors, :old_post, :lang, :template, - :template_fields + :template_fields, :collection REJECT_FROM_DATA = %w[excerpt].freeze # datos que no tienen que terminar en el front matter @@ -369,6 +369,18 @@ class Post @template ||= template_from_layout end + # Eliminar el artículo del repositorio y de la lista de artículos del + # sitio + def destroy + FileUtils.rm_f path + + site.posts_for(collection).delete_if do |post| + post.path == path + end + + true + end + private # Completa el front_matter a partir de las variables de otro post que diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 7ac5ba09..de1a7aeb 100644 --- a/test/models/post_test.rb +++ b/test/models/post_test.rb @@ -28,4 +28,17 @@ class PostTest < ActiveSupport::TestCase test 'Es obvio que un post recién cargado es válido' do assert @post.valid? end + + test 'El post se puede borrar' do + path = @post.path + + assert @post.destroy + assert_not File.exist?(path) + + post = @site.posts_for(@post.collection).find do |p| + p.path == @post.path + end + + assert_not post + end end