diff --git a/app/controllers/merged/pages_controller.rb b/app/controllers/merged/pages_controller.rb index bcf9f9f..d8d8061 100644 --- a/app/controllers/merged/pages_controller.rb +++ b/app/controllers/merged/pages_controller.rb @@ -11,22 +11,21 @@ module Merged def show end - # GET /merged/pages/new - def new - end - # GET /merged/pages/1/edit def edit end # POST /merged/pages def create - @page = Merged::Page.new(page_params) - - if @page.save - redirect_to @page, notice: "Page was successfully created." + name = params[:name] + message = Page.check_name(name) + if( message.nil?) + @page = Page.build_new(name) + redirect_to new_page_section_url(@page.name) , notice: "Page was successfully created." else - render :new, status: :unprocessable_entity + @pages = Page.all.values + flash.now.alert = message + render :index end end @@ -41,8 +40,8 @@ module Merged # DELETE /merged/pages/1 def destroy - @page.destroy - redirect_to page_url, notice: "Page was successfully destroyed." + Page.destroy(@page) + redirect_to pages_url, notice: "Page #{@page.name} was removed." end private diff --git a/app/models/merged/page.rb b/app/models/merged/page.rb index b1042a4..5adcb0c 100644 --- a/app/models/merged/page.rb +++ b/app/models/merged/page.rb @@ -34,6 +34,23 @@ module Merged @@all[@name] = self end + def self.check_name(name) + return "only alphanumeric, not #{name}" if name.match(/\A[a-zA-Z0-9]*\z/).nil? + nil + end + def self.build_new(name) + raise "only alphanumeric, not #{name}" unless check_name(name).nil? + name = name + ".yaml" + fullname = Rails.root.join(Page.cms_root , name ) + File.write(fullname , "--- []\n") + Page.new(name) + end + + def self.destroy( page ) + @@all.delete(page.name) + File.delete(Rails.root.join(Page.cms_root , page.name + ".yaml")) + end + def new_section(section_template) section_template = "section_spacer" if section_template.blank? section_data = Section.build_data(section_template) @@ -54,6 +71,7 @@ module Merged end def first_template + return "none" unless @content[0] @content[0]["template"] end diff --git a/app/views/merged/pages/index.haml b/app/views/merged/pages/index.haml index ce8f252..9c39aa8 100644 --- a/app/views/merged/pages/index.haml +++ b/app/views/merged/pages/index.haml @@ -28,6 +28,10 @@ %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) -%section - %a.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 => new_page_path} - New Page +.grid.grid-cols-3.gap-2.m-8 + .relative.block.border.border-gray-100 + = form_tag( pages_url , {method: :post } ) do + %label.block + %h4.text-lg.font-bold Name + = text_field_tag( :name , params[:name], class: "block w-full rounded-lg border-gray-200 p-4 pr-12 text-sm shadow-sm") + =submit_button( "New Page") diff --git a/config/routes.rb b/config/routes.rb index 6bf6eb9..87ebec7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,7 @@ Merged::Engine.routes.draw do post 'changes/commit' get "styles/index" - resources :pages , except: [:show] , shallow: true do + resources :pages , except: [:show , :new] , shallow: true do resources :sections do get :select_image get :set_image