From 0d1adab3f3afd112d01a9662a121ffc53fdfe0d6 Mon Sep 17 00:00:00 2001 From: Torsten Date: Mon, 12 Dec 2022 14:28:57 +0200 Subject: [PATCH] fix new menthods --- app/models/merged/page.rb | 31 ++++++++++-------------- app/models/merged/section.rb | 16 +++++------- spec/models/merged/page_spec.rb | 11 +++++++++ spec/models/merged/section_spec.rb | 5 ++++ spec/models/merged/section_style_spec.rb | 10 +++++++- 5 files changed, 44 insertions(+), 29 deletions(-) diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 55a110d..25b210d 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -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) diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 11fe18f..50e6ca1 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -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 diff --git a/spec/models/merged/page_spec.rb b/spec/models/merged/page_spec.rb index 775064e..f4733d5 100644 --- a/spec/models/merged/page_spec.rb +++ b/spec/models/merged/page_spec.rb @@ -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 diff --git a/spec/models/merged/section_spec.rb b/spec/models/merged/section_spec.rb index 379d2de..53a3b08 100644 --- a/spec/models/merged/section_spec.rb +++ b/spec/models/merged/section_spec.rb @@ -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 diff --git a/spec/models/merged/section_style_spec.rb b/spec/models/merged/section_style_spec.rb index c5c8a09..2344c61 100644 --- a/spec/models/merged/section_style_spec.rb +++ b/spec/models/merged/section_style_spec.rb @@ -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