diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 493e429..602aef2 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -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 diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 2f52437..792e8a2 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -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 diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index b3e723d..532e95f 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -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 diff --git a/app/models/merged/optioned.rb b/app/models/merged/optioned.rb index 3b435f6..b7d0e25 100644 --- a/app/models/merged/optioned.rb +++ b/app/models/merged/optioned.rb @@ -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 diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 0585703..d795165 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -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} diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 2566c6f..57b8aa4 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -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 diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index 56fea8f..4999392 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -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 diff --git a/app/views/merged/sections/index.html.haml b/app/views/merged/sections/index.html.haml index 4b778c6..9d70d26 100644 --- a/app/views/merged/sections/index.html.haml +++ b/app/views/merged/sections/index.html.haml @@ -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) ) diff --git a/app/views/merged/sections/select_template.haml b/app/views/merged/sections/select_template.haml index ea84267..9b66022 100644 --- a/app/views/merged/sections/select_template.haml +++ b/app/views/merged/sections/select_template.haml @@ -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| diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index 440f637..d0f170e 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -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 diff --git a/spec/models/merged/card_spec.rb b/spec/models/merged/card_spec.rb index c5e9b69..ed617fb 100644 --- a/spec/models/merged/card_spec.rb +++ b/spec/models/merged/card_spec.rb @@ -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 diff --git a/spec/models/merged/page_spec.rb b/spec/models/merged/page_spec.rb index d73ed80..24f5439 100644 --- a/spec/models/merged/page_spec.rb +++ b/spec/models/merged/page_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 386e952..805247c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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