Add Item#ancestors

This commit is contained in:
Wen-Tien Chang 2010-01-18 15:52:52 +08:00
parent e1440a79d8
commit 32e8bd108f
17 changed files with 60 additions and 37 deletions

View File

@ -10,7 +10,7 @@ class Admin::ComponentsController < ApplicationController
def new
@component = Component.new
@component.is_published = true
@component.parent_item_name = @parent_item.name
@component.parent_name = @parent_item.name
@component.engine_name = 'Announcement' # only support Announcement now
end
@ -24,7 +24,7 @@ class Admin::ComponentsController < ApplicationController
if @component.save
flash[:notice] = 'Component was successfully created.'
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
redirect_to admin_items_url( :parent_name => @component.parent_name )
else
render :action => "new"
end
@ -35,7 +35,7 @@ class Admin::ComponentsController < ApplicationController
if @component.update_attributes(params[:component])
flash[:notice] = 'Component was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
redirect_to admin_items_url( :parent_name => @component.parent_name )
else
render :action => "edit"
end
@ -47,7 +47,7 @@ class Admin::ComponentsController < ApplicationController
#TODO: destroy engine data
redirect_to admin_items_url( :parent_item_name => @component.parent_item_name )
redirect_to admin_items_url( :parent_name => @component.parent_name )
end
end

View File

@ -5,13 +5,14 @@ class Admin::ItemsController < ApplicationController
before_filter :find_snippets, :only => :index
def index
@items = Item.all( :conditions => { :parent_item_name => @parent_item.name } )
@items = Item.all( :conditions => { :parent_name => @parent_item.name } )
@items.unshift Item.find_by_name("root") if @parent_item.name == 'root'
end
protected
def find_snippets
@snippets = Snippet.all( :conditions => { :parent_item_name => @parent_item.name } )
@snippets = Snippet.all( :conditions => { :parent_name => @parent_item.name } )
end
end

View File

@ -10,7 +10,7 @@ class Admin::LinksController < ApplicationController
def new
@link = Link.new
@link.is_published = true
@link.parent_item_name = @parent_item.name
@link.parent_name = @parent_item.name
end
def edit
@ -22,7 +22,7 @@ class Admin::LinksController < ApplicationController
if @link.save
flash[:notice] = 'Link was successfully created.'
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
redirect_to admin_items_url( :parent_name => @link.parent_name )
else
render :action => "new"
end
@ -33,7 +33,7 @@ class Admin::LinksController < ApplicationController
if @link.update_attributes(params[:link])
flash[:notice] = 'Link was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
redirect_to admin_items_url( :parent_name => @link.parent_name )
else
render :action => "edit"
end
@ -43,7 +43,7 @@ class Admin::LinksController < ApplicationController
@link = Link.find(params[:id])
@link.destroy
redirect_to admin_items_url( :parent_item_name => @link.parent_item_name )
redirect_to admin_items_url( :parent_name => @link.parent_name )
end
end

View File

@ -10,7 +10,7 @@ class Admin::PagesController < ApplicationController
def new
@page = Page.new
@page.is_published = true
@page.parent_item_name = @parent_item.name
@page.parent_name = @parent_item.name
end
def edit
@ -22,7 +22,7 @@ class Admin::PagesController < ApplicationController
if @page.save
flash[:notice] = 'Page was successfully created.'
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
redirect_to admin_items_url( :parent_name => @page.parent_name )
else
render :action => "new"
end
@ -33,7 +33,7 @@ class Admin::PagesController < ApplicationController
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
redirect_to admin_items_url( :parent_name => @page.parent_name )
else
render :action => "edit"
end
@ -43,7 +43,7 @@ class Admin::PagesController < ApplicationController
@page = Page.find(params[:id])
@page.destroy
redirect_to admin_items_url( :parent_item_name => @page.parent_item_name )
redirect_to admin_items_url( :parent_name => @page.parent_name )
end
end

View File

