Add page layout support

This commit is contained in:
Wen-Tien Chang 2009-05-08 01:54:33 +08:00
parent 78e8c32969
commit 8dcde174cf
10 changed files with 179 additions and 1 deletions

View File

@ -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

View File

@ -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

8
app/models/layout.rb Normal file
View File

@ -0,0 +1,8 @@
class Layout < CouchFoo::Base
property :name, String
property :content, String
validates_presence_of :name
end

View File

@ -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

View File

@ -0,0 +1,21 @@
<h1>Editing layouts</h1>
<% form_for @layout, :url => admin_layout_path(@layout) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Back', admin_layouts_path %>

View File

@ -0,0 +1,18 @@
<h1>Listing layouts</h1>
<table>
<tr>
</tr>
<% @layouts.each do |layout| %>
<tr>
<td><%= layout.name %></td>
<td><%= link_to 'Edit', edit_admin_layout_path(layout) %></td>
<td><%= link_to 'Destroy', admin_layout_path(layout), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New layouts', new_admin_layout_path %>

View File

@ -0,0 +1,21 @@
<h1>New layouts</h1>
<% form_for :layout, :url => admin_layouts_path do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', admin_layouts_path %>

View File

@ -8,6 +8,11 @@
<%= f.text_field :name %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>

View File

@ -8,6 +8,11 @@
<%= f.text_field :name %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name %>
</p>
<p>
<%= f.label :content, "Content" %>
<%= f.text_area :content, :size => '100x30' %>

View File

@ -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.