add index (synthetic) to sections

This commit is contained in:
2022-11-27 14:09:16 +02:00
parent 1823f5cb3e
commit 8d405e44e1
8 changed files with 31 additions and 15 deletions

View File

@ -18,12 +18,19 @@ module Cms
end
def sections
@content.collect{|section_data| Section.new(self , section_data)}
sections = []
@content.each_with_index do |section_data, index|
sections << Section.new(self , index, section_data)
end
sections
end
def find_section(section_id)
content = @content.find{|section| section["id"] == section_id }
Section.new(self , content)
@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}"
end
def first_template
@ -34,7 +41,7 @@ module Cms
section = Hash.new
section['id'] = SecureRandom.hex(10)
@content << section
Section.new(self , section)
Section.new(self , 0 , section)
end
def save

View File

@ -4,16 +4,18 @@ module Cms
include ActiveModel::Conversion
include ActiveModel::Dirty
attr_reader :name , :content , :page
attr_reader :name , :content , :page , :index
def persisted?
false
end
def initialize(page , section_data)
def initialize(page , index , section_data)
@page = page
raise "No number #{index}" unless index.is_a?(Integer)
raise "No has #{section_data}" unless section_data.is_a?(Hash)
@index = index
@content = section_data
# id = SecureRandom.hex(10) if new or not there
end
def template