Add announcement admin and view template

This commit is contained in:
Wen-Tien Chang 2009-06-01 14:20:15 +08:00
parent 4807066c68
commit 5570a2dfbf
12 changed files with 145 additions and 115 deletions

View File

@ -0,0 +1,66 @@
class Admin::AnnouncementsController < ApplicationController
layout "admin"
def index
@announcements = Announcement.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @announcements }
end
end
def new
@announcement = Announcement.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @announcement }
end
end
def edit
@announcement = Announcement.find(params[:id])
end
def create
@announcement = Announcement.new(params[:announcement])
respond_to do |format|
if @announcement.save
flash[:notice] = 'Announcement was successfully created.'
format.html { redirect_to admin_announcements_path }
format.xml { render :xml => @announcement, :status => :created, :location => @announcement }
else
format.html { render :action => "new" }
format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
end
end
end
def update
@announcement = Announcement.find(params[:id])
respond_to do |format|
if @announcement.update_attributes(params[:announcement])
flash[:notice] = 'Announcement was successfully updated.'
format.html { redirect_to admin_announcements_path }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@announcement = Announcement.find(params[:id])
@announcement.destroy
respond_to do |format|
format.html { redirect_to admin_announcements_path }
format.xml { head :ok }
end
end
end

View File

@ -1,85 +1,29 @@
class AnnouncementsController < ApplicationController
# GET /announcements
# GET /announcements.xml
def index
@announcements = Announcement.all
respond_to do |format|
format.html # index.html.erb
format.html {
@page = Page.find_by_name( 'announcement_index')
@page_options = { "announcements" => @announcements.map{ |a| a.to_liquid } }
render_liquid_page
}
format.xml { render :xml => @announcements }
end
end
# GET /announcements/1
# GET /announcements/1.xml
def show
@announcement = Announcement.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.html{
@page = Page.find_by_name( 'announcement_show')
@page_options = { 'announcement' => @announcement.to_liquid }
render_liquid_page
}
format.xml { render :xml => @announcement }
end
end
# GET /announcements/new
# GET /announcements/new.xml
def new
@announcement = Announcement.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @announcement }
end
end
# GET /announcements/1/edit
def edit
@announcement = Announcement.find(params[:id])
end
# POST /announcements
# POST /announcements.xml
def create
@announcement = Announcement.new(params[:announcement])
respond_to do |format|
if @announcement.save
flash[:notice] = 'Announcement was successfully created.'
format.html { redirect_to(@announcement) }
format.xml { render :xml => @announcement, :status => :created, :location => @announcement }
else
format.html { render :action => "new" }
format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
end
end
end
# PUT /announcements/1
# PUT /announcements/1.xml
def update
@announcement = Announcement.find(params[:id])
respond_to do |format|
if @announcement.update_attributes(params[:announcement])
flash[:notice] = 'Announcement was successfully updated.'
format.html { redirect_to(@announcement) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /announcements/1
# DELETE /announcements/1.xml
def destroy
@announcement = Announcement.find(params[:id])
@announcement.destroy
respond_to do |format|
format.html { redirect_to(announcements_url) }
format.xml { head :ok }
end
end
end

View File

@ -1,10 +1,22 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
# filter_parameter_logging :password
helper :all
protect_from_forgery
filter_parameter_logging :password
Liquid::Template.register_filter(SnippetFilter)
def render_liquid_page
if @page
@layout = @page.layout
@page_options ||= {}
@page_content = Liquid::Template.parse(@page.content).render(@page_options)
@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
end
end

View File

@ -1,22 +1,24 @@
class PagesController < ApplicationController
Liquid::Template.register_filter(SnippetFilter)
def index
@page = Page.find_by_name('home')
if @page
render_liquid_page
else
render :text => 'You need a home page'
end
end
def show
@page = Page.find(:first, :conditions => { :name => params[:page_name], :is_published => true })
@page = Page.find_by_name(params[:page_name])
if @page && !@page.external_link.blank?
redirect_to @page.external_link
elsif @page && @page.use_engine
#model_class = Kernel.const_get( "Announcement" ) # page.use_engine
redirect_to announcements_path
elsif @page
@layout = @page.layout
@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'
render_liquid_page
end
end

View File

@ -1,3 +1,4 @@
require 'couch_foo'
class Announcement < CouchFoo::Base
property :title, String
@ -5,12 +6,8 @@ class Announcement < CouchFoo::Base
validates_presence_of :title
def self.load
# hmm.... but one module will has many pages
end
def to_liquid
{ :title => self.title, :content => self.content }
{ "id" => self.id, "title" => self.title, "content" => self.content }
end
end

View File

@ -21,6 +21,12 @@ class Page < CouchFoo::Base
default_sort :position
def self.find_by_name(page_name)
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
end
protected
def setup_layout_id
if self.layout_name.blank?
self.layout_id = nil
@ -29,8 +35,6 @@ class Page < CouchFoo::Base
end
end
protected
def setup_default_value
if self.position.blank?
max_page = Page.last( :use_key => 'position')

View File

@ -1,6 +1,6 @@
<h1>Editing announcement</h1>
<% form_for(@announcement) do |f| %>
<% form_for @announcement, :url => admin_announcement_path(@announcement) do |f| %>
<%= f.error_messages %>
<p>
@ -19,4 +19,4 @@
<% end %>
<%= link_to 'Show', @announcement %> |
<%= link_to 'Back', announcements_path %>
<%= link_to 'Back', admin_announcements_path %>

View File

@ -0,0 +1,21 @@
<h1>Listing announcements</h1>
<table>
<tr>
<th>Title</th>
<th>Actions</th>
</tr>
<% @announcements.each do |announcement| %>
<tr>
<td><%=h announcement.title %></td>
<td><%= link_to 'Show', announcement_path(announcement) %> |
<%= link_to 'Edit', edit_admin_announcement_path(announcement) %> |
<%= link_to 'Destroy', admin_announcement_path(announcement), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New announcement', new_admin_announcement_path %>

View File

@ -1,6 +1,6 @@
<h1>New announcement</h1>
<% form_for(@announcement) do |f| %>
<% form_for @announcement, :url => admin_announcements_path do |f| %>
<%= f.error_messages %>
<p>
@ -18,4 +18,4 @@
</p>
<% end %>
<%= link_to 'Back', announcements_path %>
<%= link_to 'Back', admin_announcements_path %>

View File

@ -1,18 +0,0 @@
<h1>Listing announcements</h1>
<table>
<tr>
</tr>
<% @announcements.each do |announcement| %>
<tr>
<td><%= link_to 'Show', announcement %></td>
<td><%= link_to 'Edit', edit_announcement_path(announcement) %></td>
<td><%= link_to 'Destroy', announcement, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New announcement', new_announcement_path %>

View File

@ -15,9 +15,11 @@
<body>
<div class="container">
<div id ="nav" class="span-24">
<%= link_to 'Home', root_path %> |
<%= link_to 'Pages', admin_pages_path %> |
<%= link_to 'Snippet', admin_snippets_path %> |
<%= link_to 'Layout', admin_layouts_path %>
<%= link_to 'Layout', admin_layouts_path %> |
<%= link_to 'Announcement', admin_announcements_path %>
</div>
<%= yield %>

View File

@ -1,6 +1,4 @@
ActionController::Routing::Routes.draw do |map|
map.resources :announcements
map.resources :announcements
@ -8,6 +6,8 @@ ActionController::Routing::Routes.draw do |map|
admin.resources :pages
admin.resources :layouts
admin.resources :snippets
admin.resources :announcements
end
# The priority is based upon order of creation: first created -> highest priority.
@ -42,7 +42,7 @@ ActionController::Routing::Routes.draw do |map|
# end
# You can have the root of your site routed with map.root -- just remember to delete public/index.html.
# map.root :controller => "welcome"
map.root :controller => "pages"
# See how all your routes lay out with "rake routes"