gateway/app/models/cms/page.rb

62 lines
1.3 KiB
Ruby
Raw Normal View History

2022-11-25 13:46:49 +01:00
module Cms
class Page
2022-11-25 15:03:39 +01:00
include ActiveModel::Model
2022-11-25 13:46:49 +01:00
include ActiveModel::Conversion
2022-11-25 15:03:39 +01:00
include ActiveModel::Dirty
2022-11-25 13:46:49 +01:00
@@files = Set.new Dir.new(Rails.root.join("cms")).children
2022-11-25 15:03:39 +01:00
attr_reader :name , :content
2022-11-25 13:46:49 +01:00
def persisted?
false
end
def initialize file_name
@name = file_name.split(".").first
@content = YAML.load_file(Rails.root.join("cms" , file_name))
end
def sections
2022-11-27 13:09:16 +01:00
sections = []
@content.each_with_index do |section_data, index|
sections << Section.new(self , index, section_data)
end
sections
end
2022-11-26 18:07:20 +01:00
def find_section(section_id)
2022-11-27 13:09:16 +01:00
@content.each_with_index do |section , index|
next unless section["id"] == section_id
return Section.new(self , index , section)
end
raise "Page #{name} as no section #{section_id}"
2022-11-26 18:07:20 +01:00
end
def first_template
2022-11-25 15:03:39 +01:00
@content[0]["template"]
end
2022-11-26 18:07:20 +01:00
def new_section
section = Hash.new
section['id'] = SecureRandom.hex(10)
@content << section
2022-11-27 13:09:16 +01:00
Section.new(self , 0 , section)
2022-11-26 18:07:20 +01:00
end
2022-11-25 15:03:39 +01:00
def save
2022-11-26 18:07:20 +01:00
file_name = Rails.root.join("cms" , name + ".yaml")
File.write( file_name , @content.to_yaml)
2022-11-25 15:03:39 +01:00
end
2022-11-25 13:46:49 +01:00
def self.all
@@files.collect{ |file| Page.new(file) }
end
2022-11-25 15:03:39 +01:00
2022-11-25 15:17:54 +01:00
def self.find(name)
Page.new(name + ".yaml")
end
2022-11-25 13:46:49 +01:00
end
2022-11-25 12:21:52 +01:00
end