@ -9,7 +9,7 @@ class Admin::SnippetsController < ApplicationController
def new
@snippet = Snippet.new
@snippet.parent_item_name = @parent_item.name
@snippet.parent_name = @parent_item.name
end
def edit
@ -21,7 +21,7 @@ class Admin::SnippetsController < ApplicationController
if @snippet.save
flash[:notice] = 'Snippet was successfully created.'
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
redirect_to admin_items_url( :parent_name => @snippet.parent_name )
else
render :action => "new"
end
@ -32,7 +32,7 @@ class Admin::SnippetsController < ApplicationController
if @snippet.update_attributes(params[:snippet])
flash[:notice] = 'Snippet was successfully updated.'
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
redirect_to admin_items_url( :parent_name => @snippet.parent_name )
else
render :action => "edit"
end
@ -42,7 +42,7 @@ class Admin::SnippetsController < ApplicationController
@snippet = Snippet.find(params[:id])
@snippet.destroy
redirect_to admin_items_url( :parent_item_name => @snippet.parent_item_name )
redirect_to admin_items_url( :parent_name => @snippet.parent_name )
end
end

View File

@ -32,9 +32,9 @@ class ApplicationController < ActionController::Base
end
def find_parent_item
@parent_item = Item.find_by_name(params[:parent_item_name] || 'root')
@parent_item = Item.find_by_name(params[:parent_name])
unless @parent_item
@parent_item = Page.create( :name => 'root', :title => t(:homepage), :layout_name => "root" )
@parent_item = Page.create( :name => "root", :title => "root", :layout_name => "root" )
end
end

View File

@ -0,0 +1,8 @@
module AdminHelper
def show_parent_items_link(parent_item)
@parent_items = @parent_item.ancestors.map{ |i| i.name }
( @parent_items.map{ |i| link_to(i, admin_items_path(:parent_name=>i) ) } << parent_item.name ).join("/")
end
end

View File

@ -5,19 +5,30 @@ class Item
key :_type, String
key :name, String, :required => true, :index => true
key :parent_item_name, String, :required => true, :index => true
key :parent_name, String, :index => true
key :parent_id, String, :index => true
key_i18n :title, String, :required => true
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
many :children, :class_name => "Item", :foreign_key => :parent_id
before_validation :setup_default_value
def self.find_by_name(item_name)
Item.find(:first, :conditions => { :name => item_name, :is_published => true })
end
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank?
nodes.reverse
end
protected
def setup_default_value
@ -26,8 +37,11 @@ class Item
self.position = (max_page)? max_page.position.to_i + 1 : 1
end
if self.parent_item_name.blank?
self.parent_item_name = "root"
if self.parent_name.blank?
self.parent_name = nil
self.parent_id = nil
else
self.parent_id = Item.find_by_name( self.parent_name ).id
end
end

View File

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

View File

@ -1,5 +1,5 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<%= f.hidden_field :parent_name %>
<p>
<%= f.label :name, "Name" %>

View File

@ -1,6 +1,6 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%= link_to item.name, admin_items_path(:parent_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>

View File

@ -1,6 +1,6 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%= link_to item.name, admin_items_path(:parent_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>

View File

@ -1,6 +1,6 @@
<tr>
<td><%= item.class %></td>
<td><%= link_to item.name, admin_items_path(:parent_item_name => item.name) %></td>
<td><%= link_to item.name, admin_items_path(:parent_name => item.name) %></td>
<td><%=h item.title %>
<td><%= item.position %></td>
<td><%= item.is_published.to_s %></td>

View File

@ -1,13 +1,13 @@
<% 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>
<li><%= link_to t(:new_page, :scope => :admin), new_admin_page_path( :parent_name => @parent_item.name ), :class => 'button positive' %></li>
<li><%= link_to t(:new_component, :scope => :admin), new_admin_component_path( :parent_name => @parent_item.name ) %>
<li><%= link_to t(:new_link, :scope => :admin), new_admin_link_path( :parent_name => @parent_item.name ) %>
<li><%= link_to t(:new_snippet, :scope => :admin ), new_admin_snippet_path( :parent_name => @parent_item.name ), :class => 'button positive' %></li>
</ul>
<% end -%>
<h1>Listing items: <%= @parent_item.name %>/</h1>
<h1>Listing items: <%= show_parent_items_link(@parent_item) %></h1>
<table>
<tr>

View File

@ -1,5 +1,5 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<%= f.hidden_field :parent_name %>
<p>
<%= f.label :name, "Name" %>

View File

@ -1,5 +1,5 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<%= f.hidden_field :parent_name %>
<p>
<%= f.label :name, "Name" %>

View File

@ -1,5 +1,5 @@
<%= f.error_messages %>
<%= f.hidden_field :parent_item_name %>
<%= f.hidden_field :parent_name %>
<p>
<%= f.label :name, "Name" %>