From e9a76250108817daa4afa387966236b77200ec95 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 30 May 2019 12:52:12 -0300 Subject: [PATCH 1/2] implementar Post.destroy --- .rubocop_todo.yml | 2 ++ app/models/post.rb | 14 +++++++++++++- test/models/post_test.rb | 13 +++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9c62a5d..f6ddd68 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 6971f8c..84588d6 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 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 From bae77d8c6960ba702f4ea4d2dc0a39ba3cec3335 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 30 May 2019 14:33:51 -0300 Subject: [PATCH 2/2] eliminar desde el panel --- app/controllers/posts_controller.rb | 13 +++++++++++++ app/policies/post_policy.rb | 6 ++++++ app/views/posts/index.haml | 14 +++++++++++--- config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 2b9ad02..ffb268b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -104,6 +104,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/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 ec393c5..8a36e47 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -254,3 +254,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 2b93429..d985d39 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -261,3 +261,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?