From 2bbe3380655d32ef1de414da3f16b322ebf0e90c Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 31 Aug 2019 12:27:56 -0300 Subject: [PATCH] No se pueden eliminar barcas si tienen consensos --- app/controllers/barcas_controller.rb | 4 ++-- app/views/barcas/_barca.json.jbuilder | 1 + test/controllers/barcas_controller_test.rb | 10 ++++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/controllers/barcas_controller.rb b/app/controllers/barcas_controller.rb index a52e839..4471e8b 100644 --- a/app/controllers/barcas_controller.rb +++ b/app/controllers/barcas_controller.rb @@ -78,14 +78,14 @@ class BarcasController < ApplicationController # DELETE /barcas/:id # - # Elimina una barca y su tripulaciĆ³n + # Elimina una barca y su tripulaciĆ³n, solo si no tiene consensos # # @return hash { id: @int, created_at: @date, updated_at: @date, # nombre: @string, descripcion: @text, abordada: @bool } def destroy @barca = current_pirata.barcas.find(params[:id]) - if @barca.destroy + if @barca.consensos.count.zero? && @barca.destroy notify(subject: :destroy, urgency: :high) render status: :no_content else diff --git a/app/views/barcas/_barca.json.jbuilder b/app/views/barcas/_barca.json.jbuilder index 8aa5feb..c596bd3 100644 --- a/app/views/barcas/_barca.json.jbuilder +++ b/app/views/barcas/_barca.json.jbuilder @@ -3,3 +3,4 @@ json.call(barca, :id, :created_at, :nombre, :descripcion) json.abordada @current_pirata.barcas.pluck(:id).include?(barca.id) +json.eliminable barca.consensos.count.zero? diff --git a/test/controllers/barcas_controller_test.rb b/test/controllers/barcas_controller_test.rb index f03dac5..a4524e4 100644 --- a/test/controllers/barcas_controller_test.rb +++ b/test/controllers/barcas_controller_test.rb @@ -75,6 +75,16 @@ class BarcasControllerTest < ActionDispatch::IntegrationTest end end + test 'no se pueden eliminar si ya tienen consensos' do + barca = create :barca, piratas: [@pirata] + barca.consensos << create(:consenso) + + delete barca_url(barca), as: :json, headers: @auth + + assert_equal 422, @response.status + assert barca.reload + end + test 'se pueden abandonar' do barca = create :barca, piratas: [@pirata]