From 32e8bd108fe5088359b381210ee6487ac93c8d0b Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 18 Jan 2010 15:52:52 +0800 Subject: [PATCH] Add Item#ancestors --- .../admin/components_controller.rb | 8 +++---- app/controllers/admin/items_controller.rb | 5 +++-- app/controllers/admin/links_controller.rb | 8 +++---- app/controllers/admin/pages_controller.rb | 8 +++---- app/controllers/admin/snippets_controller.rb | 8 +++---- app/controllers/application_controller.rb | 4 ++-- app/helpers/admin_helper.rb | 8 +++++++ app/models/item.rb | 22 +++++++++++++++---- app/models/snippet.rb | 2 +- app/views/admin/components/_form.html.erb | 2 +- app/views/admin/items/_component.html.erb | 2 +- app/views/admin/items/_link.html.erb | 2 +- app/views/admin/items/_page.html.erb | 2 +- app/views/admin/items/index.html.erb | 10 ++++----- app/views/admin/links/_form.html.erb | 2 +- app/views/admin/pages/_form.html.erb | 2 +- app/views/admin/snippets/_form.html.erb | 2 +- 17 files changed, 60 insertions(+), 37 deletions(-) create mode 100644 app/helpers/admin_helper.rb diff --git a/app/controllers/admin/components_controller.rb b/app/controllers/admin/components_controller.rb index 4cdb5000..fda62c9a 100644 --- a/app/controllers/admin/components_controller.rb +++ b/app/controllers/admin/components_controller.rb @@ -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 diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index 7fe5bc20..0f2cf2f3 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -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 diff --git a/app/controllers/admin/links_controller.rb b/app/controllers/admin/links_controller.rb index 2757d7bb..71ac9f3b 100644 --- a/app/controllers/admin/links_controller.rb +++ b/app/controllers/admin/links_controller.rb @@ -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 diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index f83fd92c..5ac4bf71 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -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 diff --git a/app/controllers/admin/snippets_controller.rb b/app/controllers/admin/snippets_controller.rb index 0929eca3..2f8ad933 100644 --- a/app/controllers/admin/snippets_controller.rb +++ b/app/controllers/admin/snippets_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c5f01694..570cb8b8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb new file mode 100644 index 00000000..c7d07fdf --- /dev/null +++ b/app/helpers/admin_helper.rb @@ -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 \ No newline at end of file diff --git a/app/models/item.rb b/app/models/item.rb index 2051e607..20c81eb6 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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 diff --git a/app/models/snippet.rb b/app/models/snippet.rb index f1046528..3c39240d 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -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 diff --git a/app/views/admin/components/_form.html.erb b/app/views/admin/components/_form.html.erb index 71788848..26e19275 100644 --- a/app/views/admin/components/_form.html.erb +++ b/app/views/admin/components/_form.html.erb @@ -1,5 +1,5 @@ <%= f.error_messages %> -<%= f.hidden_field :parent_item_name %> +<%= f.hidden_field :parent_name %>

<%= f.label :name, "Name" %> diff --git a/app/views/admin/items/_component.html.erb b/app/views/admin/items/_component.html.erb index 143debc3..4f07f2ae 100644 --- a/app/views/admin/items/_component.html.erb +++ b/app/views/admin/items/_component.html.erb @@ -1,6 +1,6 @@ <%= item.class %> - <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%= link_to item.name, admin_items_path(:parent_name => item.name) %> <%=h item.title %> <%= item.position %> <%= item.is_published.to_s %> diff --git a/app/views/admin/items/_link.html.erb b/app/views/admin/items/_link.html.erb index 7ac8f619..7e167b0c 100644 --- a/app/views/admin/items/_link.html.erb +++ b/app/views/admin/items/_link.html.erb @@ -1,6 +1,6 @@ <%= item.class %> - <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%= link_to item.name, admin_items_path(:parent_name => item.name) %> <%=h item.title %> <%= item.position %> <%= item.is_published.to_s %> diff --git a/app/views/admin/items/_page.html.erb b/app/views/admin/items/_page.html.erb index 8a782a88..0f9ce5f1 100644 --- a/app/views/admin/items/_page.html.erb +++ b/app/views/admin/items/_page.html.erb @@ -1,6 +1,6 @@ <%= item.class %> - <%= link_to item.name, admin_items_path(:parent_item_name => item.name) %> + <%= link_to item.name, admin_items_path(:parent_name => item.name) %> <%=h item.title %> <%= item.position %> <%= item.is_published.to_s %> diff --git a/app/views/admin/items/index.html.erb b/app/views/admin/items/index.html.erb index fa3c651b..3db021bc 100644 --- a/app/views/admin/items/index.html.erb +++ b/app/views/admin/items/index.html.erb @@ -1,13 +1,13 @@ <% content_for :secondary do %>

<% end -%> -

Listing items: <%= @parent_item.name %>/

+

Listing items: <%= show_parent_items_link(@parent_item) %>

diff --git a/app/views/admin/links/_form.html.erb b/app/views/admin/links/_form.html.erb index b81d2526..65a2cffa 100644 --- a/app/views/admin/links/_form.html.erb +++ b/app/views/admin/links/_form.html.erb @@ -1,5 +1,5 @@ <%= f.error_messages %> -<%= f.hidden_field :parent_item_name %> +<%= f.hidden_field :parent_name %>

<%= f.label :name, "Name" %> diff --git a/app/views/admin/pages/_form.html.erb b/app/views/admin/pages/_form.html.erb index 1605dd9c..9550e63f 100644 --- a/app/views/admin/pages/_form.html.erb +++ b/app/views/admin/pages/_form.html.erb @@ -1,5 +1,5 @@ <%= f.error_messages %> -<%= f.hidden_field :parent_item_name %> +<%= f.hidden_field :parent_name %>

<%= f.label :name, "Name" %> diff --git a/app/views/admin/snippets/_form.html.erb b/app/views/admin/snippets/_form.html.erb index 25734b36..77b6301a 100644 --- a/app/views/admin/snippets/_form.html.erb +++ b/app/views/admin/snippets/_form.html.erb @@ -1,5 +1,5 @@ <%= f.error_messages %> -<%= f.hidden_field :parent_item_name %> +<%= f.hidden_field :parent_name %>

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