Fixed Item#parent_name again

This commit is contained in:
Wen-Tien Chang 2010-02-22 17:09:39 +08:00
parent 93169b1098
commit 496d73487b
4 changed files with 8 additions and 8 deletions

View File

@ -32,7 +32,7 @@ class ApplicationController < ActionController::Base
end end
def find_parent_item def find_parent_item
@parent_item = Item.find_by_name(params[:parent_name]) @parent_item = Item.find_by_name(params[:parent_name] || 'root')
unless @parent_item unless @parent_item
@parent_item = Page.create( :name => "root", :title => "root", :layout_name => "root" ) @parent_item = Page.create( :name => "root", :title => "root", :layout_name => "root" )
end end

View File

@ -22,9 +22,9 @@ class Item
before_validation :setup_default_value before_validation :setup_default_value
attr_writer :parent_name attr_accessor :parent_name
def parent_name def parent_name
self.parent.name @parent_name || self.parent.name
end end
def self.find_by_name(item_name) def self.find_by_name(item_name)

View File

@ -8,7 +8,7 @@ class Layout
many :children, :class_name => "Item", :foreign_key => "layout_id", :dependent => :nullify many :children, :class_name => "Item", :foreign_key => "layout_id", :dependent => :nullify
validates_format_of :name, :with => /^[a-zA-Z-_]+$/ validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates_uniqueness_of :name validates_uniqueness_of :name
end end

View File

@ -12,11 +12,11 @@ class Snippet
before_validation :setup_default_value before_validation :setup_default_value
validates_uniqueness_of :name, :scope => :parent_id validates_uniqueness_of :name, :scope => :parent_id
belongs_to :parent, :class_name => "Snippet", :foreign_key => :parent_id belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
attr_writer :parent_name attr_accessor :parent_name
def parent_name def parent_name
self.parent.name @parent_name || self.parent.name
end end
protected protected
@ -28,7 +28,7 @@ class Snippet
end end
def setup_default_value def setup_default_value
self.parent_id = Item.find_by_name( self.parent_name ).id self.parent_id = Item.find_by_name( self.parent_name || 'root' ).id
full_node = self.ancestors.map{ |a| a.name }.push( self.name ) full_node = self.ancestors.map{ |a| a.name }.push( self.name )
full_node.shift if full_node.size >= 2 full_node.shift if full_node.size >= 2