From fc611ce5c43810a309a408609f50363fc881c70e Mon Sep 17 00:00:00 2001 From: Torsten Date: Thu, 22 Dec 2022 16:58:51 +0200 Subject: [PATCH] move saves into baes class --- app/models/merged/card.rb | 11 ----------- app/models/merged/image.rb | 11 ----------- app/models/merged/page.rb | 6 ------ app/models/merged/section.rb | 11 ----------- config/initializers/active_file.rb | 13 ++++++++++++- 5 files changed, 12 insertions(+), 40 deletions(-) diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 4432106..aa76130 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -43,17 +43,6 @@ module Merged section.reset_index if reindex end - def save - super - Card.save_all - end - - def self.save_all - data = Card.the_private_records.collect {|obj| obj.attributes} - File.write( Card.full_path , data.to_yaml) - Card.reload - 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/image.rb b/app/models/merged/image.rb index a8a0166..48cdb75 100644 --- a/app/models/merged/image.rb +++ b/app/models/merged/image.rb @@ -52,17 +52,6 @@ module Merged Image.delete( self.id ) end - def save - super - Image.save_all - end - - def self.save_all - data = Image.the_private_records.collect {|obj| obj.attributes} - File.write( Image.full_path , data.to_yaml) - Image.reload - end - def asset_name image_root + "/" + self.id.to_s + "." + self.type end diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 1dc4743..f5dfc2a 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -66,7 +66,6 @@ module Merged self.redirects = olds.join(" ") updated_at = Time.now super - Page.save_all end def self.new_page(name ) @@ -80,10 +79,5 @@ module Merged nil end - def self.save_all - data = Page.the_private_records.collect {|obj| obj.attributes} - File.write( Page.full_path , data.to_yaml) - Page.reload - end end end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 841c06d..516b54d 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -77,11 +77,6 @@ module Merged has_cards end - def save - super - Section.save_all - end - def self.new_section(template , page_id , index) data = { template: template , index: index , page_id: page_id} style = SectionStyle.find_by_template( template) @@ -97,11 +92,5 @@ module Merged s end - def self.save_all - data = Section.the_private_records.collect {|obj| obj.attributes} - File.write( Section.full_path , data.to_yaml) - Section.reload - end - end end diff --git a/config/initializers/active_file.rb b/config/initializers/active_file.rb index f2fd1b0..4e625ba 100644 --- a/config/initializers/active_file.rb +++ b/config/initializers/active_file.rb @@ -1,8 +1,19 @@ require "active_hash" -module ActiveHash +module ActiveYaml Base.class_eval do + def save + super + self.class.save_all + end + + def self.save_all + data = @records.collect {|obj| obj.attributes} + File.write( self.full_path , data.to_yaml) + self.reload + end + def self.delete(id) # only works with id's @record_index.delete(id.to_s) @records.delete_if{|record| record[:id] == id.to_i}