From d4a7a2466ad9fac2a5b5cbdfee322e106d02fb45 Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 20 Apr 2019 17:53:23 -0300 Subject: [PATCH] =?UTF-8?q?eliminar=20consensos=20si=20est=C3=A1n=20vac?= =?UTF-8?q?=C3=ADos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/consensos_controller.rb | 23 +++++++++++++++++++ app/views/consensos/destroy.json.jbuilder | 3 +++ config/routes.rb | 2 +- test/controllers/consensos_controller_test.rb | 20 ++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 app/views/consensos/destroy.json.jbuilder diff --git a/app/controllers/consensos_controller.rb b/app/controllers/consensos_controller.rb index e3a64cd..6da8f44 100644 --- a/app/controllers/consensos_controller.rb +++ b/app/controllers/consensos_controller.rb @@ -50,4 +50,27 @@ class ConsensosController < ApplicationController status: :unprocessable_entity end end + + # DELETE /consensos/:id + # + # Eliminar un consenso, solo si no tiene posiciones! + # + # @param :id [Integer] El ID del consenso a eliminar + # @return [Hash] { id: @int, created_at: @date, + # titulo: @string, texto: @string, + # posiciones: [] } + def destroy + begin + @consenso = Consenso.find(params[:id]) + rescue ActiveRecord::RecordNotFound + render json: {}, status: :not_found + return + end + + if @consenso.posiciones.empty? && @consenso.destroy + render status: :ok + else + render json: {}, status: :unprocessable_entity + end + end end diff --git a/app/views/consensos/destroy.json.jbuilder b/app/views/consensos/destroy.json.jbuilder new file mode 100644 index 0000000..f1e4c6b --- /dev/null +++ b/app/views/consensos/destroy.json.jbuilder @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +json.partial! @consenso, as: :consenso diff --git a/config/routes.rb b/config/routes.rb index 1e4c5dd..eb184a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Rails.application.routes.draw do # No queremos un índice de piratas resources :piratas, only: %i[create] # Podemos crear consensos pero no modificarlos - resources :consensos, only: %i[index show create] do + resources :consensos, only: %i[index show create destroy] do # Y solo le podemos agregar posiciones resources :posiciones, only: %i[create] end diff --git a/test/controllers/consensos_controller_test.rb b/test/controllers/consensos_controller_test.rb index 88d9cf1..3abc531 100644 --- a/test/controllers/consensos_controller_test.rb +++ b/test/controllers/consensos_controller_test.rb @@ -43,4 +43,24 @@ class ConsensosControllerTest < ActionDispatch::IntegrationTest assert_equal 'hola', consenso.titulo assert_equal 'chau', consenso.texto end + + test 'se pueden borrar si están vacíos' do + consenso = create :consenso + + delete consenso_url(consenso), as: :json, headers: @auth + + assert_equal 200, @response.status + + body = JSON.parse(@response.body) + assert_raise(ActiveRecord::RecordNotFound) { Consenso.find(body['id']) } + end + + test 'no se pueden borrar si no están vacíos' do + consenso = create :consenso + create :posicion, consenso: consenso + + delete consenso_url(consenso), as: :json, headers: @auth + + assert_equal 422, @response.status + end end