Fixed Item#parent_name

This commit is contained in:
Wen-Tien Chang 2010-02-22 15:27:11 +08:00
parent 9fa03db2c0
commit f9b1618ac7
4 changed files with 16 additions and 8 deletions

View File

@ -5,14 +5,14 @@ class Admin::ItemsController < ApplicationController
before_filter :find_snippets, :only => :index before_filter :find_snippets, :only => :index
def 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' @items.unshift Item.find_by_name("root") if @parent_item.name == 'root'
end end
protected protected
def find_snippets def find_snippets
@snippets = Snippet.all( :conditions => { :parent_name => @parent_item.name } ) @snippets = Snippet.all( :conditions => { :parent_id => @parent_item.id } )
end end
end end

View File

@ -6,8 +6,7 @@ class Item
key :name, String, :required => true, :index => true key :name, String, :required => true, :index => true
key :full_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 :parent_id, ObjectId, :index => true
key_i18n :title, String, :required => true key_i18n :title, String, :required => true
@ -15,14 +14,19 @@ class Item
key :position, Integer, :required => true key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => 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 validates_uniqueness_of :name, :scope => :parent_id
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
many :children, :class_name => "Item", :foreign_key => :parent_id, :dependent => :destroy many :children, :class_name => "Item", :foreign_key => :parent_id, :dependent => :destroy
before_validation :setup_default_value before_validation :setup_default_value
attr_writer :parent_name
def parent_name
self.parent.name
end
def self.find_by_name(item_name) def self.find_by_name(item_name)
Item.find(:first, :conditions => { :name => item_name, :is_published => true }) Item.find(:first, :conditions => { :name => item_name, :is_published => true })
end end

View File

@ -5,7 +5,6 @@ class Snippet
key :name, String, :required => true, :index => true key :name, String, :required => true, :index => true
key :full_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 :parent_id, ObjectId, :required => true, :index => true
key_i18n :content, String key_i18n :content, String
@ -15,6 +14,11 @@ class Snippet
belongs_to :parent, :class_name => "Snippet", :foreign_key => :parent_id belongs_to :parent, :class_name => "Snippet", :foreign_key => :parent_id
attr_writer :parent_name
def parent_name
self.parent.name
end
protected protected
def ancestors def ancestors

View File

@ -9,7 +9,7 @@ class RerouteMiddleware
#Rails.logger.debug env.to_yaml #Rails.logger.debug env.to_yaml
return @app.call(env) if env['REQUEST_URI'] =~ /^\/admin/ 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 parsed_entry_name = $1
entry = Item.find_by_full_name( parsed_entry_name ) entry = Item.find_by_full_name( parsed_entry_name )