From 66a462faa83bc342fd21be68232e1b12f9c4458b Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Fri, 5 Feb 2010 17:18:57 +0800 Subject: [PATCH] Add nested route url --- app/models/item.rb | 11 +++++++++-- app/models/snippet.rb | 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 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app/models/item.rb b/app/models/item.rb index a8f4f9c0..92e25874 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -5,7 +5,8 @@ class Item key :_type, String key :name, String, :required => true, :index => true - + key :full_name, String, :required => true, :index => true + key :parent_name, String, :index => true key :parent_id, ObjectId, :index => true @@ -15,7 +16,7 @@ class Item key :is_published, Boolean, :required => true, :default => true, :index => true validates_format_of :name, :with => /^[a-zA-Z-_]+$/ - validates_uniqueness_of :name + validates_uniqueness_of :name, :scope => :parent_id belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id many :children, :class_name => "Item", :foreign_key => :parent_id, :dependent => :destroy @@ -32,6 +33,12 @@ class Item nodes.reverse end + def url + urls = ancestors.map{ |a| a.name } << self.name + urls.delete("root") + "/#{urls.join("/")}" + end + protected def setup_default_value diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 688bf945..472c2a03 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -3,6 +3,8 @@ class Snippet include MongoMapper::Document key :name, String, :required => true, :index => true + key :full_name, String, :required => true, :index => true + key :parent_name, String, :required => true, :index => true key :parent_id, ObjectId, :required => true, :index => true diff --git a/app/views/admin/items/_component.html.erb b/app/views/admin/items/_component.html.erb index 4f07f2ae..1ec404bd 100644 --- a/app/views/admin/items/_component.html.erb +++ b/app/views/admin/items/_component.html.erb @@ -5,7 +5,7 @@ <%= item.position %> <%= item.is_published.to_s %> - <%= link_to t(:show), admin_component_path(item.name) %> | + <%= link_to t(:show), item.url %> | <%= link_to t(:edit), edit_admin_component_path(item) %> | <%= link_to t(:delete), admin_component_path(item), :confirm => 'Are you sure?', :method => :delete %> diff --git a/app/views/admin/items/_link.html.erb b/app/views/admin/items/_link.html.erb index 7e167b0c..db692c70 100644 --- a/app/views/admin/items/_link.html.erb +++ b/app/views/admin/items/_link.html.erb @@ -5,7 +5,7 @@ <%= item.position %> <%= item.is_published.to_s %> - <%= link_to t(:show), admin_link_path(item.name) %> | + <%= link_to t(:show), item.url %> | <%= link_to t(:edit), edit_admin_link_path(item) %> | <%= link_to t(:delete), admin_link_path(item), :confirm => 'Are you sure?', :method => :delete %> diff --git a/app/views/admin/items/_page.html.erb b/app/views/admin/items/_page.html.erb index 0f9ce5f1..bf217b97 100644 --- a/app/views/admin/items/_page.html.erb +++ b/app/views/admin/items/_page.html.erb @@ -5,7 +5,7 @@ <%= item.position %> <%= item.is_published.to_s %> - <%= link_to t(:show), admin_page_path(item.name) %> | + <%= link_to t(:show), item.url %> | <%= link_to t(:edit), edit_admin_page_path(item) %> | <%= link_to t(:delete), admin_page_path(item), :confirm => 'Are you sure?', :method => :delete %>