adding new section
This commit is contained in:
parent
ffbdccb263
commit
2eaa344ed0
@ -1,11 +1,12 @@
|
|||||||
module Merged
|
module Merged
|
||||||
class SectionsController < MergedController
|
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]
|
#, only: %i[ show edit update destroy set_image select_image]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@page = Page.find(params[:page_id])
|
@page = Page.find(params[:page_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def select_image
|
def select_image
|
||||||
@images = Image.all
|
@images = Image.all
|
||||||
end
|
end
|
||||||
@ -16,6 +17,12 @@ module Merged
|
|||||||
@cards = Style.cards
|
@cards = Style.cards
|
||||||
end
|
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
|
def set_image
|
||||||
@section.content["image"] = params[:image]
|
@section.content["image"] = params[:image]
|
||||||
@section.save
|
@section.save
|
||||||
@ -49,9 +56,9 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@section.content.each do |key , value|
|
@section.allowed_fields.each do |key|
|
||||||
next if key == "id"
|
puts "Update #{key}"
|
||||||
if(!params[key].nil?)
|
if( params.has_key?(key) )
|
||||||
@section.update(key, params[key])
|
@section.update(key, params[key])
|
||||||
puts "updating:#{key}=#{params[key]}"
|
puts "updating:#{key}=#{params[key]}"
|
||||||
end
|
end
|
||||||
|
@ -7,6 +7,7 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def bg(section)
|
def bg(section)
|
||||||
|
return "" if section.image.blank?
|
||||||
puts "--#{Image.image_root}/#{section.image}--"
|
puts "--#{Image.image_root}/#{section.image}--"
|
||||||
img = asset_url( "#{Image.image_root}/#{section.image}" )
|
img = asset_url( "#{Image.image_root}/#{section.image}" )
|
||||||
"background-image: url('#{img}');"
|
"background-image: url('#{img}');"
|
||||||
|
@ -34,6 +34,15 @@ module Merged
|
|||||||
@@all[@name] = self
|
@@all[@name] = self
|
||||||
end
|
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
|
def first_template
|
||||||
@content[0]["template"]
|
@content[0]["template"]
|
||||||
end
|
end
|
||||||
@ -41,16 +50,16 @@ module Merged
|
|||||||
def move_section_up(section)
|
def move_section_up(section)
|
||||||
return if sections.length == 1
|
return if sections.length == 1
|
||||||
return if section.index == 0
|
return if section.index == 0
|
||||||
swap( section , sections[section.index - 1])
|
swap_sections( section , sections[section.index - 1])
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_section_down(section)
|
def move_section_down(section)
|
||||||
return if sections.length == 1
|
return if sections.length == 1
|
||||||
return if section.index == sections.last.index
|
return if section.index == sections.last.index
|
||||||
swap( section , sections[section.index + 1])
|
swap_sections( section , sections[section.index + 1])
|
||||||
end
|
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
|
# swap in the actual objects, index is cached in the objects
|
||||||
this_old_index = this_section.index
|
this_old_index = this_section.index
|
||||||
this_section.set_index( that_section.index )
|
this_section.set_index( that_section.index )
|
||||||
|
@ -25,6 +25,12 @@ module Merged
|
|||||||
end
|
end
|
||||||
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|
|
[:template , :card_template , :id , :text , :header, :image].each do |meth|
|
||||||
define_method(meth) do
|
define_method(meth) do
|
||||||
@content[meth.to_s]
|
@content[meth.to_s]
|
||||||
@ -50,6 +56,13 @@ module Merged
|
|||||||
options[option] = value
|
options[option] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def template_style
|
||||||
|
Style.sections[ template ]
|
||||||
|
end
|
||||||
|
def allowed_fields
|
||||||
|
template_style.fields
|
||||||
|
end
|
||||||
|
|
||||||
def cards?
|
def cards?
|
||||||
! cards.empty?
|
! cards.empty?
|
||||||
end
|
end
|
||||||
@ -89,8 +102,8 @@ module Merged
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update(key , value)
|
def update(key , value)
|
||||||
return if key == "id" #not updating that
|
raise "unsuported field #{key} for #{template}" unless allowed_fields.include?(key)
|
||||||
if(! @content[key].nil? )
|
if(! @content[key].nil? ) # first setting ok, types not (yet?) specified
|
||||||
if( @content[key].class != value.class )
|
if( @content[key].class != value.class )
|
||||||
raise "Type mismatch #{key} #{key.class}!=#{value.class}"
|
raise "Type mismatch #{key} #{key.class}!=#{value.class}"
|
||||||
end
|
end
|
||||||
|
@ -13,7 +13,6 @@
|
|||||||
= 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( "New" , "/index" )
|
|
||||||
= red_button( "Delete" , "/index" )
|
= red_button( "Delete" , "/index" )
|
||||||
.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
|
||||||
@ -38,3 +37,5 @@
|
|||||||
.relative.block.border.border-gray-100.p-4
|
.relative.block.border.border-gray-100.p-4
|
||||||
%h3.mt-4.text-lg.font-bold Options
|
%h3.mt-4.text-lg.font-bold Options
|
||||||
%p To be done
|
%p To be done
|
||||||
|
%p
|
||||||
|
= green_button( "New Section" , new_page_section_url(@page.name) )
|
||||||
|
@ -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
|
.grid.grid-cols-4.gap-2.m-8
|
||||||
- @sections.each do |style|
|
- @sections.each do |name , style|
|
||||||
.relative.block.border.border-gray-100
|
.relative.block.border.border-gray-100
|
||||||
= link_to( section_set_template_path( template: style.template )) do
|
= 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")
|
=image_tag(style.section_preview , class: "h-56 w-full object-contain lg:h-72")
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"}
|
Page #{link_to @section.page.name, page_sections_url(@section.page.name), class: "underline"}
|
||||||
.flex.items-center.justify-center.flex-1
|
.flex.items-center.justify-center.flex-1
|
||||||
%h3.text-xl.font-bold.tracking-tight.text-gray-900
|
%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
|
.grid.grid-cols-3.gap-2.m-8
|
||||||
.relative.block.border.border-gray-100
|
.relative.block.border.border-gray-100
|
||||||
|
Loading…
x
Reference in New Issue
Block a user