fix indexing and ordering for section and card

This commit is contained in:
Torsten 2022-12-11 14:03:21 +02:00
parent 829d653b44
commit 912400b852
13 changed files with 53 additions and 71 deletions

View File

@ -35,7 +35,7 @@ module Merged
def destroy
@card.destroy
@card.section.save
redirect_to section_cards_url(@card.section.id) , notice: "Card #{@card.index + 1} removed"
redirect_to section_cards_url(@card.section.id) , notice: "Card #{@card.index} removed"
end
def update

View File

@ -34,7 +34,7 @@ module Merged
def destroy
@section.destroy()
@section.page.save
redirect_to page_sections_url(@section.page.name) , notice: "Section #{@section.index + 1} removed"
redirect_to page_sections_url(@section.page.name) , notice: "Section #{@section.index} removed"
end
def set_image

View File

@ -28,10 +28,19 @@ module Merged
end
def move_up
@section.move_card_up(self)
swap_index_with(next_card)
end
def move_down
@section.move_card_down(self)
swap_index_with(previous_card)
end
def previous_card
section.cards.where(index: index - 1).first
end
def next_card
section.cards.where(index: index + 1).first
end
def save

View File

@ -24,5 +24,13 @@ module Merged
options[option] = value
end
#other may be nil
def swap_index_with(other)
return unless other
old_index = self.index
self.index = other.index
other.index = old_index
end
end
end

View File

@ -1,8 +1,7 @@
module Merged
class Page < ActiveYaml::Base
set_root_path Rails.root #ouside engines not necessary
include ActiveHash::Associations
has_many :sections , class_name: "Merged::Section" , scope: -> { order(index: :desc) }
# could be config options
def self.cms_root
"cms"
@ -12,6 +11,10 @@ module Merged
alias :id :name
def sections
Section.where(page_id: name).order(index: :asc)
end
def self.check_name(name)
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
nil
@ -53,32 +56,6 @@ module Merged
@content[0]["template"]
end
def move_section_up(section)
return if sections.length == 1
return if section.index == 0
swap_sections( section , sections[section.index - 1])
end
def move_section_down(section)
return if sections.length == 1
return if section.index == sections.last.index
swap_sections( section , sections[section.index + 1])
end
def swap_sections( 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
super
data = Page.all.collect {|obj| obj.attributes}

View File

@ -4,14 +4,16 @@ module Merged
include ActiveHash::Associations
belongs_to :page , class_name: "Merged::Page"
has_many :cards , class_name: "Merged::Card" , scope: -> { order(index: :desc) }
include Optioned
fields :name , :page_id , :index , :cards , :options
fields :template , :card_template , :id , :text , :header, :image
def cards
Card.where(section_id: id).order(index: :asc)
end
def set_template(new_template)
self.template = new_template
new_style = template_style
@ -54,16 +56,13 @@ module Merged
end
end
def destroy
@page.remove_section(self)
end
def move_up
@page.move_section_up(self)
swap_index_with(next_section)
end
def move_down
@page.move_section_down(self)
swap_index_with(previous_section)
end
def previous_section
@ -74,33 +73,6 @@ module Merged
page.sections.where(index: index + 1).first
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)
raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key)
if(! @content[key].nil? ) # first setting ok, types not (yet?) specified

View File

@ -9,7 +9,7 @@
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Cards for
= link_to "Section #{@section.index + 1}", section_url( @section.id) , class: "underline"
= link_to "Section #{@section.index}", section_url( @section.id) , class: "underline"
.grid.grid-cols-4.gap-2.m-8

View File

@ -11,7 +11,7 @@
-@page.sections.each do |section |
.grid.grid-cols-5.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' , id: "section_#{section.id}"}
.relative.block.border.border-gray-100.p-4
%h3.mt-4.text-lg.font-bold Section #{section.index + 1} : #{section.header}
%h3.mt-4.text-lg.font-bold Section #{section.index} : #{section.header}
= blue_button( "Up" , section_move_url(section.id , dir: :up) )
= blue_button( "Down" , section_move_url(section.id , dir: :down) )
= yellow_button("Edit" , section_path(section.id) )

View File

@ -6,7 +6,7 @@
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-3xl.font-bold.tracking-tight.text-gray-900
Select Template for Section #{@section.index + 1}
Select Template for Section #{@section.index}
.grid.grid-cols-4.gap-2.m-8
- @sections.each do |style|

View File

@ -8,7 +8,7 @@
Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"}
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Section #{@section.index + 1} / #{@section.page.sections.length}
Section #{@section.index} / #{@section.page.sections.length}
- if @section.previous_section
=link_to "(prev)" , section_url(@section.previous_section.id)
- if @section.next_section

View File

@ -13,5 +13,14 @@ module Merged
it "has index" do
expect(first.index).to eq 1
end
it "first has no previous" do
expect(first.index).to be 1
expect(first.previous_card).to be nil
end
it "first has next" do
expect(first.next_card.index).to be 2
end
end
end

View File

@ -18,7 +18,7 @@ module Merged
end
it "has section indexes" do
index.sections.each_with_index do |section, index|
expect(section.index).to be index + 1
expect(section.index).to be index + 1 # because we have human index
end
end
end

View File

@ -1,4 +1,5 @@
require 'capybara/rspec'
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
@ -38,6 +39,12 @@ RSpec.configure do |config|
mocks.verify_partial_doubles = true
end
config.before(:each) do |example|
module Merged
[Page, Section, Card].each { |m| m.reload(true) }
end
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be