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
|
include ActiveModel::Conversion
|
||||||
extend ActiveModel::Naming
|
extend ActiveModel::Naming
|
||||||
|
|
||||||
|
cattr_reader :all
|
||||||
|
@@all = {}
|
||||||
|
|
||||||
attr_reader :content , :section
|
attr_reader :content , :section
|
||||||
|
|
||||||
def initialize(section , card_data)
|
def initialize(section , card_data)
|
||||||
@section = section
|
@section = section
|
||||||
raise "No data #{card_data}" unless card_data.is_a?(Hash)
|
raise "No data #{card_data}" unless card_data.is_a?(Hash)
|
||||||
@content = card_data
|
@content = card_data
|
||||||
|
raise "No id #{card_data}" unless id.is_a?(String)
|
||||||
|
@@all[self.id] = self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def id
|
||||||
|
@content['id']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,27 +4,27 @@ module Merged
|
|||||||
include ActiveModel::Conversion
|
include ActiveModel::Conversion
|
||||||
extend ActiveModel::Naming
|
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
|
attr_reader :name , :type , :size , :created_at , :updated_at
|
||||||
|
|
||||||
def initialize(filename)
|
def initialize(filename)
|
||||||
puts "New Image #{filename}"
|
|
||||||
@name , @type = filename.split(".")
|
@name , @type = filename.split(".")
|
||||||
file = File.new(Rails.root.join(Image.asset_root,filename))
|
file = File.new(Rails.root.join(Image.asset_root,filename))
|
||||||
@created_at = file.birthtime
|
@created_at = file.birthtime
|
||||||
@updated_at = file.ctime
|
@updated_at = file.ctime
|
||||||
@size = (file.size/1024).to_i
|
@size = (file.size/1024).to_i
|
||||||
end
|
@@all[@name] = self
|
||||||
|
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
#save an io with given name (without ending, that is taken from io)
|
#save an io with given name (without ending, that is taken from io)
|
||||||
@ -39,17 +39,12 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.asset_root
|
def self.asset_root
|
||||||
"app/assets/images/" + root
|
"app/assets/images/" + image_root
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.root
|
def self.image_root
|
||||||
"cms"
|
"cms"
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def self.add(filename)
|
|
||||||
key = filename.split(".").first
|
|
||||||
@@images[key] = Image.new(filename)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -4,28 +4,34 @@ module Merged
|
|||||||
include ActiveModel::Conversion
|
include ActiveModel::Conversion
|
||||||
extend ActiveModel::Naming
|
extend ActiveModel::Naming
|
||||||
|
|
||||||
@@root = "cms"
|
# could be config options
|
||||||
@@files = Set.new Dir.new(Rails.root.join(@@root)).children
|
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
|
alias :id :name
|
||||||
|
|
||||||
def persisted?
|
def initialize( file_name )
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize file_name
|
|
||||||
@name = file_name.split(".").first
|
@name = file_name.split(".").first
|
||||||
@content = YAML.load_file(Rails.root.join(@@root , file_name))
|
@content = YAML.load_file(Rails.root.join(Page.cms_root , file_name))
|
||||||
end
|
@sections = {}
|
||||||
|
|
||||||
def sections
|
|
||||||
sections = []
|
|
||||||
@content.each_with_index do |section_data, index|
|
@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
|
end
|
||||||
sections
|
@@all[@name] = self
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_section(section_id)
|
def find_section(section_id)
|
||||||
@ -48,16 +54,12 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def save
|
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)
|
File.write( file_name , @content.to_yaml)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.all
|
|
||||||
@@files.collect{ |file| Page.new(file) }
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.find(name)
|
def self.find(name)
|
||||||
Page.new(name + ".yaml")
|
@@all[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -4,11 +4,10 @@ module Merged
|
|||||||
include ActiveModel::Conversion
|
include ActiveModel::Conversion
|
||||||
extend ActiveModel::Naming
|
extend ActiveModel::Naming
|
||||||
|
|
||||||
attr_reader :name , :content , :page , :index
|
cattr_reader :all
|
||||||
|
@@all = {}
|
||||||
|
|
||||||
def persisted?
|
attr_reader :name , :content , :page , :index , :cards
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
def initialize(page , index , section_data)
|
def initialize(page , index , section_data)
|
||||||
@page = page
|
@page = page
|
||||||
@ -16,7 +15,16 @@ module Merged
|
|||||||
raise "No hash #{section_data}" unless section_data.is_a?(Hash)
|
raise "No hash #{section_data}" unless section_data.is_a?(Hash)
|
||||||
@index = index
|
@index = index
|
||||||
@content = section_data
|
@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
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def update(key , value)
|
def update(key , value)
|
||||||
return if key == "id" #not updating that
|
return if key == "id" #not updating that
|
||||||
@ -28,12 +36,6 @@ module Merged
|
|||||||
@content[key] = value
|
@content[key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
def cards
|
|
||||||
element = @content["cards"]
|
|
||||||
return [] if element.nil?
|
|
||||||
element.collect{|card_content| Card.new(self , card_content)}
|
|
||||||
end
|
|
||||||
|
|
||||||
def template
|
def template
|
||||||
@content["template"]
|
@content["template"]
|
||||||
end
|
end
|
||||||
@ -49,10 +51,6 @@ module Merged
|
|||||||
raise "Called"
|
raise "Called"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.all
|
|
||||||
@page.sections
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.find(page_name , section_id)
|
def self.find(page_name , section_id)
|
||||||
raise "buggy"
|
raise "buggy"
|
||||||
Page.new(name + ".yaml")
|
Page.new(name + ".yaml")
|
||||||
|
@ -2,5 +2,5 @@ require "merged/version"
|
|||||||
require "merged/engine"
|
require "merged/engine"
|
||||||
|
|
||||||
module Merged
|
module Merged
|
||||||
# Your code goes here...
|
|
||||||
end
|
end
|
||||||
|
@ -7,6 +7,13 @@ module Merged
|
|||||||
add_image_assets(app.config , "card_preview")
|
add_image_assets(app.config , "card_preview")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
initializer "after_initialize" do |app|
|
||||||
|
ActiveSupport::Reloader.to_prepare do
|
||||||
|
Merged::Page.load_pages()
|
||||||
|
Image.load_images()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def add_image_assets(config , sub_dir )
|
def add_image_assets(config , sub_dir )
|
||||||
dir = Dir.new(Engine.root.join("app/assets/images/merged/" , sub_dir))
|
dir = Dir.new(Engine.root.join("app/assets/images/merged/" , sub_dir))
|
||||||
|
@ -2,6 +2,13 @@ require 'rails_helper'
|
|||||||
|
|
||||||
module Merged
|
module Merged
|
||||||
RSpec.describe Card, type: :model do
|
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
|
||||||
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'
|
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
ENV['RAILS_ENV'] ||= 'test'
|
ENV['RAILS_ENV'] ||= 'test'
|
||||||
require_relative '../config/environment'
|
require_relative '../test/dummy/config/environment'
|
||||||
# Prevent database truncation if the environment is production
|
# Prevent database truncation if the environment is production
|
||||||
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
abort("The Rails environment is running in production mode!") if Rails.env.production?
|
||||||
require 'rspec/rails'
|
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