store all data in memory in preperarion for shallow routes

This commit is contained in:
2022-11-30 13:21:06 +02:00
parent 277d4dce36
commit 1d91ff2fc6
14 changed files with 149 additions and 57 deletions

View File

@ -4,13 +4,21 @@ module Merged
include ActiveModel::Conversion
extend ActiveModel::Naming
cattr_reader :all
@@all = {}
attr_reader :content , :section
def initialize(section , card_data)
@section = section
raise "No data #{card_data}" unless card_data.is_a?(Hash)
@content = card_data
raise "No id #{card_data}" unless id.is_a?(String)
@@all[self.id] = self
end
def id
@content['id']
end
end
end

View File

@ -4,27 +4,27 @@ module Merged
include ActiveModel::Conversion
extend ActiveModel::Naming
@@images = {}
cattr_reader :all
@@all = {}
def self.load_images()
glob = Rails.root.join Image.asset_root + "/*.*"
Dir[glob].each do |f|
file = f.split("/").last
Image.new( file )
end
end
attr_reader :name , :type , :size , :created_at , :updated_at
def initialize(filename)
puts "New Image #{filename}"
@name , @type = filename.split(".")
file = File.new(Rails.root.join(Image.asset_root,filename))
@created_at = file.birthtime
@updated_at = file.ctime
@size = (file.size/1024).to_i
end
def self.all
return @@images unless @@images.empty?
dir = Rails.root.join Image.asset_root + "/*.*"
Dir[dir].each do |f|
file = f.split("/").last
self.add( file )
end
@@images
@@all[@name] = self
end
#save an io with given name (without ending, that is taken from io)
@ -39,17 +39,12 @@ module Merged
end
def self.asset_root
"app/assets/images/" + root
"app/assets/images/" + image_root
end
def self.root
def self.image_root
"cms"
end
private
def self.add(filename)
key = filename.split(".").first
@@images[key] = Image.new(filename)
end
end
end

View File

@ -4,28 +4,34 @@ module Merged
include ActiveModel::Conversion
extend ActiveModel::Naming
@@root = "cms"
@@files = Set.new Dir.new(Rails.root.join(@@root)).children
# could be config options
def self.cms_root
"cms"
end
attr_reader :name , :content
cattr_reader :all
@@all = {}
def self.load_pages()
files = Set.new Dir.new(Rails.root.join(Page.cms_root)).children
files.each do |file|
page = Page.new(file)
end
end
attr_reader :name , :content , :sections
alias :id :name
def persisted?
false
end
def initialize file_name
def initialize( file_name )
@name = file_name.split(".").first
@content = YAML.load_file(Rails.root.join(@@root , file_name))
end
def sections
sections = []
@content = YAML.load_file(Rails.root.join(Page.cms_root , file_name))
@sections = {}
@content.each_with_index do |section_data, index|
sections << Section.new(self , index, section_data)
section = Section.new(self , index, section_data)
@sections[ section.id] = section
end
sections
@@all[@name] = self
end
def find_section(section_id)
@ -48,16 +54,12 @@ module Merged
end
def save
file_name = Rails.root.join(@@root , name + ".yaml")
file_name = Rails.root.join(Page.cms_root , name + ".yaml")
File.write( file_name , @content.to_yaml)
end
def self.all
@@files.collect{ |file| Page.new(file) }
end
def self.find(name)
Page.new(name + ".yaml")
@@all[name]
end
end

View File

@ -4,11 +4,10 @@ module Merged
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_reader :name , :content , :page , :index
cattr_reader :all
@@all = {}
def persisted?
false
end
attr_reader :name , :content , :page , :index , :cards
def initialize(page , index , section_data)
@page = page
@ -16,8 +15,17 @@ module Merged
raise "No hash #{section_data}" unless section_data.is_a?(Hash)
@index = index
@content = section_data
@@all[self.id] = self
@cards = {}
element = @content["cards"]
return if element.nil?
element.each do|card_content|
card = Card.new(self , card_content)
@cards[card.id] = card
end
end
def update(key , value)
return if key == "id" #not updating that
if(! @content[key].nil? )
@ -28,12 +36,6 @@ module Merged
@content[key] = value
end
def cards
element = @content["cards"]
return [] if element.nil?
element.collect{|card_content| Card.new(self , card_content)}
end
def template
@content["template"]
end
@ -49,10 +51,6 @@ module Merged
raise "Called"
end
def self.all
@page.sections
end
def self.find(page_name , section_id)
raise "buggy"
Page.new(name + ".yaml")