diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index ef678a7..c30099f 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -1,11 +1,12 @@ module Merged class SectionsController < MergedController - before_action :set_section , except: :index + before_action :set_section , except: [:index ,:new] #, only: %i[ show edit update destroy set_image select_image] def index @page = Page.find(params[:page_id]) end + def select_image @images = Image.all end @@ -16,6 +17,12 @@ module Merged @cards = Style.cards end + def new + page = Page.find(params[:page_id]) + new_section = page.new_section + redirect_to section_select_template_url(new_section.id) + end + def set_image @section.content["image"] = params[:image] @section.save @@ -49,9 +56,9 @@ module Merged end def update - @section.content.each do |key , value| - next if key == "id" - if(!params[key].nil?) + @section.allowed_fields.each do |key| + puts "Update #{key}" + if( params.has_key?(key) ) @section.update(key, params[key]) puts "updating:#{key}=#{params[key]}" end diff --git a/app/helpers/merged/view_helper.rb b/app/helpers/merged/view_helper.rb index 27cfb52..d165aae 100644 --- a/app/helpers/merged/view_helper.rb +++ b/app/helpers/merged/view_helper.rb @@ -7,6 +7,7 @@ module Merged end def bg(section) + return "" if section.image.blank? puts "--#{Image.image_root}/#{section.image}--" img = asset_url( "#{Image.image_root}/#{section.image}" ) "background-image: url('#{img}');" diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 0e86127..7d7144c 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -34,6 +34,15 @@ module Merged @@all[@name] = self end + def new_section + section_data = Section.build_data + index = sections.length + section = Section.new(self , index, section_data) + @sections << section + @content << section_data + section + end + def first_template @content[0]["template"] end @@ -41,16 +50,16 @@ module Merged def move_section_up(section) return if sections.length == 1 return if section.index == 0 - swap( section , sections[section.index - 1]) + swap_sections( section , sections[section.index - 1]) end def move_section_down(section) return if sections.length == 1 return if section.index == sections.last.index - swap( section , sections[section.index + 1]) + swap_sections( section , sections[section.index + 1]) end - def swap( this_section , that_section) + def swap_sections( this_section , that_section) # swap in the actual objects, index is cached in the objects this_old_index = this_section.index this_section.set_index( that_section.index ) diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index 1bd241b..d306f26 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -25,6 +25,12 @@ module Merged end end + def self.build_data + data = { "template" => "spacer"} + data["id"] = SecureRandom.hex(10) + data + end + [:template , :card_template , :id , :text , :header, :image].each do |meth| define_method(meth) do @content[meth.to_s] @@ -50,6 +56,13 @@ module Merged options[option] = value end + def template_style + Style.sections[ template ] + end + def allowed_fields + template_style.fields + end + def cards? ! cards.empty? end @@ -89,8 +102,8 @@ module Merged end def update(key , value) - return if key == "id" #not updating that - if(! @content[key].nil? ) + raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key) + if(! @content[key].nil? ) # first setting ok, types not (yet?) specified if( @content[key].class != value.class ) raise "Type mismatch #{key} #{key.class}!=#{value.class}" end diff --git a/app/views/merged/sections/index.html.haml b/app/views/merged/sections/index.html.haml index 5cafcfc..90dd857 100644 --- a/app/views/merged/sections/index.html.haml +++ b/app/views/merged/sections/index.html.haml @@ -13,7 +13,6 @@ = 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( "New" , "/index" ) = red_button( "Delete" , "/index" ) .relative.block.border.border-gray-100.p-4 %h3.mt-4.text-lg.font-bold Template @@ -38,3 +37,5 @@ .relative.block.border.border-gray-100.p-4 %h3.mt-4.text-lg.font-bold Options %p To be done +%p + = green_button( "New Section" , new_page_section_url(@page.name) ) diff --git a/app/views/merged/sections/select_template.haml b/app/views/merged/sections/select_template.haml index 14be504..07c07ff 100644 --- a/app/views/merged/sections/select_template.haml +++ b/app/views/merged/sections/select_template.haml @@ -1,5 +1,13 @@ +%p#notice= notice + +.flex.flex-col.bg-white + .flex.items-center.justify-center.flex-1 + .max-w-xl.px-4.py-8.mx-auto.text-center + %h1.text-3xl.font-bold.tracking-tight.text-gray-900 + Select Template for Section #{@section.index + 1} + .grid.grid-cols-4.gap-2.m-8 - - @sections.each do |style| + - @sections.each do |name , style| .relative.block.border.border-gray-100 = link_to( section_set_template_path( template: style.template )) do =image_tag(style.section_preview , class: "h-56 w-full object-contain lg:h-72") diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index 8769a64..b0d1562 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -10,7 +10,7 @@ Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"} .flex.items-center.justify-center.flex-1 %h3.text-xl.font-bold.tracking-tight.text-gray-900 - Section #{@section.id} + Section #{@section.index + 1} .grid.grid-cols-3.gap-2.m-8 .relative.block.border.border-gray-100