From 6287373dae0e42889af85a45e366d8af0bc0fb0b Mon Sep 17 00:00:00 2001 From: Torsten Date: Fri, 2 Dec 2022 22:48:55 +0200 Subject: [PATCH] removing cards --- app/controllers/merged/cards_controller.rb | 7 +++++++ app/models/merged/section.rb | 9 +++++++++ app/views/merged/cards/index.haml | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 33bf31f..7ee3c12 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -26,6 +26,13 @@ module Merged redirect_to section_cards_url(@card.section.id) end + def remove + section = @card.section + section.remove_card( @card ) + section.save + redirect_to section_cards_url(section.id) + end + def update @card.content.each do |key , value| next if key == "id" diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index d306f26..fdc4920 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -67,6 +67,15 @@ module Merged ! cards.empty? end + def remove_card(card) + from_index = card.index + @cards.delete_at(from_index) + @content["cards"].delete_at(from_index) + @cards.each_with_index do |card, index| + card.set_index(index) + end + end + def move_up @page.move_section_up(self) end diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index c761291..01c90c1 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -21,7 +21,7 @@ = blue_button( "Up" , card_move_url(card.id , dir: :up) ) = blue_button( "Down" , card_move_url(card.id , dir: :down) ) = green_button( "New" , "/index" ) - = red_button("Delete" , "/index") + = red_button( "Delete" , card_remove_path(card.id) ) .p-4 %h3.mt-4.text-lg.font-bold Image = yellow_button("Change Image" , card_select_image_url(card.id) )