From 9fa03db2c02323c96b3c4a50c075f2d26c17adcb Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Thu, 11 Feb 2010 16:46:20 +0800 Subject: [PATCH] Let nested route works --- app/controllers/pages_controller.rb | 2 +- app/models/item.rb | 8 ++++++-- app/models/snippet.rb | 12 ++++++++++++ config/routes.rb | 2 +- lib/reroute_middleware.rb | 4 ++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 00704a73..92644ad6 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -10,7 +10,7 @@ class PagesController < ApplicationController end def show - @page = Page.find_by_name(params[:page_name]) + @page = Page.find_by_full_name(params[:page_name].join("/")) render_liquid_page end diff --git a/app/models/item.rb b/app/models/item.rb index 92e25874..ece28eb8 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -15,7 +15,7 @@ class Item key :position, Integer, :required => true key :is_published, Boolean, :required => true, :default => true, :index => true - validates_format_of :name, :with => /^[a-zA-Z-_]+$/ + validates_format_of :name, :with => /^[a-zA-Z\-_]+$/ validates_uniqueness_of :name, :scope => :parent_id belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id @@ -52,7 +52,11 @@ class Item self.parent_id = nil else self.parent_id = Item.find_by_name( self.parent_name ).id - end + end + + full_node = self.ancestors.map{ |a| a.name }.push( self.name ) + full_node.shift if full_node.size >= 2 + self.full_name = full_node.join("/") end end \ No newline at end of file diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 472c2a03..6e940f8f 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -13,10 +13,22 @@ class Snippet before_validation :setup_default_value validates_uniqueness_of :name, :scope => :parent_id + belongs_to :parent, :class_name => "Snippet", :foreign_key => :parent_id + protected + def ancestors + node, nodes = self, [] + nodes << node = node.parent while !node.parent.blank? + nodes.reverse + end + def setup_default_value self.parent_id = Item.find_by_name( self.parent_name ).id + + full_node = self.ancestors.map{ |a| a.name }.push( self.name ) + full_node.shift if full_node.size >= 2 + self.full_name = full_node.join("/") end end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 20cdf488..a9a7faaa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,7 +56,7 @@ ActionController::Routing::Routes.draw do |map| # Note: These default routes make all actions in every controller accessible via GET requests. You should # consider removing the them or commenting them out if you're using named routes and resources. - map.page ':page_name', :controller => 'pages', :action => 'show' + map.page '*page_name', :controller => 'pages', :action => 'show' #map.connect ':controller/:action/:id' #map.connect ':controller/:action/:id.:format' diff --git a/lib/reroute_middleware.rb b/lib/reroute_middleware.rb index 29dc89c4..2d24fcf5 100644 --- a/lib/reroute_middleware.rb +++ b/lib/reroute_middleware.rb @@ -9,10 +9,10 @@ class RerouteMiddleware #Rails.logger.debug env.to_yaml return @app.call(env) if env['REQUEST_URI'] =~ /^\/admin/ - env['REQUEST_URI'] =~ /^\/([a-zA-Z-_]*)/ + env['REQUEST_URI'] =~ /^\/([a-zA-Z\-_\/]*)/ parsed_entry_name = $1 - entry = Item.find_by_name( parsed_entry_name ) + entry = Item.find_by_full_name( parsed_entry_name ) case entry.type.to_s when 'Component' :