diff options
Diffstat (limited to 'app/models/node.rb')
| -rw-r--r-- | app/models/node.rb | 27 |
1 files changed, 26 insertions, 1 deletions
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 | |||
| 4 | has_many :pages, :order => "revision ASC" | 4 | has_many :pages, :order => "revision ASC" |
| 5 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id | 5 | belongs_to :head, :class_name => "Page", :foreign_key => :head_id |
| 6 | 6 | ||
| 7 | # Callbacks | ||
| 8 | |||
| 9 | after_create :initialize_empty_page | ||
| 10 | |||
| 7 | # Class methods | 11 | # Class methods |
| 8 | 12 | ||
| 9 | 13 | ||
| @@ -43,12 +47,20 @@ class Node < ActiveRecord::Base | |||
| 43 | draft | 47 | draft |
| 44 | else | 48 | else |
| 45 | # make a new fresh page with node reference | 49 | # make a new fresh page with node reference |
| 46 | draft = Page.new( :node_id => id) | 50 | draft = Page.create( head.attributes ) |
| 47 | end | 51 | end |
| 48 | 52 | ||
| 49 | draft | 53 | draft |
| 50 | end | 54 | end |
| 51 | 55 | ||
| 56 | def publish_draft! | ||
| 57 | self.head = self.draft | ||
| 58 | self.save! | ||
| 59 | |||
| 60 | self.head.published_at = Time.now | ||
| 61 | self.head.save! | ||
| 62 | end | ||
| 63 | |||
| 52 | # returns an array with all parts of a unique_name rather than a string | 64 | # returns an array with all parts of a unique_name rather than a string |
| 53 | def unique_path | 65 | def unique_path |
| 54 | unique_name.split("/") rescue unique_name | 66 | unique_name.split("/") rescue unique_name |
| @@ -64,4 +76,17 @@ class Node < ActiveRecord::Base | |||
| 64 | self.unique_name = path.join("/") | 76 | self.unique_name = path.join("/") |
| 65 | self.save | 77 | self.save |
| 66 | end | 78 | end |
| 79 | |||
| 80 | protected | ||
| 81 | |||
| 82 | # Creates an empty page, associates it to the given node and sets its | ||
| 83 | # published_at date so it isn't considered a draft. Look up the draft | ||
| 84 | # method! | ||
| 85 | def initialize_empty_page | ||
| 86 | if self.pages.empty? | ||
| 87 | self.pages.create! | ||
| 88 | end | ||
| 89 | end | ||
| 67 | end | 90 | end |
| 91 | |||
| 92 | |||
