flattened routes, lots of paths and arg changes

This commit is contained in:
Torsten 2022-11-30 16:22:11 +02:00
parent 1d91ff2fc6
commit df5713e6fe
31 changed files with 121 additions and 104 deletions

View File

@ -92,8 +92,19 @@ $ bundle
```
Mount engine in routes for editing.
```ruby
mount Merged::Engine => "/merged"
```
Create route to serve content.
```ruby
get ":id" , to: "merged/view#view" , id: :id
```
If Merged served the root:
```ruby
root "merged/view#view" , id: 'index'
```
## Contributing
Ask first.

View File

@ -1,9 +1,9 @@
module Merged
class CardsController < MergedController
before_action :set_page
before_action :set_card , except: :index
def index
@section = Section.find(params[:section_id])
end
private

View File

@ -4,7 +4,7 @@ module Merged
# GET /merged/pages
def index
@pages = Merged::Page.all
@pages = Page.all.values
end
# GET /merged/pages/1
@ -13,7 +13,6 @@ module Merged
# GET /merged/pages/new
def new
@page = Merged::Page.new
end
# GET /merged/pages/1/edit
@ -49,7 +48,7 @@ module Merged
private
# Use callbacks to share common setup or constraints between actions.
def set_page
@page = Merged::Page.find(params[:id])
@page = Page.find(params[:id])
end
# Only allow a list of trusted parameters through.

View File

@ -1,8 +1,11 @@
module Merged
class SectionsController < MergedController
before_action :set_page
before_action :set_section , except: :index
#, 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
@ -15,24 +18,24 @@ module Merged
def set_image
@section.content["image"] = params[:image]
@page.save
redirect_to page_section_url(@page.id,@section.id)
@section.save
redirect_to section_url(@section.id)
end
def set_template
template = params[:template]
raise "no template given" if template.blank?
@section.content["template"] = template
@page.save
redirect_to page_section_url(@page.id,@section.id)
@section.save
redirect_to section_url(@section.id)
end
def set_card_template
card_template = params[:card_template]
raise "no card template given" if card_template.blank?
@section.content["card_template"] = card_template
@page.save
redirect_to page_section_url(@page.id,@section.id)
@section.save
redirect_to section_url(@section.id)
end
def update
@ -43,15 +46,13 @@ module Merged
puts "updating:#{key}=#{params[key]}"
end
end
@page.save
redirect_to :page_section
@section.save
redirect_to :section
end
private
def set_page
@page = Page.find(params[:page_id])
section_id = params[:id] || params[:section_id]
@section = @page.find_section( section_id )
def set_section
@section = Section.find( params[:id] || params[:section_id] )
end
end

View File

@ -1,10 +0,0 @@
module Merged::SectionHelper
def section_form(options)
url = page_section_url( @page.id , @section.id)
puts "URL #{url}"
form_tag( url , {method: :patch}) do
yield
end
end
end

View File

@ -0,0 +1,17 @@
module Merged
module SectionsHelper
def section_form(options)
url = section_url( @section.id)
puts "URL #{url}"
form_tag( url , {method: :patch}) do
yield
end
end
def image_root
Image.image_root
end
end
end

View File

@ -7,10 +7,11 @@ module Merged
cattr_reader :all
@@all = {}
attr_reader :content , :section
attr_reader :content , :index , :section
def initialize(section , card_data)
def initialize(section , index , card_data)
@section = section
@index = index
raise "No data #{card_data}" unless card_data.is_a?(Hash)
@content = card_data
raise "No id #{card_data}" unless id.is_a?(String)
@ -20,5 +21,16 @@ module Merged
def id
@content['id']
end
def save
section.save
end
def self.find(id)
raise "nil given" if id.blank?
card = @@all[id]
raise "Section not found #{id}" unless card
return card
end
end
end

View File

@ -35,7 +35,7 @@ module Merged
File.open(Rails.root.join(Image.asset_root, full_filename), "wb") do |f|
f.write( io.read )
end
self.add( full_filename )
Image.new( full_filename )
end
def self.asset_root

View File

@ -26,22 +26,14 @@ module Merged
def initialize( file_name )
@name = file_name.split(".").first
@content = YAML.load_file(Rails.root.join(Page.cms_root , file_name))
@sections = {}
@sections = []
@content.each_with_index do |section_data, index|
section = Section.new(self , index, section_data)
@sections[ section.id] = section
@sections << section
end
@@all[@name] = self
end
def find_section(section_id)
@content.each_with_index do |section , index|
next unless section["id"] == section_id
return Section.new(self , index , section)
end
raise "Page #{name} as no section #{section_id}"
end
def first_template
@content[0]["template"]
end
@ -59,7 +51,11 @@ module Merged
end
def self.find(name)
@@all[name]
raise "nil given" if name.blank?
page = @@all[name]
raise "Page not found #{name}" unless page
return page
end
end

View File

@ -16,12 +16,11 @@ module Merged
@index = index
@content = section_data
@@all[self.id] = self
@cards = {}
@cards = []
element = @content["cards"]
return if element.nil?
element.each do|card_content|
card = Card.new(self , card_content)
@cards[card.id] = card
element.each_with_index do|card_content , index|
@cards << Card.new(self , index , card_content)
end
end
@ -48,12 +47,14 @@ module Merged
end
def save
raise "Called"
page.save
end
def self.find(page_name , section_id)
raise "buggy"
Page.new(name + ".yaml")
def self.find(section_id)
raise "nil given" if section_id.blank?
section = @@all[section_id]
raise "Section not found #{section_id}" unless section
return section
end
end

View File

@ -4,8 +4,8 @@
%p #{section.content['cards'].length} cards
%p view cards (index)
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "View and Edit Cards" , page_section_card_url(@page.name,@section.id)
=link_to "View and Edit Cards" , section_card_url(@page.name,@section.id)
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Card Template" , page_section_select_card_template_url(@page.name,@section.id)
=link_to "Change Card Template" , section_select_card_template_url(@page.name,@section.id)
.relative.block.border.border-gray-100
=image_tag("merged/card_preview/#{value}" , class: "w-full object-contain")

View File

@ -1,9 +1,9 @@
.relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold= key.upcase
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Image" , page_section_select_image_url(@page.name,@section.id)
=link_to "Change Image" , section_select_image_url(@page.name,@section.id)
%button.ml-3.inline-block.rounded-lg.bg-red-500.px-5.py-3.text-md.font-medium.text-white
= link_to( "Remove image" , page_section_set_image_path( @page.name, @section.id , image: ""))
= link_to( "Remove image" , section_set_image_path( @page.name, @section.id , image: ""))
.relative.block.border.border-gray-100

View File

@ -1,6 +1,6 @@
.relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold= key.upcase + " : " + value
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Template" , page_section_select_template_url(@page.name,@section.id)
=link_to "Change Template" , section_select_template_url(@section.id)
.relative.block.border.border-gray-100
=image_tag("merged/section_preview/#{section.template}" , class: "w-full object-contain")

View File

@ -4,10 +4,10 @@
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900
Page #{@page.name}
Page #{@section.page.name}
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Cards for Section #{@section.id}
Cards for Section #{@section.index + 1}
.grid.grid-cols-4.gap-2.m-8
- @section.cards.each_with_index do |card , index|
@ -19,7 +19,7 @@
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
%button.mt-4.rounded-lg.bg-blue-400.p-4
=link_to "Edit" , page_section_path(@page.name , @section.id)
=link_to "Edit" , card_path( @section.id)
%button.mt-4.rounded-lg.bg-cyan-400.p-4
=link_to "New" , "/index"
%button.mt-4.rounded-lg.bg-red-400.p-4

View File

@ -1,10 +0,0 @@
= form_for @merged_page do |f|
- if @merged_page.errors.any?
#error_explanation
%h2= "#{pluralize(@merged_page.errors.count, "error")} prohibited this merged_page from being saved:"
%ul
- @merged_page.errors.full_messages.each do |message|
%li= message
.actions
= f.submit 'Save'

View File

@ -1,7 +0,0 @@
%h1 Editing #{@merged_page.name}
= render 'form'
= link_to 'Show', page_url(@merged_page.name)
\|
= link_to 'Back', pages_path

View File

@ -17,14 +17,14 @@
- @pages.each do |merged_page|
%tr
%td.whitespace-nowrap.px-4.py-2.text-gray-700
= link_to merged_page.name , page_path(merged_page.name)
= link_to merged_page.name , page_sections_path(merged_page.name)
%td.whitespace-nowrap.px-4.py-2.text-gray-700
= merged_page.first_template
%td.whitespace-nowrap.px-4.py-2.text-gray-700
= merged_page.content.length
%td.whitespace-nowrap.px-4.py-2
%strong.rounded.bg-green-100.px-3.text-xs.font-medium.text-green-700{:class => "py-1.5"}
= link_to 'Show', page_path(merged_page.name)
= link_to 'Sections', page_sections_path(merged_page.name)
%strong.rounded.bg-amber-100.px-3.text-xs.font-medium.text-amber-700{:class => "py-1.5"}
= link_to 'Edit', edit_page_path(merged_page.name)

View File

@ -1,5 +0,0 @@
%h1 New merged_page
= render 'form'
= link_to 'Back', pages_path

View File

@ -5,17 +5,17 @@
%h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl
Page #{@page.name}
-@page.sections.each_with_index do |section , index|
.grid.grid-cols-6.gap-2.m-8{class: (index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
-@page.sections.each do |section |
.grid.grid-cols-6.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
.relative.block.border.border-gray-100
.p-4
%h3.mt-4.text-lg.font-bold Section #{index + 1}
%h3.mt-4.text-lg.font-bold Section #{section.index + 1}
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to( "Up" , "/index")
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
%button.mt-4.rounded-lg.bg-blue-400.p-4
=link_to "Edit" , page_section_path(@page.name , section.id)
=link_to "Edit" , section_path(section.id)
%button.mt-4.rounded-lg.bg-cyan-400.p-4
=link_to "New" , "/index"
%button.mt-4.rounded-lg.bg-red-400.p-4

View File

@ -4,8 +4,8 @@
%p #{section.content['cards'].length} cards
%p view cards (index)
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "View and Edit Cards" , page_section_card_url(@page.name,@section.id)
=link_to "View and Edit Cards" , section_cards_url(@section.id)
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Card Template" , page_section_select_card_template_url(@page.name,@section.id)
=link_to "Change Card Template" , section_select_card_template_url(@section.id)
.relative.block.border.border-gray-100
=image_tag("merged/card_preview/#{value}" , class: "w-full object-contain")

View File

@ -1,9 +1,9 @@
.relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold= key.upcase
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Image" , page_section_select_image_url(@page.name,@section.id)
=link_to "Change Image" , section_select_image_url(@section.id)
%button.ml-3.inline-block.rounded-lg.bg-red-500.px-5.py-3.text-md.font-medium.text-white
= link_to( "Remove image" , page_section_set_image_path( @page.name, @section.id , image: ""))
= link_to( "Remove image" , section_set_image_path( @section.id , image: ""))
.relative.block.border.border-gray-100

View File

@ -1,6 +1,6 @@
.relative.block.border.border-gray-100
%h3.mt-4.text-lg.font-bold= key.upcase + " : " + value
%button.ml-3.inline-block.rounded-lg.bg-blue-500.px-5.py-3.text-md.font-medium.text-white
=link_to "Change Template" , page_section_select_template_url(@page.name,@section.id)
=link_to "Change Template" , section_select_template_url(@section.id)
.relative.block.border.border-gray-100
=image_tag("merged/section_preview/#{section.template}" , class: "w-full object-contain")

View File

@ -5,17 +5,17 @@
%h1.text-2xl.font-bold.tracking-tight.text-gray-900.sm:text-4xl
Page #{@page.name}
-@page.sections.each_with_index do |section , index|
.grid.grid-cols-6.gap-2.m-8{class: (index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
-@page.sections.each do |section |
.grid.grid-cols-6.gap-2.m-8{class: (section.index%2)==1 ? 'bg-cyan-50' : 'bg-red-50' }
.relative.block.border.border-gray-100
.p-4
%h3.mt-4.text-lg.font-bold Section #{index + 1}
%h3.mt-4.text-lg.font-bold Section #{section.index + 1}
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to( "Up" , "/index")
%button.mt-4.rounded-lg.bg-yellow-500.p-4
=link_to "Down" , "/index"
%button.mt-4.rounded-lg.bg-blue-400.p-4
=link_to "Edit" , page_section_path(@page.name , section.id)
=link_to "Edit" , section_path(section.id)
%button.mt-4.rounded-lg.bg-cyan-400.p-4
=link_to "New" , "/index"
%button.mt-4.rounded-lg.bg-red-400.p-4

View File

@ -1,6 +1,6 @@
.grid.grid-cols-4.gap-2.m-8
- @cards.each do |style|
.relative.block.border.border-gray-100
= link_to( page_section_set_card_template_path( card_template: style.template )) do
= link_to( section_set_card_template_path( card_template: style.template )) do
=image_tag(style.card_preview , class: "h-56 w-full object-contain lg:h-72")
= style.header

View File

@ -5,11 +5,11 @@
= text_field_tag 'filename'
%h5.mt-4.text-lg.font-bold Name is optional
%p will be taken from uploaded file
= hidden_field_tag :redirect , page_section_set_image_url(@page.name,@section.id,image: "NEW")
= hidden_field_tag :redirect , section_set_image_url(@section.id,image: "NEW")
= file_field_tag 'image_file'
.inline-block.rounded.border.border-indigo-600.bg-indigo-600.px-12.py-3.text-sm.font-medium.text-white.hover:bg-transparent.hover:text-indigo-600.focus:outline-none.focus:ring.active:text-indigo-500{:href => merged.new_image_path}
= submit_tag 'Submit'
-@images.each do |name , image|
.relative.block.border.border-gray-100
= link_to( page_section_set_image_path( image: name)) do
=image_tag("cms/#{name}" , class: "h-56 w-full object-contain lg:h-72")
= link_to( section_set_image_path( image: name)) do
=image_tag("#{image_root}/#{name}" , class: "h-56 w-full object-contain lg:h-72")

View File

@ -1,6 +1,6 @@
.grid.grid-cols-4.gap-2.m-8
- @sections.each do |style|
.relative.block.border.border-gray-100
= link_to( page_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")
= style.header

View File

@ -4,7 +4,7 @@
.flex.items-center.justify-center.flex-1
.max-w-xl.px-4.py-8.mx-auto.text-center
%h1.text-2xl.font-bold.tracking-tight.text-gray-900
Page #{@page.name}
Page #{@section.page.name}
.flex.items-center.justify-center.flex-1
%h3.text-xl.font-bold.tracking-tight.text-gray-900
Section #{@section.id}

View File

@ -1,7 +1,7 @@
Merged::Engine.routes.draw do
get "/styles/index" , to: "styles#index"
resources :pages do
resources :pages , shallow: true do
resources :sections do
get :select_image
get :set_image

View File

@ -10,5 +10,8 @@ module Merged
it "has cards" do
expect(first.class).to be Card
end
it "has index" do
expect(first.index).to eq 0
end
end
end

View File

@ -13,5 +13,8 @@ module Merged
it "has sections" do
expect(index.sections.length).to be 1
end
it "has section array" do
expect(index.sections.class).to be Array
end
end
end

View File

@ -10,8 +10,14 @@ module Merged
it "has index page" do
expect(first.class).to be Section
end
it "has sections" do
expect(first.cards.length).to be 2
it "has index" do
expect(first.index).to eq 0
end
it "has cards" do
expect(first.cards.length).to eq 2
end
it "has cards array" do
expect(first.cards.class).to be Array
end
end
end