From 496d73487b39e7dab6a87589d91aa40ae0f223dc Mon Sep 17 00:00:00 2001 From: Wen-Tien Chang Date: Mon, 22 Feb 2010 17:09:39 +0800 Subject: [PATCH] Fixed Item#parent_name again --- app/controllers/application_controller.rb | 2 +- app/models/item.rb | 4 ++-- app/models/layout.rb | 2 +- app/models/snippet.rb | 8 ++++---- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 570cb8b8..c380716f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/models/item.rb b/app/models/item.rb index 11a7f5dc..db643770 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -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) diff --git a/app/models/layout.rb b/app/models/layout.rb index 3b4f15b7..71d82c2f 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -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 \ No newline at end of file diff --git a/app/models/snippet.rb b/app/models/snippet.rb index a1b4b669..37a2dfa9 100644 --- a/app/models/snippet.rb +++ b/app/models/snippet.rb @@ -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