From a4af2d68725dcb881add33f86d331b333ccc3cf5 Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 1 Dec 2022 19:25:22 +0200 Subject: [PATCH] moving cards up and down --- app/controllers/merged/cards_controller.rb | 10 +++++++ app/models/merged/card.rb | 11 +++++++ app/models/merged/section.rb | 34 ++++++++++++++++++++++ app/views/merged/cards/index.haml | 4 +-- config/routes.rb | 1 + 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 6f4f814..33bf31f 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -16,6 +16,16 @@ module Merged redirect_to section_cards_url(@card.section.id) end + def move + if( params[:dir] == "up") + @card.move_up + else + @card.move_down + end + @card.save + redirect_to section_cards_url(@card.section.id) + end + def update @card.content.each do |key , value| next if key == "id" diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index c6d525a..05d6954 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -35,6 +35,13 @@ module Merged @content[key] = value end + def move_up + @section.move_card_up(self) + end + def move_down + @section.move_card_down(self) + end + def save section.save end @@ -45,5 +52,9 @@ module Merged raise "Section not found #{id}" unless card return card end + + def set_index(index) + @index = index + end end end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 44c1ce4..cc3cb89 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -35,6 +35,40 @@ module Merged ! cards.empty? end + def move_up + @page.move_section_up(self) + end + def move_down + @section.move_section_down(self) + end + + def move_card_up(card) + return if cards.length == 1 + return if card.index == 0 + swap( card , cards[card.index - 1]) + end + + def move_card_down(card) + return if cards.length == 1 + return if card.index == cards.last.index + swap( card , cards[card.index + 1]) + end + + def swap( this_card , that_card) + # swap in the actual objects, index is cached in the objects + this_old_index = this_card.index + this_card.set_index( that_card.index ) + that_card.set_index( this_old_index ) + + # swap in the cards cache + cards[ this_card.index ] = this_card + cards[ that_card.index ] = that_card + # swap in the yaml + card_content = content["cards"] + card_content[this_card.index] = this_card.content + card_content[that_card.index] = that_card.content + end + def update(key , value) return if key == "id" #not updating that if(! @content[key].nil? ) diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index dabd35f..1b10f0d 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -15,9 +15,9 @@ .p-4 %h3.mt-4.text-lg.font-bold Card #{index + 1} %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to( "Up" , "/index") + =link_to( "Up" , card_move_url(card.id , dir: :up) ) %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to "Down" , "/index" + =link_to( "Down" , card_move_url(card.id , dir: :down) ) %button.mt-4.rounded-lg.bg-cyan-400.p-4 =link_to "New" , "/index" %button.mt-4.rounded-lg.bg-red-400.p-4 diff --git a/config/routes.rb b/config/routes.rb index 89ce911..4aea482 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ Merged::Engine.routes.draw do resources :cards do get :select_image get :set_image + get :move end end end