diff --git a/app/models/merged/active_base.rb b/app/models/merged/active_base.rb index 9eae27c..7f47f32 100644 --- a/app/models/merged/active_base.rb +++ b/app/models/merged/active_base.rb @@ -1,26 +1,21 @@ module Merged class ActiveBase < ActiveYaml::Base - def save + def edit_save! ChangeSet.current.edit(self.class.name , self.change_name) - super + save!() self.class.save_all end - def add_save + def add_save! ChangeSet.current.add(self.class.name , self.change_name) - super.save() + save!() self.class.save_all end - - def delete + def delete_save! ChangeSet.current.delete(self.class.name , self.change_name) self.class.delete(self.id) - end - - def destroy - delete self.class.save_all end diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 079d666..9f657d3 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -38,11 +38,6 @@ module Merged CardStyle.find_by_template( self.template ) end - def delete(reindex = true) - super() - section.reset_index if reindex - end - def self.new_card(card_template , section_id , index) data = { section_id: section_id , index: index} template = CardStyle.find_by_template( card_template ) diff --git a/app/models/merged/change_set.rb b/app/models/merged/change_set.rb index d12e73b..a4dfe1d 100644 --- a/app/models/merged/change_set.rb +++ b/app/models/merged/change_set.rb @@ -8,6 +8,10 @@ module Merged attr_reader :adds , :edits , :deletes def initialize + zero + end + + def zero @adds = [] @edits = [] @deletes = [] diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 5305c86..6f8e739 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -35,7 +35,7 @@ module Merged PageStyle.find_by_type( type ) end - def new_section(section_template) + def new_section(section_template = nil) section_template = "section_spacer" if section_template.blank? section = Section.new_section(section_template, self.id , sections.length + 1) section @@ -59,21 +59,9 @@ module Merged sections.each_with_index{|section, index| section.index = index + 1} end - def destroy - has_sections , has_cards = delete() - Page.save_all - if has_sections > 0 - Section.save_all - Card.save_all if has_cards > 0 - end - end - def delete - has_sections = sections.length - has_cards = 0 - sections.each {|section| has_cards += section.delete(false) } - super - [has_sections , has_cards] + sections.each {|section| section.delete } + delete_save! end def save(editor) diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 7b88396..9e5c4ed 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -67,18 +67,9 @@ module Merged cards.each_with_index{|card, index| card.index = index + 1} end - def destroy - has_cards = delete() - Section.save_all - Card.save_all if has_cards > 0 - end - - def delete( reindex = true ) - has_cards = cards.length - cards.each {|card| card.delete(false) } - super() - page.reset_index if reindex - has_cards + def delete + cards.each {|card| card.delete_save! } + delete_save!() end def self.new_section(template , page_id , index) diff --git a/app/models/merged/view_base.rb b/app/models/merged/view_base.rb index 4ec03b1..7ba3bb3 100644 --- a/app/models/merged/view_base.rb +++ b/app/models/merged/view_base.rb @@ -12,10 +12,16 @@ module Merged fields :options , :updated_at , :updated_by - def save( editor ) + def edit_save( editor ) self.updated_at = Time.now self.updated_by = editor - super() + edit_save! + end + + def add_save( editor ) + self.updated_at = Time.now + self.updated_by = editor + add_save! end def has_option?(option) diff --git a/test/models/card_write_test.rb b/test/models/card_write_test.rb index aaae0f3..b275c72 100644 --- a/test/models/card_write_test.rb +++ b/test/models/card_write_test.rb @@ -8,16 +8,16 @@ module Merged def test_deletes id = first.id - first.delete + first.delete_save! assert_raises(ActiveHash::RecordNotFound) {Card.find(id) } end - def test_destroys - id = first.id - first.delete - Card.reload - assert_raises(ActiveHash::RecordNotFound) {Card.find(id) } + def test_adds + card = Card.first.section.new_card + card.add_save! + assert_equal "NEW" , card.header end + end end diff --git a/test/models/change_set_test.rb b/test/models/change_set_test.rb new file mode 100644 index 0000000..268621c --- /dev/null +++ b/test/models/change_set_test.rb @@ -0,0 +1,90 @@ +require "test_helper" + +module Merged + module Zero + def change + ChangeSet.current + end + def setup + change.zero + end + end + class ChangeSetTest < ActiveSupport::TestCase + include Zero + def test_has_add + change.add("Section" , "name") + assert_equal "name", change.added("Section").first.last + assert_equal :Section, change.added("Section").first.first + end + def test_has_edit + change.edit("Section" , "name") + assert_equal "name", change.edited("Section").first.last + assert_equal :Section, change.edited("Section").first.first + end + def test_has_delete + change.delete("Section" , "name") + assert_equal "name", change.deleted("Section").first.last + assert_equal :Section, change.deleted("Section").first.first + end + + end + class ChangeSetWriteTest < ActiveSupport::TestCase + include Cleanup + include Zero + + def test_page_edit + studios = Page.first + studios.edit_save("email") + assert_equal "studios" , change.edited("Page").first.last + end + def test_section_edit + studios = Section.first + studios.edit_save("email") + assert_equal "studios:Studios" , change.edited("Section").first.last + end + def test_card_edit + studios = Card.first + studios.edit_save("email") + assert_equal "studios:Standard" , change.edited("Card").first.last + end + + def test_page_new + studios = Page.new_page("new") + studios.add_save("email") + assert_equal "new" , change.added("Page").first.last + assert_nil change.edited("Page").first + end + def test_section_new + studios = Page.first.new_section() + studios.add_save("email") + assert_equal "studios:" , change.added("Section").first.last + assert_nil change.edited("Section").first + end + def test_card_new + studios = Card.first.section.new_card() + studios.add_save("email") + assert_equal "studios:NEW" , change.added("Card").first.last + assert_nil change.edited("Card").first + end + + def test_page_delete + studios = Page.first + studios.delete_save! + assert_equal "studios" , change.deleted("Page").first.last + assert_nil change.edited("Page").first + end + def test_section_delete + studios = Section.first + studios.delete_save!() + assert_equal "studios:Studios" , change.deleted("Section").first.last + assert_nil change.edited("Section").first + end + def test_card_delete + studios = Card.first + studios.delete_save!() + assert_equal "studios:Standard" , change.deleted("Card").first.last + assert_nil change.edited("Card").first + end + + end +end diff --git a/test/models/page_write_test.rb b/test/models/page_write_test.rb index 2a930fe..f2f9e21 100644 --- a/test/models/page_write_test.rb +++ b/test/models/page_write_test.rb @@ -15,13 +15,13 @@ module Merged end def test_destroys id = index.id - index.destroy + index.delete Section.reload assert_raises(ActiveHash::RecordNotFound){Page.find(id) } end def test_destroys_sections id = index.sections.first.id - index.destroy + index.delete Section.reload assert_raises(ActiveHash::RecordNotFound){Page.find(id) } end diff --git a/test/models/section_write_test.rb b/test/models/section_write_test.rb index 39a88f3..7bf111c 100644 --- a/test/models/section_write_test.rb +++ b/test/models/section_write_test.rb @@ -25,13 +25,13 @@ module Merged def test_destroys last_id = last.id - last.destroy + last.delete Section.reload assert_raises(ActiveHash::RecordNotFound){Section.find(last_id) } end def test_destroys_cards card_id = last.cards.first.id - last.destroy + last.delete Section.reload assert_raises(ActiveHash::RecordNotFound){Card.find(card_id) } end