From 4f6538e13588df14ea335b55ffa0921a6c5d53db Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 1 Dec 2022 20:14:34 +0200 Subject: [PATCH] moving sections up and down --- app/controllers/merged/sections_controller.rb | 10 +++++++ app/models/merged/page.rb | 30 +++++++++++++++---- app/models/merged/section.rb | 6 +++- app/views/merged/cards/index.haml | 7 +++-- app/views/merged/pages/show.haml | 24 --------------- app/views/merged/sections/index.html.haml | 7 +++-- app/views/merged/sections/show.html.haml | 4 +-- config/routes.rb | 3 +- 8 files changed, 51 insertions(+), 40 deletions(-) delete mode 100644 app/views/merged/pages/show.haml diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 381d676..ef678a7 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -38,6 +38,16 @@ module Merged redirect_to section_url(@section.id) end + def move + if( params[:dir] == "up") + @section.move_up + else + @section.move_down + end + @section.save + redirect_to page_sections_url(@section.page.name) + end + def update @section.content.each do |key , value| next if key == "id" diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index e013148..0e86127 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -38,11 +38,30 @@ module Merged @content[0]["template"] end - def new_section - section = Hash.new - section['id'] = SecureRandom.hex(10) - @content << section - Section.new(self , 0 , section) + def move_section_up(section) + return if sections.length == 1 + return if section.index == 0 + swap( section , sections[section.index - 1]) + end + + def move_section_down(section) + return if sections.length == 1 + return if section.index == sections.last.index + swap( section , sections[section.index + 1]) + end + + def swap( this_section , that_section) + # swap in the actual objects, index is cached in the objects + this_old_index = this_section.index + this_section.set_index( that_section.index ) + that_section.set_index( this_old_index ) + + # swap in the sections cache + sections[ this_section.index ] = this_section + sections[ that_section.index ] = that_section + # swap in the yaml + content[this_section.index] = this_section.content + content[that_section.index] = that_section.content end def save @@ -55,7 +74,6 @@ module Merged page = @@all[name] raise "Page not found #{name}" unless page return page - end end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index cc3cb89..aef1b89 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -39,7 +39,7 @@ module Merged @page.move_section_up(self) end def move_down - @section.move_section_down(self) + @page.move_section_down(self) end def move_card_up(card) @@ -83,6 +83,10 @@ module Merged page.save end + def set_index(index) + @index = index + end + def self.find(section_id) raise "nil given" if section_id.blank? section = @@all[section_id] diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index 1b10f0d..dec5dca 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -3,11 +3,12 @@ .flex.flex-col.bg-white .flex.items-center.justify-center.flex-1 .max-w-xl.px-4.py-8.mx-auto.text-center - %h1.text-2xl.font-bold.tracking-tight.text-gray-900 - Page #{@section.page.name} + %h1.text-3xl.font-bold.tracking-tight.text-gray-900 + Page #{link_to @section.page.name, page_sections_url(@section.page.name)} .flex.items-center.justify-center.flex-1 %h3.text-xl.font-bold.tracking-tight.text-gray-900 - Cards for Section #{@section.index + 1} + Cards for + = link_to "Section #{@section.index + 1}", page_sections_url(page_id: @section.page.name) .grid.grid-cols-4.gap-2.m-8 - @section.cards.each_with_index do |card , index| diff --git a/app/views/merged/pages/show.haml b/app/views/merged/pages/show.haml deleted file mode 100644 index 1c64672..0000000 --- a/app/views/merged/pages/show.haml +++ /dev/null @@ -1,24 +0,0 @@ -%p#notice= notice -.flex.flex-col.bg-white - .flex.items-center.justify-center.flex-1 - .max-w-xl.px-4.py-8.mx-auto.text-center - %h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl - Page #{@page.name} - --@page.sections.each do |section | - .grid.grid-cols-6.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' } - .relative.block.border.border-gray-100 - .p-4 - %h3.mt-4.text-lg.font-bold Section #{section.index + 1} - %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to( "Up" , "/index") - %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to "Down" , "/index" - %button.mt-4.rounded-lg.bg-blue-400.p-4 - =link_to "Edit" , section_path(section.id) - %button.mt-4.rounded-lg.bg-cyan-400.p-4 - =link_to "New" , "/index" - %button.mt-4.rounded-lg.bg-red-400.p-4 - =link_to "Delete" , "/index" - -section.content.each do |key , value| - = render "merged/sections/overview/#{key}", section: section , key: key , value: value diff --git a/app/views/merged/sections/index.html.haml b/app/views/merged/sections/index.html.haml index c448754..98980ec 100644 --- a/app/views/merged/sections/index.html.haml +++ b/app/views/merged/sections/index.html.haml @@ -3,16 +3,17 @@ .flex.items-center.justify-center.flex-1 .max-w-xl.px-4.py-8.mx-auto.text-center %h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl - Page #{@page.name} + Page + = @page.name -@page.sections.each do |section | .grid.grid-cols-5.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' } .relative.block.border.border-gray-100.p-4 %h3.mt-4.text-lg.font-bold Section #{section.index + 1} %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to( "Up" , "/index") + =link_to( "Up" , section_move_url(section.id , dir: :up) ) %button.mt-4.rounded-lg.bg-yellow-500.p-4 - =link_to "Down" , "/index" + =link_to( "Down" , section_move_url(section.id , dir: :down) ) %button.mt-4.rounded-lg.bg-blue-400.p-4 =link_to "Edit" , section_path(section.id) %button.mt-4.rounded-lg.bg-cyan-400.p-4 diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index caa980c..e85d20f 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -3,8 +3,8 @@ .flex.flex-col.bg-white .flex.items-center.justify-center.flex-1 .max-w-xl.px-4.py-8.mx-auto.text-center - %h1.text-2xl.font-bold.tracking-tight.text-gray-900 - Page #{link_to @section.page.name, page_url(@section.page.name)} + %h1.text-3xl.font-bold.tracking-tight.text-gray-900 + Page #{link_to @section.page.name, page_sections_url(@section.page.name)} .flex.items-center.justify-center.flex-1 %h3.text-xl.font-bold.tracking-tight.text-gray-900 Section #{@section.id} diff --git a/config/routes.rb b/config/routes.rb index 4aea482..a900caa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Merged::Engine.routes.draw do get "/styles/index" , to: "styles#index" - resources :pages , shallow: true do + resources :pages , except: [:show] , shallow: true do resources :sections do get :select_image get :set_image @@ -9,6 +9,7 @@ Merged::Engine.routes.draw do get :set_template get :select_card_template get :set_card_template + get :move resources :cards do get :select_image get :set_image