store all data in memory in preperarion for shallow routes
This commit is contained in:
parent
277d4dce36
commit
1d91ff2fc6
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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")
|
||||
|
@ -2,5 +2,5 @@ require "merged/version"
|
||||
require "merged/engine"
|
||||
|
||||
module Merged
|
||||
# Your code goes here...
|
||||
|
||||
end
|
||||
|
@ -7,6 +7,13 @@ module Merged
|
||||
add_image_assets(app.config , "card_preview")
|
||||
end
|
||||
|
||||
initializer "after_initialize" do |app|
|
||||
ActiveSupport::Reloader.to_prepare do
|
||||
Merged::Page.load_pages()
|
||||
Image.load_images()
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def add_image_assets(config , sub_dir )
|
||||
dir = Dir.new(Engine.root.join("app/assets/images/merged/" , sub_dir))
|
||||
|
@ -2,6 +2,13 @@ require 'rails_helper'
|
||||
|
||||
module Merged
|
||||
RSpec.describe Card, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
let(:first) {Card.all.values.first}
|
||||
|
||||
it "has Card.all" do
|
||||
expect(Card.all.class).to be Hash
|
||||
end
|
||||
it "has cards" do
|
||||
expect(first.class).to be Card
|
||||
end
|
||||
end
|
||||
end
|
||||
|
17
spec/models/merged/image_spec.rb
Normal file
17
spec/models/merged/image_spec.rb
Normal file
@ -0,0 +1,17 @@
|
||||
require 'rails_helper'
|
||||
|
||||
module Merged
|
||||
RSpec.describe Image, type: :model do
|
||||
let(:first) {Image.all.values.first}
|
||||
|
||||
it "has Pages.all" do
|
||||
expect(Image.all.class).to be Hash
|
||||
end
|
||||
it "has image" do
|
||||
expect(first.class).to be Image
|
||||
end
|
||||
it "image has name" do
|
||||
expect(first.name).to eq "large"
|
||||
end
|
||||
end
|
||||
end
|
17
spec/models/merged/page_spec.rb
Normal file
17
spec/models/merged/page_spec.rb
Normal file
@ -0,0 +1,17 @@
|
||||
require 'rails_helper'
|
||||
|
||||
module Merged
|
||||
RSpec.describe Page, type: :model do
|
||||
let(:index) {Page.find('index')}
|
||||
|
||||
it "has Pages.all" do
|
||||
expect(Page.all.class).to be Hash
|
||||
end
|
||||
it "has index page" do
|
||||
expect(index.class).to be Page
|
||||
end
|
||||
it "has sections" do
|
||||
expect(index.sections.length).to be 1
|
||||
end
|
||||
end
|
||||
end
|
17
spec/models/merged/section_spec.rb
Normal file
17
spec/models/merged/section_spec.rb
Normal file
@ -0,0 +1,17 @@
|
||||
require 'rails_helper'
|
||||
|
||||
module Merged
|
||||
RSpec.describe Section, type: :model do
|
||||
let(:first) {Section.all.values.first}
|
||||
|
||||
it "has Sections.all" do
|
||||
expect(Section.all.class).to be Hash
|
||||
end
|
||||
it "has index page" do
|
||||
expect(first.class).to be Section
|
||||
end
|
||||
it "has sections" do
|
||||
expect(first.cards.length).to be 2
|
||||
end
|
||||
end
|
||||
end
|
@ -1,7 +1,7 @@
|
||||
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
||||
require 'spec_helper'
|
||||
ENV['RAILS_ENV'] ||= 'test'
|
||||
require_relative '../config/environment'
|
||||
require_relative '../test/dummy/config/environment'
|
||||
# Prevent database truncation if the environment is production
|
||||
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
||||
require 'rspec/rails'
|
||||
|
BIN
test/dummy/app/assets/images/cms/large.webp
Normal file
BIN
test/dummy/app/assets/images/cms/large.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 211 KiB |
BIN
test/dummy/app/assets/images/cms/standard.webp
Normal file
BIN
test/dummy/app/assets/images/cms/standard.webp
Normal file
Binary file not shown.
After Width: | Height: | Size: 137 KiB |
24
test/dummy/cms/index.yaml
Normal file
24
test/dummy/cms/index.yaml
Normal file
@ -0,0 +1,24 @@
|
||||
---
|
||||
- template: section_2_col
|
||||
header: Sizes and kinds
|
||||
text: We offer different sizes and different types of studios for artists. There
|
||||
large and small rooms, with more or less light, also rooms with tiles for wet-work.
|
||||
The rooms are in the old hospital wings, so most are old patient rooms, but there
|
||||
are plenty of others too. Prices do not include electricity or vat, but do include
|
||||
the use of common spaces, see below.
|
||||
image:
|
||||
id: b7d16d4da518bafd3d39
|
||||
card_template: card_full_image
|
||||
cards:
|
||||
- header: Standard
|
||||
text: This is the standard 2 patient room. They are mostly towards the south,
|
||||
so may have great, or too much light, depending on how you see it. The size
|
||||
is about 3x5, cost 120e.
|
||||
image: standard
|
||||
id: b7d16d4da618bafd3d39
|
||||
- header: Large
|
||||
text: The old 4 patient rooms are basically twice the size as the small. They
|
||||
are large enough to be shared. The size is about 30m2 and the cost 240e. Like
|
||||
the small rooms these are south facing, very light.
|
||||
image: large
|
||||
id: b7d76d4da518bafd3d39
|
Loading…
Reference in New Issue
Block a user