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" "cms"
end end
fields :name , :content , :size , :updated_at fields :name , :size , :updated_at
def sections def sections
Section.where(page_id: id).order(index: :asc) Section.where(page_id: id).order(index: :asc)
end 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) def new_section(section_template)
section_template = "section_spacer" if section_template.blank? section_template = "section_spacer" if section_template.blank?
section_data = Section.build_data(section_template) section = Section.new_section(section_template, self.id , sections.length)
index = sections.length
section = Section.new(self , index, section_data)
@sections << section
@content << section_data
section section
end end
@ -75,6 +59,17 @@ module Merged
Page.save_all Page.save_all
end 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 def self.save_all
data = Page.the_private_records.collect {|obj| obj.attributes} data = Page.the_private_records.collect {|obj| obj.attributes}
File.write( Page.full_path , data.to_yaml) File.write( Page.full_path , data.to_yaml)

View File

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

View File

@ -41,5 +41,16 @@ module Merged
Section.reload Section.reload
expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound)
end 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
end end

View File

@ -45,6 +45,11 @@ module Merged
expect(last.next_section).to be nil expect(last.next_section).to be nil
end 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 it "deletes " do
last_id = last.id last_id = last.id
last.delete last.delete

View File

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