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
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
@parent_item = Page.create( :name => "root", :title => "root", :layout_name => "root" )
end

View File

@ -22,9 +22,9 @@ class Item
before_validation :setup_default_value
attr_writer :parent_name
attr_accessor :parent_name
def parent_name
self.parent.name
@parent_name || self.parent.name
end
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
validates_format_of :name, :with => /^[a-zA-Z-_]+$/
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates_uniqueness_of :name
end

View File

@ -12,11 +12,11 @@ class Snippet
before_validation :setup_default_value
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
self.parent.name
@parent_name || self.parent.name
end
protected
@ -28,7 +28,7 @@ class Snippet
end
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.shift if full_node.size >= 2