From 7966437324b0056c61af7c8cf55b2236c85a149c Mon Sep 17 00:00:00 2001 From: Torsten Date: Mon, 12 Dec 2022 18:17:48 +0200 Subject: [PATCH] fix all news --- app/controllers/merged/cards_controller.rb | 1 + app/controllers/merged/pages_controller.rb | 3 +- app/controllers/merged/sections_controller.rb | 2 +- app/models/merged/card.rb | 9 ++--- app/models/merged/optioned.rb | 6 ++-- app/models/merged/page.rb | 2 +- app/models/merged/section.rb | 36 ++++++++++--------- spec/models/merged/card_spec.rb | 5 +++ spec/models/merged/page_spec.rb | 9 ++--- spec/models/merged/section_spec.rb | 7 ++++ 10 files changed, 47 insertions(+), 33 deletions(-) diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 4790129..42b88ca 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -29,6 +29,7 @@ module Merged def new @section = Section.find(params[:section_id]) new_card = @section.new_card + new_card.save redirect_to section_cards_url(@section.id) , notice: "Card created" end diff --git a/app/controllers/merged/pages_controller.rb b/app/controllers/merged/pages_controller.rb index 35913e6..3404f27 100644 --- a/app/controllers/merged/pages_controller.rb +++ b/app/controllers/merged/pages_controller.rb @@ -11,7 +11,8 @@ module Merged message = Page.check_name(name) message = "Must enter name" if name.blank? if( message.nil?) - @page = Page.build_new(name) + @page = Page.new_page(name) + @page.save redirect_to new_page_section_url(@page.id) , notice: "Page was successfully created." else @pages = Page.all.values diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index cdd78c8..c91defe 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -23,7 +23,7 @@ module Merged page = Page.find(params[:page_id]) template = params[:template] new_section = page.new_section(template) - page.save + new_section.save if(template.blank?) # new redirect_to section_select_template_url(new_section.id), notice: "New section created" else # copy diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 5fbe55d..79cb8de 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -9,10 +9,6 @@ module Merged fields :index , :section_id, :id , :text , :header, :image - def template_style - section.card_template - end - def move_up swap_index_with(next_card) end @@ -60,11 +56,12 @@ module Merged def self.new_card(card_template , section_id , index) data = { section_id: section_id , index: index} - CardStyle.find_by_template( card_template ).fields.each do |key| + template = CardStyle.find_by_template( card_template ) + template.fields.each do |key| data[key] = "NEW" end card = Card.new(data) - card.add_default_options + card.add_default_options(template.options_definitions) card end diff --git a/app/models/merged/optioned.rb b/app/models/merged/optioned.rb index d3cdf01..a7d027f 100644 --- a/app/models/merged/optioned.rb +++ b/app/models/merged/optioned.rb @@ -23,12 +23,12 @@ module Merged end def set_option( option , value) - puts "#{template_style} setting option #{option}=#{value.class}" options[option] = value end - def add_default_options - option_definitions.each do |option| + def add_default_options( definitions = nil ) + definitions = option_definitions if definitions.nil? + definitions.each do |option| next unless option.default set_option( option.name , option.default) end diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 25b210d..fad92f7 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -15,7 +15,7 @@ module Merged def new_section(section_template) section_template = "section_spacer" if section_template.blank? - section = Section.new_section(section_template, self.id , sections.length) + section = Section.new_section(section_template, self.id , sections.length + 1) section end diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 50e6ca1..e3718d1 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -19,10 +19,8 @@ module Merged new_style = template_style if(new_style.has_cards?) unless card_template - self.card_template = CardStyle.first.name + self.card_template = CardStyle.first.template end - else - cards.delete_all end end @@ -43,7 +41,6 @@ module Merged end end - def move_up swap_index_with(next_section) end @@ -60,19 +57,9 @@ module Merged page.sections.where(index: index + 1).first end - 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? - if(style.has_cards?) - data["cards"] = [] - data["card_template"] = CardStyle.first.name - end - s = Section.new(data) - s.add_default_options - s + def new_card + card = Card.new_card(card_template, self.id , cards.length + 1) + card end def reset_index @@ -98,6 +85,21 @@ module Merged Section.save_all end + 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? + if(style.has_cards?) + data["cards"] = [] + data["card_template"] = CardStyle.first.name + end + s = Section.new(data) + s.add_default_options + s + end + def self.save_all data = Section.the_private_records.collect {|obj| obj.attributes} File.write( Section.full_path , data.to_yaml) diff --git a/spec/models/merged/card_spec.rb b/spec/models/merged/card_spec.rb index 0fe10b0..115b57b 100644 --- a/spec/models/merged/card_spec.rb +++ b/spec/models/merged/card_spec.rb @@ -23,6 +23,11 @@ module Merged expect(first.next_card.index).to be 2 end + it "create a new" do + card = Card.new_card("card_normal_square" , 1 , 1) + expect(card.index).to eq 1 + end + it "deletes " do card = Card.find(20) expect(card).not_to be nil diff --git a/spec/models/merged/page_spec.rb b/spec/models/merged/page_spec.rb index f4733d5..dba88e2 100644 --- a/spec/models/merged/page_spec.rb +++ b/spec/models/merged/page_spec.rb @@ -21,32 +21,33 @@ module Merged expect(section.index).to be index + 1 # because we have human index end end - it "deletes " do id = index.id index.delete expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) end - it "destroys " do id = index.id index.destroy Section.reload expect{Page.find(id) }.to raise_error(ActiveHash::RecordNotFound) end - it "destroys sections" do id = index.sections.first.id index.destroy 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 section with right index" do + should_be = index.sections.last.index + 1 + s = index.new_section("section_spacer") + expect(s.index).to eq should_be + end it "creates page" do name = "randomname" page = Page.new_page( name) diff --git a/spec/models/merged/section_spec.rb b/spec/models/merged/section_spec.rb index 53a3b08..260b430 100644 --- a/spec/models/merged/section_spec.rb +++ b/spec/models/merged/section_spec.rb @@ -50,6 +50,13 @@ module Merged expect(s.template).to eq "section_spacer" end + it "creates card with right index" do + s = Section.find_by_template("section_cards") + length = s.cards.length + c = s.new_card + expect(c.index).to eq length + 1 + end + it "deletes " do last_id = last.id last.delete