Let nested route works

This commit is contained in:
Wen-Tien Chang 2010-02-11 16:46:20 +08:00
parent 66a462faa8
commit 9fa03db2c0
5 changed files with 22 additions and 6 deletions

View File

@ -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

View File

@ -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
@ -53,6 +53,10 @@ class Item
else
self.parent_id = Item.find_by_name( self.parent_name ).id
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

View File

@ -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

View File

@ -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'

View File

@ -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' :