From ecaf0eb587cc0c040dbd6c81e33a32867bce46ea Mon Sep 17 00:00:00 2001 From: Torsten Date: Sat, 3 Dec 2022 00:14:19 +0200 Subject: [PATCH] copy sections (template) and fix cards inclusion --- app/controllers/merged/sections_controller.rb | 12 ++++++--- app/models/merged/page.rb | 4 +-- app/models/merged/section.rb | 26 ++++++++++++++++--- app/models/merged/style.rb | 7 +++-- app/views/merged/sections/index.html.haml | 1 + 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 8ebda26..bc66f30 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -19,8 +19,14 @@ module Merged def new page = Page.find(params[:page_id]) - new_section = page.new_section - redirect_to section_select_template_url(new_section.id) + template = params[:template] + new_section = page.new_section(template) + page.save + if(template.blank?) # new + redirect_to section_select_template_url(new_section.id) + else # copy + redirect_to section_url(new_section.id) + end end def remove @@ -39,7 +45,7 @@ module Merged def set_template template = params[:template] raise "no template given" if template.blank? - @section.content["template"] = template + @section.set_template( template ) @section.save redirect_to section_url(@section.id) end diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 27c58b9..b1042a4 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -34,8 +34,8 @@ module Merged @@all[@name] = self end - def new_section - section_template = params[:template] || "section_spacer" + 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) diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 79c04d6..f0d88e2 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -50,6 +50,22 @@ module Merged options[option] = value end + def set_template(new_template) + @content["template"] = new_template + new_style = template_style + if(new_style.cards?) + unless card_template + @content["card_template"] = Style.cards.keys.first + @content["cards"] = [] + raise "Should not have cards" unless cards.empty? + end + else + @content.delete("cards") + @content.delete("card_template") + @cards.clear + end + end + def template_style Style.sections[ template ] end @@ -58,7 +74,7 @@ module Merged end def cards? - ! cards.empty? + ! card_template.blank? end def new_card @@ -133,11 +149,15 @@ module Merged def self.build_data(template) data = { "template" => template , "id" => SecureRandom.hex(10) } - Style.sections[ template ].fields.each do |key| + style = Style.sections[ template ] + style.fields.each do |key| data[key] = key.upcase + end unless style.fields.blank? + if(style.cards?) + data["cards"] = [] + data["card_template"] = Style.cards.keys.first end data - end def self.find(section_id) diff --git a/app/models/merged/style.rb b/app/models/merged/style.rb index a1bd827..72864a9 100644 --- a/app/models/merged/style.rb +++ b/app/models/merged/style.rb @@ -2,7 +2,6 @@ module Merged class Style include ActiveModel::API - cattr_accessor :sections , :cards @@sections = {} @@cards = {} @@ -29,16 +28,16 @@ module Merged end def self.cards - self.all + self.load @@cards end def self.sections - self.all + self.load @@sections end - def self.all + def self.load if @@sections.length == 0 all = YAML.load_file(Engine.root.join("config/styles.yaml")) all["sections"].each do |content| diff --git a/app/views/merged/sections/index.html.haml b/app/views/merged/sections/index.html.haml index ec249e3..41a6821 100644 --- a/app/views/merged/sections/index.html.haml +++ b/app/views/merged/sections/index.html.haml @@ -13,6 +13,7 @@ = blue_button( "Up" , section_move_url(section.id , dir: :up) ) = blue_button( "Down" , section_move_url(section.id , dir: :down) ) = yellow_button("Edit" , section_path(section.id) ) + = green_button( "Copy" , new_page_section_url(@page.name, template: section.template) ) = red_button( "Delete" , section_remove_path(section.id) ) .relative.block.border.border-gray-100.p-4 %h3.mt-4.text-lg.font-bold Template