From 8dcde174cf22cfb45e7443b98d0e9b12eb7f9fd3 Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 8 May 2009 01:54:33 +0800 Subject: [PATCH] Add page layout support --- app/controllers/admin/layouts_controller.rb | 82 +++++++++++++++++++++ app/controllers/pages_controller.rb | 5 +- app/models/layout.rb | 8 ++ app/models/page.rb | 14 ++++ app/views/admin/layouts/edit.html.erb | 21 ++++++ app/views/admin/layouts/index.html.erb | 18 +++++ app/views/admin/layouts/new.html.erb | 21 ++++++ app/views/admin/pages/edit.html.erb | 5 ++ app/views/admin/pages/new.html.erb | 5 ++ config/routes.rb | 1 + 10 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 app/controllers/admin/layouts_controller.rb create mode 100644 app/models/layout.rb create mode 100644 app/views/admin/layouts/edit.html.erb create mode 100644 app/views/admin/layouts/index.html.erb create mode 100644 app/views/admin/layouts/new.html.erb diff --git a/app/controllers/admin/layouts_controller.rb b/app/controllers/admin/layouts_controller.rb new file mode 100644 index 000000000..987fc4e30 --- /dev/null +++ b/app/controllers/admin/layouts_controller.rb @@ -0,0 +1,82 @@ +class Admin::LayoutsController < ApplicationController + # GET /layouts + # GET /layouts.xml + def index + @layouts = Layout.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @layouts } + end + end + + # GET /layouts/1 + # GET /layouts/1.xml + def show + @layout = Layout.find(params[:id]) + + redirect_to "/#{@layout.name}" + end + + # GET /layouts/new + # GET /layouts/new.xml + def new + @layout = Layout.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @layouts } + end + end + + # GET /layouts/1/edit + def edit + @layout = Layout.find(params[:id]) + end + + # POST /layouts + # POST /layouts.xml + def create + @layout = Layout.new(params[:layout]) + + respond_to do |format| + if @layout.save + flash[:notice] = 'Layout was successfully created.' + format.html { redirect_to admin_layouts_url } + format.xml { render :xml => @layout, :status => :created, :location => @layouts } + else + format.html { render :action => "new" } + format.xml { render :xml => @layout.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /layouts/1 + # PUT /layouts/1.xml + def update + @layout = Layout.find(params[:id]) + + respond_to do |format| + if @layout.update_attributes(params[:layout]) + flash[:notice] = 'Layout was successfully updated.' + format.html { redirect_to admin_layouts_url } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @layout.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /layouts/1 + # DELETE /layouts/1.xml + def destroy + @layout = Layout.find(params[:id]) + @layout.destroy + + respond_to do |format| + format.html { redirect_to admin_layouts_url } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 53d499ee8..159d2e6c7 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -2,9 +2,12 @@ class PagesController < ApplicationController def show @page = Page.find_by_name(params[:page_name]) + @layout = @page.layout if @page - render :text => Liquid::Template.parse(@page.content).render + @page_content = Liquid::Template.parse(@page.content).render + @layout_content = (@page.layout)? @layout.content : "{{page_content}}" + render :text => Liquid::Template.parse(@layout_content).render( 'page_content' => @page_content ) else render :text => '404 not found' end diff --git a/app/models/layout.rb b/app/models/layout.rb new file mode 100644 index 000000000..cc1ea5341 --- /dev/null +++ b/app/models/layout.rb @@ -0,0 +1,8 @@ +class Layout < CouchFoo::Base + + property :name, String + property :content, String + + validates_presence_of :name + +end \ No newline at end of file diff --git a/app/models/page.rb b/app/models/page.rb index 56caa2d0b..a34097fb4 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -3,7 +3,21 @@ class Page < CouchFoo::Base property :name, String property :parent_name, String property :content, String + property :layout_id, String + property :layout_name, String + + belongs_to :layout validates_presence_of :name + before_save :setup_layout_id + + def setup_layout_id + if self.layout_name.blank? + self.layout_id = nil + else + self.layout_id = Layout.find_by_name( self.layout_name ).id + end + end + end \ No newline at end of file diff --git a/app/views/admin/layouts/edit.html.erb b/app/views/admin/layouts/edit.html.erb new file mode 100644 index 000000000..41ae18749 --- /dev/null +++ b/app/views/admin/layouts/edit.html.erb @@ -0,0 +1,21 @@ +

Editing layouts

+ +<% form_for @layout, :url => admin_layout_path(@layout) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name, "Name" %> + <%= f.text_field :name %> +

+ +

+ <%= f.label :content, "Content" %> + <%= f.text_area :content, :size => '100x30' %> +

+ +

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Back', admin_layouts_path %> \ No newline at end of file diff --git a/app/views/admin/layouts/index.html.erb b/app/views/admin/layouts/index.html.erb new file mode 100644 index 000000000..508ba0881 --- /dev/null +++ b/app/views/admin/layouts/index.html.erb @@ -0,0 +1,18 @@ +

Listing layouts

+ + + + + +<% @layouts.each do |layout| %> + + + + + +<% end %> +
<%= layout.name %><%= link_to 'Edit', edit_admin_layout_path(layout) %><%= link_to 'Destroy', admin_layout_path(layout), :confirm => 'Are you sure?', :method => :delete %>
+ +
+ +<%= link_to 'New layouts', new_admin_layout_path %> \ No newline at end of file diff --git a/app/views/admin/layouts/new.html.erb b/app/views/admin/layouts/new.html.erb new file mode 100644 index 000000000..f91912242 --- /dev/null +++ b/app/views/admin/layouts/new.html.erb @@ -0,0 +1,21 @@ +

New layouts

+ +<% form_for :layout, :url => admin_layouts_path do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name, "Name" %> + <%= f.text_field :name %> +

+ +

+ <%= f.label :content, "Content" %> + <%= f.text_area :content, :size => '100x30' %> +

+ +

+ <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', admin_layouts_path %> \ No newline at end of file diff --git a/app/views/admin/pages/edit.html.erb b/app/views/admin/pages/edit.html.erb index b9eb861b1..77559c8cc 100644 --- a/app/views/admin/pages/edit.html.erb +++ b/app/views/admin/pages/edit.html.erb @@ -8,6 +8,11 @@ <%= f.text_field :name %>

+

+ <%= f.label :layout_name, "Layout Name" %> + <%= f.text_field :layout_name %> +

+

<%= f.label :content, "Content" %> <%= f.text_area :content, :size => '100x30' %> diff --git a/app/views/admin/pages/new.html.erb b/app/views/admin/pages/new.html.erb index a14824672..76412879e 100644 --- a/app/views/admin/pages/new.html.erb +++ b/app/views/admin/pages/new.html.erb @@ -8,6 +8,11 @@ <%= f.text_field :name %>

+

+ <%= f.label :layout_name, "Layout Name" %> + <%= f.text_field :layout_name %> +

+

<%= f.label :content, "Content" %> <%= f.text_area :content, :size => '100x30' %> diff --git a/config/routes.rb b/config/routes.rb index 7cdda3c3d..6e988cb0a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ ActionController::Routing::Routes.draw do |map| map.namespace :admin do |admin| admin.resources :pages + admin.resources :layouts end # The priority is based upon order of creation: first created -> highest priority.