From f9b1618ac7a97395b03e78724fbd0acad2b50dae Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 22 Feb 2010 15:27:11 +0800 Subject: [PATCH] Fixed Item#parent_name --- app/controllers/admin/items_controller.rb | 4 ++-- app/models/item.rb | 12 ++++++++---- app/models/snippet.rb | 6 +++++- lib/reroute_middleware.rb | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/controllers/admin/items_controller.rb b/app/controllers/admin/items_controller.rb index 0f2cf2f3..d2b60843 100644 --- a/app/controllers/admin/items_controller.rb +++ b/app/controllers/admin/items_controller.rb @@ -5,14 +5,14 @@ class Admin::ItemsController < ApplicationController before_filter :find_snippets, :only => :index def index - @items = Item.all( :conditions => { :parent_name => @parent_item.name } ) + @items = Item.all( :conditions => { :parent_id => @parent_item.id } ) @items.unshift Item.find_by_name("root") if @parent_item.name == 'root' end protected def find_snippets - @snippets = Snippet.all( :conditions => { :parent_name => @parent_item.name } ) + @snippets = Snippet.all( :conditions => { :parent_id => @parent_item.id } ) end end diff --git a/app/models/item.rb b/app/models/item.rb index ece28eb8..11a7f5dc 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -6,8 +6,7 @@ class Item 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 key_i18n :title, String, :required => true @@ -15,14 +14,19 @@ 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 => /^[0-9a-zA-Z\-_]+$/ 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 before_validation :setup_default_value - + + attr_writer :parent_name + def parent_name + self.parent.name + end + def self.find_by_name(item_name) Item.find(:first, :conditions => { :name => item_name, :is_published => true }) end diff --git a/app/models/snippet.rb b/app/models/snippet.rb index 6e940f8f..a1b4b669 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -5,7 +5,6 @@ class Snippet 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 key_i18n :content, String @@ -15,6 +14,11 @@ class Snippet belongs_to :parent, :class_name => "Snippet", :foreign_key => :parent_id + attr_writer :parent_name + def parent_name + self.parent.name + end + protected def ancestors diff --git a/lib/reroute_middleware.rb b/lib/reroute_middleware.rb index 2d24fcf5..2db436d7 100644 --- a/lib/reroute_middleware.rb +++ b/lib/reroute_middleware.rb @@ -9,7 +9,7 @@ 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'] =~ /^\/([0-9a-zA-Z\-_\/]*)/ parsed_entry_name = $1 entry = Item.find_by_full_name( parsed_entry_name )