fix indexing and ordering for section and card
This commit is contained in:
parent
829d653b44
commit
912400b852
@ -35,7 +35,7 @@ module Merged
|
|||||||
def destroy
|
def destroy
|
||||||
@card.destroy
|
@card.destroy
|
||||||
@card.section.save
|
@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
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
|
@ -34,7 +34,7 @@ module Merged
|
|||||||
def destroy
|
def destroy
|
||||||
@section.destroy()
|
@section.destroy()
|
||||||
@section.page.save
|
@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
|
end
|
||||||
|
|
||||||
def set_image
|
def set_image
|
||||||
|
@ -28,10 +28,19 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def move_up
|
def move_up
|
||||||
@section.move_card_up(self)
|
swap_index_with(next_card)
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_down
|
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
|
end
|
||||||
|
|
||||||
def save
|
def save
|
||||||
|
@ -24,5 +24,13 @@ module Merged
|
|||||||
options[option] = value
|
options[option] = value
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
module Merged
|
module Merged
|
||||||
class Page < ActiveYaml::Base
|
class Page < ActiveYaml::Base
|
||||||
set_root_path Rails.root #ouside engines not necessary
|
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
|
# could be config options
|
||||||
def self.cms_root
|
def self.cms_root
|
||||||
"cms"
|
"cms"
|
||||||
@ -12,6 +11,10 @@ module Merged
|
|||||||
|
|
||||||
alias :id :name
|
alias :id :name
|
||||||
|
|
||||||
|
def sections
|
||||||
|
Section.where(page_id: name).order(index: :asc)
|
||||||
|
end
|
||||||
|
|
||||||
def self.check_name(name)
|
def self.check_name(name)
|
||||||
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
|
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
|
||||||
nil
|
nil
|
||||||
@ -53,32 +56,6 @@ module Merged
|
|||||||
@content[0]["template"]
|
@content[0]["template"]
|
||||||
end
|
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
|
def save
|
||||||
super
|
super
|
||||||
data = Page.all.collect {|obj| obj.attributes}
|
data = Page.all.collect {|obj| obj.attributes}
|
||||||
|
@ -4,14 +4,16 @@ module Merged
|
|||||||
|
|
||||||
include ActiveHash::Associations
|
include ActiveHash::Associations
|
||||||
belongs_to :page , class_name: "Merged::Page"
|
belongs_to :page , class_name: "Merged::Page"
|
||||||
has_many :cards , class_name: "Merged::Card" , scope: -> { order(index: :desc) }
|
|
||||||
|
|
||||||
|
|
||||||
include Optioned
|
include Optioned
|
||||||
|
|
||||||
fields :name , :page_id , :index , :cards , :options
|
fields :name , :page_id , :index , :cards , :options
|
||||||
fields :template , :card_template , :id , :text , :header, :image
|
fields :template , :card_template , :id , :text , :header, :image
|
||||||
|
|
||||||
|
def cards
|
||||||
|
Card.where(section_id: id).order(index: :asc)
|
||||||
|
end
|
||||||
|
|
||||||
def set_template(new_template)
|
def set_template(new_template)
|
||||||
self.template = new_template
|
self.template = new_template
|
||||||
new_style = template_style
|
new_style = template_style
|
||||||
@ -54,16 +56,13 @@ module Merged
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
|
||||||
@page.remove_section(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def move_up
|
def move_up
|
||||||
@page.move_section_up(self)
|
swap_index_with(next_section)
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_down
|
def move_down
|
||||||
@page.move_section_down(self)
|
swap_index_with(previous_section)
|
||||||
end
|
end
|
||||||
|
|
||||||
def previous_section
|
def previous_section
|
||||||
@ -74,33 +73,6 @@ module Merged
|
|||||||
page.sections.where(index: index + 1).first
|
page.sections.where(index: index + 1).first
|
||||||
end
|
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)
|
def update(key , value)
|
||||||
raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key)
|
raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key)
|
||||||
if(! @content[key].nil? ) # first setting ok, types not (yet?) specified
|
if(! @content[key].nil? ) # first setting ok, types not (yet?) specified
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
.flex.items-center.justify-center.flex-1
|
.flex.items-center.justify-center.flex-1
|
||||||
%h3.text-xl.font-bold.tracking-tight.text-gray-900
|
%h3.text-xl.font-bold.tracking-tight.text-gray-900
|
||||||
Cards for
|
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
|
.grid.grid-cols-4.gap-2.m-8
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
-@page.sections.each do |section |
|
-@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}"}
|
.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
|
.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( "Up" , section_move_url(section.id , dir: :up) )
|
||||||
= blue_button( "Down" , section_move_url(section.id , dir: :down) )
|
= blue_button( "Down" , section_move_url(section.id , dir: :down) )
|
||||||
= yellow_button("Edit" , section_path(section.id) )
|
= yellow_button("Edit" , section_path(section.id) )
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
.flex.items-center.justify-center.flex-1
|
.flex.items-center.justify-center.flex-1
|
||||||
.max-w-xl.px-4.py-8.mx-auto.text-center
|
.max-w-xl.px-4.py-8.mx-auto.text-center
|
||||||
%h1.text-3xl.font-bold.tracking-tight.text-gray-900
|
%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
|
.grid.grid-cols-4.gap-2.m-8
|
||||||
- @sections.each do |style|
|
- @sections.each do |style|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"}
|
Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"}
|
||||||
.flex.items-center.justify-center.flex-1
|
.flex.items-center.justify-center.flex-1
|
||||||
%h3.text-xl.font-bold.tracking-tight.text-gray-900
|
%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
|
- if @section.previous_section
|
||||||
=link_to "(prev)" , section_url(@section.previous_section.id)
|
=link_to "(prev)" , section_url(@section.previous_section.id)
|
||||||
- if @section.next_section
|
- if @section.next_section
|
||||||
|
@ -13,5 +13,14 @@ module Merged
|
|||||||
it "has index" do
|
it "has index" do
|
||||||
expect(first.index).to eq 1
|
expect(first.index).to eq 1
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -18,7 +18,7 @@ module Merged
|
|||||||
end
|
end
|
||||||
it "has section indexes" do
|
it "has section indexes" do
|
||||||
index.sections.each_with_index do |section, index|
|
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
require 'capybara/rspec'
|
require 'capybara/rspec'
|
||||||
|
|
||||||
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
|
# 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`.
|
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
||||||
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
||||||
@ -38,6 +39,12 @@ RSpec.configure do |config|
|
|||||||
mocks.verify_partial_doubles = true
|
mocks.verify_partial_doubles = true
|
||||||
end
|
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
|
# 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
|
# have no way to turn it off -- the option exists only for backwards
|
||||||
# compatibility in RSpec 3). It causes shared context metadata to be
|
# compatibility in RSpec 3). It causes shared context metadata to be
|
||||||
|
Loading…
Reference in New Issue
Block a user