fix new menthods

This commit is contained in:
Torsten 2022-12-12 14:28:57 +02:00
parent 60b270786c
commit 0d1adab3f3
5 changed files with 44 additions and 29 deletions

View File

@ -7,31 +7,15 @@ module Merged
"cms"
end
fields :name , :content , :size , :updated_at
fields :name , :size , :updated_at
def sections
Section.where(page_id: id).order(index: :asc)
end
def self.check_name(name)
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
nil
end
def self.build_new(name)
raise "only alphanumeric, not #{name}" unless check_name(name).nil?
name = name + ".yaml"
fullname = Rails.root.join(Page.cms_root , name )
File.write(fullname , "--- []\n")
Page.new(name)
end
def new_section(section_template)
section_template = "section_spacer" if section_template.blank?
section_data = Section.build_data(section_template)
index = sections.length
section = Section.new(self , index, section_data)
@sections << section
@content << section_data
section = Section.new_section(section_template, self.id , sections.length)
section
end
@ -75,6 +59,17 @@ module Merged
Page.save_all
end
def self.new_page(name )
raise "only alphanumeric, not #{name}" unless check_name(name).nil?
data = { name: name.dup }
Page.new(data)
end
def self.check_name(name)
return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil?
nil
end
def self.save_all
data = Page.the_private_records.collect {|obj| obj.attributes}
File.write( Page.full_path , data.to_yaml)

View File

@ -34,12 +34,6 @@ module Merged
! card_template.blank?
end
def new_card
card = Card.new_card( card_template , self.id , cards.length)
card.save
card
end
def remove_card(card)
from_index = card.index
@cards.delete_at(from_index)
@ -66,9 +60,9 @@ module Merged
page.sections.where(index: index + 1).first
end
def self.build_data(template)
data = { "template" => template , "id" => SecureRandom.hex(10) }
style = SectionStyle.sections[ template ]
def self.new_section(template , page_id , index)
data = { template: template , index: index , page_id: page_id}
style = SectionStyle.find_by_template( template)
style.fields.each do |key|
data[key] = key.upcase
end unless style.fields.blank?
@ -76,7 +70,9 @@ module Merged
data["cards"] = []
data["card_template"] = CardStyle.first.name
end
data
s = Section.new(data)
s.add_default_options
s
end
def reset_index

View File

@ -41,5 +41,16 @@ module Merged
Section.reload
expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound)
end
it "creates simple section" do
s = index.new_section("section_spacer")
expect(s).not_to be nil
expect(s.template).to eq "section_spacer"
end
it "creates page" do
name = "randomname"
page = Page.new_page( name)
expect(page.name).to eq name
end
end
end

View File

@ -45,6 +45,11 @@ module Merged
expect(last.next_section).to be nil
end
it "creates new spacer section" do
s = Section.new_section("section_spacer" , 1 , 1)
expect(s.template).to eq "section_spacer"
end
it "deletes " do
last_id = last.id
last.delete

View File

@ -4,6 +4,11 @@ module Merged
RSpec.describe SectionStyle, type: :model do
let(:first_section) {SectionStyle.all.first}
it "finds stye" do
spacer = SectionStyle.find_by_template("section_spacer")
expect(spacer).not_to be nil
end
it "has Style.sections" do
expect(SectionStyle.all.length).to be 7
end
@ -15,6 +20,9 @@ module Merged
spacer = SectionStyle.find_by_template("section_spacer")
expect(spacer.fields).to be nil
end
it "Spacer has no fields" do
spacer = SectionStyle.find_by_template("section_spacer")
expect(spacer.fields).to be nil
end
end
end