Add Item, Page, Component, Link inheritanced models

This commit is contained in:
Wen-Tien Chang 2010-01-14 18:30:53 +08:00
parent cd21cadfe7
commit e1440a79d8
38 changed files with 417 additions and 175 deletions

View File

@ -0,0 +1,53 @@
class Admin::ComponentsController < ApplicationController
layout "admin"
before_filter :find_parent_item
def show
#TODO
end
def new
@component = Component.new
@component.is_published = true
@component.parent_item_name = @parent_item.name
@component.engine_name = 'Announcement' # only support Announcement now
end
def edit
@component = Component.find(params[:id])
end
def create
@component = Component.new(params[:component])
if @component.save
flash[:notice] = 'Component was successfully created.'
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
else
render :action => "new"
end
end
def update
@component = Component.find(params[:id])
if @component.update_attributes(params[:component])
flash[:notice] = 'Component was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
else
render :action => "edit"
end
end
def destroy
@component = Component.find(params[:id])
@component.destroy
#TODO: destroy engine data
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
end
end

View File

@ -0,0 +1,17 @@
class Admin::ItemsController < ApplicationController
layout "admin"
before_filter :find_parent_item
before_filter :find_snippets, :only => :index
def index
@items = Item.all( :conditions => { :parent_item_name => @parent_item.name } )
end
protected
def find_snippets
@snippets = Snippet.all( :conditions => { :parent_item_name => @parent_item.name } )
end
end

View File

@ -0,0 +1,49 @@
class Admin::LinksController < ApplicationController
layout "admin"
before_filter :find_parent_item
def show
#TODO
end
def new
@link = Link.new
@link.is_published = true
@link.parent_item_name = @parent_item.name
end
def edit
@link = Link.find(params[:id])
end
def create
@link = Link.new(params[:link])
if @link.save
flash[:notice] = 'Link was successfully created.'
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
else
render :action => "new"
end
end
def update
@link = Link.find(params[:id])
if @link.update_attributes(params[:link])
flash[:notice] = 'Link was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
else
render :action => "edit"
end
end
def destroy
@link = Link.find(params[:id])
@link.destroy
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
end
end

View File

@ -1,21 +1,16 @@
class Admin::PagesController < ApplicationController
layout "admin"
def index
@pages = Page.all( :conditions => { :parent_page_name => params[:parent] || "root" } )
end
before_filter :find_parent_item
def show
@page = Page.find(params[:id])
redirect_to "/#{@page.name}"
#TODO
end
def new
@page = Page.new
@page.is_published = true
@page.parent_page_name = params[:parent_page_name]
@page.parent_item_name = @parent_item.name
end
def edit
@ -27,7 +22,7 @@ class Admin::PagesController < ApplicationController
if @page.save
flash[:notice] = 'Page was successfully created.'
redirect_to admin_pages_url( :parent_page_name => @page.parent_page_name )
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
else
render :action => "new"
end
@ -38,7 +33,7 @@ class Admin::PagesController < ApplicationController
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
redirect_to admin_pages_url
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
else
render :action => "edit"
end
@ -48,7 +43,7 @@ class Admin::PagesController < ApplicationController
@page = Page.find(params[:id])
@page.destroy
redirect_to admin_pages_url
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
end
end

View File

@ -1,36 +1,15 @@
class Admin::SnippetsController < ApplicationController
layout "admin"
def index
@snippets = Snippet.all
end
before_filter :find_parent_item
def show
@snippet = Snippet.find(params[:id])
redirect_to "/#{@snippet.name}"
#TODO
end
def new
@snippet = Snippet.new
if params[:menu]
@snippet.name = "#{params[:menu]}_nav"
lis = Page.find(:all, :conditions => { :parent_page_id => params[:menu], :is_published => true }, :order => "position" ).map do |page|
if page.external_link.blank?
"<li><a href='/#{page.name}'>#{page.name}</a></li>\n"
else
"<li><a href='#{page.external_link}'>#{page.name}</a></li>\n"
end
end
VALID_LOCALES.each do |locale|
@snippet.send( :write_attribute, "content_#{locale}", "<ul>\n#{lis}</ul>" )
end
end
@snippet.parent_item_name = @parent_item.name
end
def edit
@ -42,7 +21,7 @@ class Admin::SnippetsController < ApplicationController
if @snippet.save
flash[:notice] = 'Snippet was successfully created.'
redirect_to admin_snippets_url
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
else
render :action => "new"
end
@ -53,7 +32,7 @@ class Admin::SnippetsController < ApplicationController
if @snippet.update_attributes(params[:snippet])
flash[:notice] = 'Snippet was successfully updated.'
redirect_to admin_snippets_url
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
else
render :action => "edit"
end
@ -63,7 +42,7 @@ class Admin::SnippetsController < ApplicationController
@snippet = Snippet.find(params[:id])
@snippet.destroy
redirect_to admin_snippets_url
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
end
end

