diff --git a/README.md b/README.md index 23a14b2..513197f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/controllers/merged/cards_controller.rb b/app/controllers/merged/cards_controller.rb index 49bdced..e3131fe 100644 --- a/app/controllers/merged/cards_controller.rb +++ b/app/controllers/merged/cards_controller.rb @@ -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 diff --git a/app/controllers/merged/pages_controller.rb b/app/controllers/merged/pages_controller.rb index 172ee54..bcf9f9f 100644 --- a/app/controllers/merged/pages_controller.rb +++ b/app/controllers/merged/pages_controller.rb @@ -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. diff --git a/app/controllers/merged/sections_controller.rb b/app/controllers/merged/sections_controller.rb index 55009ae..381d676 100644 --- a/app/controllers/merged/sections_controller.rb +++ b/app/controllers/merged/sections_controller.rb @@ -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 diff --git a/app/helpers/merged/section_helper.rb b/app/helpers/merged/section_helper.rb deleted file mode 100644 index 8cd6cbc..0000000 --- a/app/helpers/merged/section_helper.rb +++ /dev/null @@ -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 diff --git a/app/helpers/merged/sections_helper.rb b/app/helpers/merged/sections_helper.rb new file mode 100644 index 0000000..bd946ea --- /dev/null +++ b/app/helpers/merged/sections_helper.rb @@ -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 diff --git a/app/models/merged/card.rb b/app/models/merged/card.rb index 9610ad1..2805776 100644 --- a/app/models/merged/card.rb +++ b/app/models/merged/card.rb @@ -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 diff --git a/app/models/merged/image.rb b/app/models/merged/image.rb index 7846ee6..494aa86 100644 --- a/app/models/merged/image.rb +++ b/app/models/merged/image.rb @@ -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 diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index 864e72b..e013148 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -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 diff --git a/app/models/merged/section.rb b/app/models/merged/section.rb index a3eb91b..5c8094c 100644 --- a/app/models/merged/section.rb +++ b/app/models/merged/section.rb @@ -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 diff --git a/app/views/merged/cards/editors/_card_template.haml b/app/views/merged/cards/editors/_card_template.haml index 2122cb6..a07df98 100644 --- a/app/views/merged/cards/editors/_card_template.haml +++ b/app/views/merged/cards/editors/_card_template.haml @@ -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") diff --git a/app/views/merged/cards/editors/_image.haml b/app/views/merged/cards/editors/_image.haml index e81fd09..6db5217 100644 --- a/app/views/merged/cards/editors/_image.haml +++ b/app/views/merged/cards/editors/_image.haml @@ -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 diff --git a/app/views/merged/cards/editors/_template.haml b/app/views/merged/cards/editors/_template.haml index 4169be3..38cc40f 100644 --- a/app/views/merged/cards/editors/_template.haml +++ b/app/views/merged/cards/editors/_template.haml @@ -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") diff --git a/app/views/merged/cards/index.haml b/app/views/merged/cards/index.haml index 35af970..496af79 100644 --- a/app/views/merged/cards/index.haml +++ b/app/views/merged/cards/index.haml @@ -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 diff --git a/app/views/merged/pages/_form.haml b/app/views/merged/pages/_form.haml deleted file mode 100644 index f804117..0000000 --- a/app/views/merged/pages/_form.haml +++ /dev/null @@ -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' diff --git a/app/views/merged/pages/edit.haml b/app/views/merged/pages/edit.haml deleted file mode 100644 index 6cf9333..0000000 --- a/app/views/merged/pages/edit.haml +++ /dev/null @@ -1,7 +0,0 @@ -%h1 Editing #{@merged_page.name} - -= render 'form' - -= link_to 'Show', page_url(@merged_page.name) -\| -= link_to 'Back', pages_path diff --git a/app/views/merged/pages/index.haml b/app/views/merged/pages/index.haml index 7476ccd..ce8f252 100644 --- a/app/views/merged/pages/index.haml +++ b/app/views/merged/pages/index.haml @@ -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) diff --git a/app/views/merged/pages/new.haml b/app/views/merged/pages/new.haml deleted file mode 100644 index 7b5cf07..0000000 --- a/app/views/merged/pages/new.haml +++ /dev/null @@ -1,5 +0,0 @@ -%h1 New merged_page - -= render 'form' - -= link_to 'Back', pages_path diff --git a/app/views/merged/pages/show.haml b/app/views/merged/pages/show.haml index 171d124..1c64672 100644 --- a/app/views/merged/pages/show.haml +++ b/app/views/merged/pages/show.haml @@ -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 diff --git a/app/views/merged/sections/editors/_card_template.haml b/app/views/merged/sections/editors/_card_template.haml index 2122cb6..d9886d8 100644 --- a/app/views/merged/sections/editors/_card_template.haml +++ b/app/views/merged/sections/editors/_card_template.haml @@ -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") diff --git a/app/views/merged/sections/editors/_image.haml b/app/views/merged/sections/editors/_image.haml index e81fd09..d51ba8a 100644 --- a/app/views/merged/sections/editors/_image.haml +++ b/app/views/merged/sections/editors/_image.haml @@ -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 diff --git a/app/views/merged/sections/editors/_template.haml b/app/views/merged/sections/editors/_template.haml index 4169be3..38cc40f 100644 --- a/app/views/merged/sections/editors/_template.haml +++ b/app/views/merged/sections/editors/_template.haml @@ -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") diff --git a/app/views/merged/sections/index.html.haml b/app/views/merged/sections/index.html.haml index 171d124..1c64672 100644 --- a/app/views/merged/sections/index.html.haml +++ b/app/views/merged/sections/index.html.haml @@ -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 diff --git a/app/views/merged/sections/select_card_template.haml b/app/views/merged/sections/select_card_template.haml index 3e5b60e..a8f8f01 100644 --- a/app/views/merged/sections/select_card_template.haml +++ b/app/views/merged/sections/select_card_template.haml @@ -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 diff --git a/app/views/merged/sections/select_image.haml b/app/views/merged/sections/select_image.haml index 8ceecf3..a066b29 100644 --- a/app/views/merged/sections/select_image.haml +++ b/app/views/merged/sections/select_image.haml @@ -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") diff --git a/app/views/merged/sections/select_template.haml b/app/views/merged/sections/select_template.haml index 2c44234..14be504 100644 --- a/app/views/merged/sections/select_template.haml +++ b/app/views/merged/sections/select_template.haml @@ -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 diff --git a/app/views/merged/sections/show.html.haml b/app/views/merged/sections/show.html.haml index e79df53..e6333f0 100644 --- a/app/views/merged/sections/show.html.haml +++ b/app/views/merged/sections/show.html.haml @@ -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} diff --git a/config/routes.rb b/config/routes.rb index a4eba73..323f8fc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/models/merged/card_spec.rb b/spec/models/merged/card_spec.rb index 22f49b2..9f99c0a 100644 --- a/spec/models/merged/card_spec.rb +++ b/spec/models/merged/card_spec.rb @@ -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 diff --git a/spec/models/merged/page_spec.rb b/spec/models/merged/page_spec.rb index c36ff8d..4f9c5ae 100644 --- a/spec/models/merged/page_spec.rb +++ b/spec/models/merged/page_spec.rb @@ -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 diff --git a/spec/models/merged/section_spec.rb b/spec/models/merged/section_spec.rb index dd2e1d8..1b95e7e 100644 --- a/spec/models/merged/section_spec.rb +++ b/spec/models/merged/section_spec.rb @@ -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