copy sections (template) and fix cards inclusion

This commit is contained in:
Torsten 2022-12-03 00:14:19 +02:00
parent 1e452ca62a
commit ecaf0eb587
5 changed files with 38 additions and 12 deletions

View File

@ -19,8 +19,14 @@ module Merged
def new def new
page = Page.find(params[:page_id]) page = Page.find(params[:page_id])
new_section = page.new_section template = params[:template]
redirect_to section_select_template_url(new_section.id) 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 end
def remove def remove
@ -39,7 +45,7 @@ module Merged
def set_template def set_template
template = params[:template] template = params[:template]
raise "no template given" if template.blank? raise "no template given" if template.blank?
@section.content["template"] = template @section.set_template( template )
@section.save @section.save
redirect_to section_url(@section.id) redirect_to section_url(@section.id)
end end

View File

@ -34,8 +34,8 @@ module Merged
@@all[@name] = self @@all[@name] = self
end end
def new_section def new_section(section_template)
section_template = params[:template] || "section_spacer" section_template = "section_spacer" if section_template.blank?
section_data = Section.build_data(section_template) section_data = Section.build_data(section_template)
index = sections.length index = sections.length
section = Section.new(self , index, section_data) section = Section.new(self , index, section_data)

View File

@ -50,6 +50,22 @@ module Merged
options[option] = value options[option] = value
end 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 def template_style
Style.sections[ template ] Style.sections[ template ]
end end
@ -58,7 +74,7 @@ module Merged
end end
def cards? def cards?
! cards.empty? ! card_template.blank?
end end
def new_card def new_card
@ -133,11 +149,15 @@ module Merged
def self.build_data(template) def self.build_data(template)
data = { "template" => template , "id" => SecureRandom.hex(10) } 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 data[key] = key.upcase
end unless style.fields.blank?
if(style.cards?)
data["cards"] = []
data["card_template"] = Style.cards.keys.first
end end
data data
end end
def self.find(section_id) def self.find(section_id)

View File

@ -2,7 +2,6 @@ module Merged
class Style class Style
include ActiveModel::API include ActiveModel::API
cattr_accessor :sections , :cards
@@sections = {} @@sections = {}
@@cards = {} @@cards = {}
@ -29,16 +28,16 @@ module Merged
end end
def self.cards def self.cards
self.all self.load
@@cards @@cards
end end
def self.sections def self.sections
self.all self.load
@@sections @@sections
end end
def self.all def self.load
if @@sections.length == 0 if @@sections.length == 0
all = YAML.load_file(Engine.root.join("config/styles.yaml")) all = YAML.load_file(Engine.root.join("config/styles.yaml"))
all["sections"].each do |content| all["sections"].each do |content|

View File

@ -13,6 +13,7 @@
= blue_button( "Up" , section_move_url(section.id , dir: :up) ) = blue_button( "Up" , section_move_url(section.id , dir: :up) )
= blue_button( "Down" , section_move_url(section.id , dir: :down) ) = blue_button( "Down" , section_move_url(section.id , dir: :down) )
= yellow_button("Edit" , section_path(section.id) ) = 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) ) = red_button( "Delete" , section_remove_path(section.id) )
.relative.block.border.border-gray-100.p-4 .relative.block.border.border-gray-100.p-4
%h3.mt-4.text-lg.font-bold Template %h3.mt-4.text-lg.font-bold Template