first go at page view, list of sections, wip

This commit is contained in:
Torsten 2022-11-25 18:56:46 +02:00
parent 0d159912f9
commit 1245c55b65
11 changed files with 73 additions and 30 deletions

View File

@ -1,10 +1,10 @@
module Cms
class PagesController < CmsController
before_action :set_cms_page, only: %i[ show edit update destroy ]
before_action :set_page, only: %i[ show edit update destroy ]
# GET /cms/pages
def index
@cms_pages = Cms::Page.all
@pages = Cms::Page.all
end
# GET /cms/pages/1
@ -13,7 +13,7 @@ module Cms
# GET /cms/pages/new
def new
@cms_page = Cms::Page.new
@page = Cms::Page.new
end
# GET /cms/pages/1/edit
@ -22,10 +22,10 @@ module Cms
# POST /cms/pages
def create
@cms_page = Cms::Page.new(cms_page_params)
@page = Cms::Page.new(page_params)
if @cms_page.save
redirect_to @cms_page, notice: "Page was successfully created."
if @page.save
redirect_to @page, notice: "Page was successfully created."
else
render :new, status: :unprocessable_entity
end
@ -33,8 +33,8 @@ module Cms
# PATCH/PUT /cms/pages/1
def update
if @cms_page.update(cms_page_params)
redirect_to @cms_page, notice: "Page was successfully updated."
if @page.update(page_params)
redirect_to @page, notice: "Page was successfully updated."
else
render :edit, status: :unprocessable_entity
end
@ -42,19 +42,19 @@ module Cms
# DELETE /cms/pages/1
def destroy
@cms_page.destroy
redirect_to cms_pages_url, notice: "Page was successfully destroyed."
@page.destroy
redirect_to page_url, notice: "Page was successfully destroyed."
end
private
# Use callbacks to share common setup or constraints between actions.
def set_cms_page
@cms_page = Cms::Page.find(params[:id])
def set_page
@page = Cms::Page.find(params[:id])
end
# Only allow a list of trusted parameters through.
def cms_page_params
params.fetch(:cms_page, {})
def page_params
params.fetch(:page, {})
end
end
end

View File

@ -1,5 +0,0 @@
module Cms
def self.table_name_prefix
"cms_"
end
end

View File

@ -8,10 +8,6 @@ module Cms
attr_reader :name , :content
def id
@name
end
def persisted?
false
end
@ -21,6 +17,10 @@ module Cms
@content = YAML.load_file(Rails.root.join("cms" , file_name))
end
def sections
@content.collect{|section_data| Section.new(self , section_data)}
end
def template
@content[0]["template"]
end

36
app/models/cms/section.rb Normal file
View File

@ -0,0 +1,36 @@
module Cms
class Section
include ActiveModel::Model
include ActiveModel::Conversion
include ActiveModel::Dirty
attr_reader :name , :content , :page
def persisted?
false
end
def initialize(page , section_data)
@page = page
@content = section_data
# id = SecureRandom.hex(10) if new or not there
end
def template
@content["template"]
end
def save
false
end
def self.all
@page.sections
end
def self.find(page_name , section_id)
Page.new(name + ".yaml")
end
end
end

View File

@ -14,7 +14,7 @@
%th.whitespace-nowrap.px-4.py-2.text-left.font-medium.text-gray-900
Status
%tbody.divide-y.divide-gray-200
- @cms_pages.each do |cms_page|
- @pages.each do |cms_page|
%tr
%td.whitespace-nowrap.px-4.py-2.text-gray-700
= link_to cms_page.name , cms_page_path(cms_page.name)

View File

@ -0,0 +1,18 @@
%p#notice= notice
-@page.sections.each_with_index do |section , index|
.grid.grid-cols-6.gap-4.m-8{class: (index%2)==1 ? 'bg-emerald-50/50' : 'bg-yellow-50/50' }
.relative.block.border.border-gray-100
=#image_tag("cms/#{file}" , class: "h-56 w-full object-contain lg:h-72")
.p-6
%h3.mt-4.text-lg.font-bold= section.template #PREVIEW here
%button.mt-4.block.w-full.rounded-sm.bg-yellow-500.p-4.text-sm.font-medium{:type => "button"}
=index #up down delete actions here
-section.content.each do |key , value|
.p-6
%h3.mt-4.text-lg.font-bold= key.upcase
%p= value
= link_to 'Edit', edit_cms_page_path(@page.name)
\|
= link_to 'Back', cms_pages_path

View File

@ -1,6 +0,0 @@
%p#notice= notice
= link_to 'Edit', edit_cms_page_path(@cms_page.name)
\|
= link_to 'Back', cms_pages_path