View File

@ -31,6 +31,13 @@ class ApplicationController < ActionController::Base
I18n.locale = session[:locale] || I18n.default_locale
end
def find_parent_item
@parent_item = Item.find_by_name(params[:parent_item_name] || 'root')
unless @parent_item
@parent_item = Page.create( :name => 'root', :title => t(:homepage), :layout_name => "root" )
end
end
def require_entry_name
render :text => 'missing entry_name' if params[:entry_name].blank?
return

View File

@ -2,12 +2,12 @@ class Announcement
include MongoMapper::Document
key :entry_name, String, :required => true, :index => true
key :component_name, String, :required => true, :index => true
key_i18n :title, String
key_i18n :content, String
def to_liquid
{ "entry_name" => self.entry_name, "id" => self.id.to_s, "title" => self.title, "content" => self.content }
{ "component_name" => self.entry_name, "id" => self.id.to_s, "title" => self.title, "content" => self.content }
end
end

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

@ -0,0 +1,8 @@
class Component < Item
include LayoutSupport
key :engine_name, String
key :layout_name, String, :required => true
end

34
app/models/item.rb Normal file
View File

@ -0,0 +1,34 @@
class Item
include MongoMapper::Document
key :_type, String
key :name, String, :required => true, :index => true
key :parent_item_name, String, :required => true, :index => true
key_i18n :title, String, :required => true
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
before_validation :setup_default_value
def self.find_by_name(item_name)
Item.find(:first, :conditions => { :name => item_name, :is_published => true })
end
protected
def setup_default_value
if self.position.blank?
max_page = Page.find(:last, :order => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
if self.parent_item_name.blank?
self.parent_item_name = "root"
end
end
end

9
app/models/link.rb Normal file
View File

@ -0,0 +1,9 @@
class Link < Item
key :url, String, :required => true
def link
ApplicationController.helpers.link_to(self.title, self.url)
end
end

View File

@ -1,40 +1,8 @@
class Page
include MongoMapper::Document
class Page < Item
key :name, String, :required => true, :index => true
key :parent_page_name, String, :required => true, :index => true
include LayoutSupport
key_i18n :title, String, :required => true
key_i18n :content, String
key :layout_name, String, :required => true
key :external_link, String
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
key :component_name, String
before_validation :setup_default_value
def self.find_by_name(page_name)
Page.find(:first, :conditions => { :name => page_name, :is_published => true })
end
def layout
Layout.find_by_name(self.layout_name)
end
protected
def setup_default_value
if self.position.blank?
max_page = Page.find(:last, :order => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
if self.parent_page_name.blank?
self.parent_page_name = "root"
end
end
end

View File

@ -3,6 +3,8 @@ class Snippet
include MongoMapper::Document
key :name, String, :required => true, :index => true
key :parent_item_name, String, :required => true, :index => true
key_i18n :content, String
end

View File

@ -0,0 +1,32 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
</p>
<p>
<%= f.label :title, "Title en" %>
<%= f.text_field :title_en, :class => 'text' %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name, :class => 'text' %>
</p>
<p>
<%= f.label :title, "Title zh_tw" %>
<%= f.text_field :title_zh_tw, :class => 'text' %>
</p>
<p>
<%= f.label :is_published, "Is Published" %>
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
</p>
<p>
<%= f.label :engine_name, "Choose Engine" %>
<%= f.text_field :engine_name %>
</p>

View File

@ -0,0 +1,10 @@
<h1>Editing pages</h1>
<% form_for @component, :url => admin_component_path(@component) do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>

View File

@ -0,0 +1,11 @@
<h1><%= t(:new_component, :scope => :admin) %></h1>
<% form_for @component, :url => admin_components_path do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>

View File

@ -0,0 +1,12 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>
<td>
<%= link_to t(:show), admin_component_path(item.name) %> |
<%= link_to t(:edit), edit_admin_component_path(item) %> |
<%= link_to t(:delete), admin_component_path(item), :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>

View File

@ -0,0 +1,12 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>
<td>
<%= link_to t(:show), admin_link_path(item.name) %> |
<%= link_to t(:edit), edit_admin_link_path(item) %> |
<%= link_to t(:delete), admin_link_path(item), :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>

View File

@ -0,0 +1,12 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>
<td>
<%= link_to t(:show), admin_page_path(item.name) %> |
<%= link_to t(:edit), edit_admin_page_path(item) %> |
<%= link_to t(:delete), admin_page_path(item), :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>

View File

@ -0,0 +1,15 @@
<h1>Listing snippets</h1>
<table>
<tr>
</tr>
<% snippets.each do |snippet| %>
<tr>
<td><%= snippet.name %></td>
<td><%= link_to t(:show), admin_snippet_path(snippet) %>
<td><%= link_to t(:edit), edit_admin_snippet_path(snippet) %></td>
<td><%= link_to t(:delete), admin_snippet_path(snippet), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

View File

@ -0,0 +1,28 @@
<% content_for :secondary do %>
<ul class="list">
<li><%= link_to t(:new_page, :scope => :admin), new_admin_page_path( :parent_item_name => @parent_item.name ), :class => 'button positive' %></li>
<li><%= link_to t(:new_component, :scope => :admin), new_admin_component_path( :parent_item_name => @parent_item.name ) %>
<li><%= link_to t(:new_link, :scope => :admin), new_admin_link_path( :parent_item_name => @parent_item.name ) %>
<li><%= link_to t(:new_snippet, :scope => :admin ), new_admin_snippet_path( :parent_item_name => @parent_item.name ), :class => 'button positive' %></li>
</ul>
<% end -%>
<h1>Listing items: <%= @parent_item.name %>/</h1>
<table>
<tr>
<th>Class</th>
<th>Name</th>
<th>Title</th>
<th>Position</th>
<th>Published?</th>
<th>Action</th>
</tr>
<% @items.each do |item| %>
<%= render :partial => item.class.to_s.downcase, :locals => { :item => item } %>
<% end %>
</table>
<%= render :partial => "snippets", :locals => { :snippets => @snippets } %>

View File

@ -7,12 +7,12 @@
<% @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>
<td><%= link_to t(:edit), edit_admin_layout_path(layout) %></td>
<td><%= link_to t(:delete), admin_layout_path(layout), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New layouts', new_admin_layout_path, :class => 'button positive' %>
<%= link_to t(:new_layout, :scope => :admin), new_admin_layout_path, :class => 'button positive' %>

View File

@ -0,0 +1,28 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
</p>
<p>
<%= f.label :title, "Title en" %>
<%= f.text_field :title_en, :class => 'text' %>
</p>
<p>
<%= f.label :title, "Title zh_tw" %>
<%= f.text_field :title_zh_tw, :class => 'text' %>
</p>
<p>
<%= f.label :is_published, "Is Published" %>
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
</p>
<p>
<%= f.label :url, "URL" %>
<%= f.text_field :url %>
</p>

View File

@ -0,0 +1,10 @@
<h1>Editing pages</h1>
<% form_for @link, :url => admin_link_path(@link) do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>

View File

@ -0,0 +1,11 @@
<h1><%= t(:new_link, :scope => :admin) %></h1>
<% form_for @link, :url => admin_links_path do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>

View File

@ -1,3 +1,6 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>
@ -14,11 +17,6 @@
<%= f.text_field :title_zh_tw, :class => 'text' %>
</p>
<p>
<%= f.label :parent_page_name, "Parent Page Name" %>
<%= f.text_field :parent_page_name, :class => 'text' %>
</p>
<p>
<%= f.label :layout_name, "Layout Name" %>
<%= f.text_field :layout_name, :class => 'text' %>
@ -41,16 +39,6 @@
<%= f.radio_button :is_published, true %>Yes <%= f.radio_button :is_published, false %> No
</p>
<p>
<%= f.label :component_name, "Component Name" %>
<%= f.text_field :component_name %>
</p>
<p>
<%= f.label :external_link, "External Link" %>
<%= f.text_field :external_link %>
</p>
<% content_for :page_specific_javascript do %>
<script type="text/javascript" charset="utf-8">
$('#content_en_block').hide();

View File

@ -1,7 +1,6 @@
<h1>Editing pages</h1>
<% form_for @page, :url => admin_page_path(@page) do |f| %>
<%= f.error_messages %>
<%= render :partial => "form", :locals => { :f => f } %>
@ -9,5 +8,3 @@
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Back', admin_pages_path %>

View File

@ -1,38 +0,0 @@
<% content_for :secondary do %>
<ul class="list">
<li><%= link_to 'New pages', new_admin_page_path, :class => 'button positive' %></li>
<li><%= link_to 'New component entry', '#' %>
<li><%= link_to 'New root menu snippets', new_admin_snippet_path( :menu => 'root' ), :class => 'button positive' %></li>
</ul>
<% end -%>
<h1>Listing pages: <%= params[:parent] || "root" %></h1>
<table>
<tr>
<th>Name</th>
<th>Title</th>
<th>Layout</th>
<th>Position</th>
<th>Published?</th>
<th>Action</th>
</tr>
<% @pages.each do |page| %>
<tr>
<td><%= link_to page.name, admin_pages_path(:parent => page.name) %></td>
<td><%=h page.title %>
<td><%= page.layout_name %></th>
<td><%= page.position %></td>
<td><%= page.is_published.to_s %></td>
<td>
<%= link_to t(:show), page_path(page.name) %> |
<%= link_to t(:edit), edit_admin_page_path(page) %> |
<%= link_to t(:add_chile_page, :scope => :admin), new_admin_page_path( :parent_page_name => page.name ) %> |
<%= link_to t(:delete), admin_page_path(page), :confirm => 'Are you sure?', :method => :delete %>
</td>
</tr>
<% end %>
</table>

View File

@ -1,8 +1,6 @@
<h1>New pages</h1>
<% form_for :page, :url => admin_pages_path do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :parent_page_name %>
<%= render :partial => "form", :locals => { :f => f } %>
@ -11,5 +9,3 @@
</p>
<% end %>
<%= link_to 'Back', admin_pages_path %>

View File

@ -1,3 +1,6 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<p>
<%= f.label :name, "Name" %>
<%= f.text_field :name, :class => 'text' %>

View File

@ -9,5 +9,3 @@
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Back', admin_snippets_path %>

View File

@ -1,18 +0,0 @@
<h1>Listing snippets</h1>
<table>
<tr>
</tr>
<% @snippets.each do |snippet| %>
<tr>
<td><%= snippet.name %></td>
<td><%= link_to 'Edit', edit_admin_snippet_path(snippet) %></td>
<td><%= link_to 'Destroy', admin_snippet_path(snippet), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New snippets', new_admin_snippet_path, :class => 'button positive' %>

View File

@ -9,5 +9,3 @@
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', admin_snippets_path %>

View File

@ -1,3 +0,0 @@
<%= link_to 'Edit', edit_announcement_path(@announcement) %> |
<%= link_to 'Back', announcements_path %>

View File

@ -20,8 +20,7 @@
<ul id="nav">
<li><%= link_to t(:home, :scope => :admin), root_path %></li>
<li><%= link_to t(:page, :scope => :admin), admin_pages_path %></li>
<li><%= link_to t(:snippet, :scope => :admin), admin_snippets_path %></li>
<li><%= link_to t(:item, :scope => :admin), admin_items_path %></li>
<li><%= link_to t(:layout, :scope => :admin), admin_layouts_path %></li>
<li><%= link_to t(:announcement, :scope => :admin), panel_announcements_path( :entry_name => 'news' ) %></li>
</ul>

View File

@ -61,6 +61,12 @@ module MongoMapper::Document::ClassMethods
define_method(key) do
self.send("#{key.to_s}_#{I18n.locale}")
end
define_method("#{key}=") do |value|
VALID_LOCALES.each do |locale|
self.send("#{key.to_s}_#{locale}=", value)
end
end
end
end

View File

@ -1,12 +1,16 @@
zh_tw:
homepage: 首頁
show: 顯示
edit: 編輯
delete: 刪除
admin:
home: 首頁
page: 頁面管理
snippet: 片段管理
item: 項目管理
layout: 樣版管理
add_chile_page: 新增子頁
new_page: 新增頁面
new_component: 新增元件
new_link: 新增連結
new_snippet: 新增片段
new_layout: 新增樣板
announcement: 公告管理

View File

@ -3,7 +3,10 @@ ActionController::Routing::Routes.draw do |map|
map.resources :announcements
map.namespace :admin do |admin|
admin.resources :items
admin.resources :pages
admin.resources :links
admin.resources :components
admin.resources :layouts
admin.resources :snippets
end

7
lib/layout_support.rb Normal file
View File

@ -0,0 +1,7 @@
module LayoutSupport
def layout
Layout.find_by_name(self.layout_name)
end
end