From bb9d9ccd57ce5673b52a1856dfd32337f4ae4b22 Mon Sep 17 00:00:00 2001 From: hukl Date: Sun, 8 Feb 2009 14:06:31 +0100 Subject: refactored refined concept of nodes and pages into the node model --- app/models/node.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'app') diff --git a/app/models/node.rb b/app/models/node.rb index 4e865e3..cd02657 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -4,6 +4,10 @@ class Node < ActiveRecord::Base has_many :pages, :order => "revision ASC" belongs_to :head, :class_name => "Page", :foreign_key => :head_id + # Callbacks + + after_create :initialize_empty_page + # Class methods @@ -43,12 +47,20 @@ class Node < ActiveRecord::Base draft else # make a new fresh page with node reference - draft = Page.new( :node_id => id) + draft = Page.create( head.attributes ) end draft end + def publish_draft! + self.head = self.draft + self.save! + + self.head.published_at = Time.now + self.head.save! + end + # returns an array with all parts of a unique_name rather than a string def unique_path unique_name.split("/") rescue unique_name @@ -64,4 +76,17 @@ class Node < ActiveRecord::Base self.unique_name = path.join("/") self.save end + + protected + + # Creates an empty page, associates it to the given node and sets its + # published_at date so it isn't considered a draft. Look up the draft + # method! + def initialize_empty_page + if self.pages.empty? + self.pages.create! + end + end end + + -- cgit v1.3