diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index aba3e4e..08cfa32 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -109,6 +109,19 @@ class PostsController < ApplicationController end end + # Eliminar artículos + def destroy + @site = find_site + @lang = find_lang(@site) + @post = find_post(@site) + + authorize @post + + @post.destroy + + redirect_to site_posts_path(@site, category: session[:category], lang: @lang) + end + private # Solo permitir cambiar estos atributos de cada articulo diff --git a/app/models/post.rb b/app/models/post.rb index a29471c..f4c732a 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 @@ -378,6 +378,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/app/policies/post_policy.rb b/app/policies/post_policy.rb index 9af48ea..579560d 100644 --- a/app/policies/post_policy.rb +++ b/app/policies/post_policy.rb @@ -34,6 +34,12 @@ class PostPolicy < SuttyPolicy usuaria? || post.author == usuarix.email end + # Solo las usuarias pueden eliminar artículos. Lxs invitadxs pueden + # borrar sus propios artículos + def destroy? + update? + end + class Scope < SuttyPolicy::Scope # Las usuarias pueden ver todos los posts # diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index aeef09b..606b392 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -118,8 +118,16 @@ %br %td= post.date.strftime('%F') - %td= link_to t('posts.edit'), - edit_site_post_path(@site, post, lang: @lang), - class: 'btn btn-info' + %td + - if policy(post).edit? + = link_to t('posts.edit'), + edit_site_post_path(@site, post, lang: @lang), + class: 'btn btn-info' + - if policy(post).destroy? + = link_to t('posts.destroy'), + site_post_path(@site, post, lang: @lang), + class: 'btn btn-danger', + method: :delete, + data: { confirm: t('posts.confirm_destroy') } - else %h2= t('posts.none') diff --git a/config/locales/en.yml b/config/locales/en.yml index de2d09a..e24511f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -255,3 +255,5 @@ en: invalid_help: It looks like the form is incomplete. Check the red-colored fields to complete it. sending_help: Saving, please wait... blank: Nothing + destroy: Delete + confirm_destroy: Are you sure? diff --git a/config/locales/es.yml b/config/locales/es.yml index 23f0442..5bef36f 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -262,3 +262,5 @@ es: invalid_help: Parece que el formulario no está completo. Verifica los campos marcados en rojo para completarlo. sending_help: Guardando, por favor espera... blank: En blanco + destroy: Borrar + confirm_destroy: ¿Estás segurx? diff --git a/test/models/post_test.rb b/test/models/post_test.rb index 7ac5ba0..de1a7ae 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