diff options
Diffstat (limited to 'app/models/node.rb')
| -rw-r--r-- | app/models/node.rb | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index cd02657..bc48ac4 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -10,7 +10,6 @@ class Node < ActiveRecord::Base | |||
| 10 | 10 | ||
| 11 | # Class methods | 11 | # Class methods |
| 12 | 12 | ||
| 13 | |||
| 14 | # Returns a page for a given node. If no revision is supplied, it returns | 13 | # Returns a page for a given node. If no revision is supplied, it returns |
| 15 | # the last / current one. If a specific revision number is supplied, the | 14 | # the last / current one. If a specific revision number is supplied, the |
| 16 | # corresponding revision of that page is returned. Get the current / latest | 15 | # corresponding revision of that page is returned. Get the current / latest |
| @@ -37,28 +36,34 @@ class Node < ActiveRecord::Base | |||
| 37 | 36 | ||
| 38 | # Instance Methods | 37 | # Instance Methods |
| 39 | 38 | ||
| 39 | # check if there is a page which has a nil :published_at column | ||
| 40 | # if there is one - it is considered a draft | ||
| 40 | def draft | 41 | def draft |
| 41 | |||
| 42 | # check if there is a page which has a nil :published_at column | ||
| 43 | # if there is one - it is considered a draft else a new revision is | ||
| 44 | # created | ||
| 45 | |||
| 46 | if draft = pages.find_by_published_at(nil) | 42 | if draft = pages.find_by_published_at(nil) |
| 47 | draft | 43 | draft |
| 44 | end | ||
| 45 | end | ||
| 46 | |||
| 47 | def find_or_create_draft user | ||
| 48 | if draft && draft.user == user | ||
| 49 | draft | ||
| 50 | elsif draft && draft.user != user | ||
| 51 | raise "Page is locked" | ||
| 48 | else | 52 | else |
| 49 | # make a new fresh page with node reference | 53 | self.pages.create! :user_id => user.id |
| 50 | draft = Page.create( head.attributes ) | ||
| 51 | end | 54 | end |
| 52 | |||
| 53 | draft | ||
| 54 | end | 55 | end |
| 55 | 56 | ||
| 56 | def publish_draft! | 57 | def publish_draft! |
| 57 | self.head = self.draft | 58 | if self.draft |
| 58 | self.save! | 59 | self.head = self.draft |
| 59 | 60 | self.save! | |
| 60 | self.head.published_at = Time.now | 61 | |
| 61 | self.head.save! | 62 | self.head.published_at = Time.now |
| 63 | self.head.save! | ||
| 64 | else | ||
| 65 | nil | ||
| 66 | end | ||
| 62 | end | 67 | end |
| 63 | 68 | ||
| 64 | # returns an array with all parts of a unique_name rather than a string | 69 | # returns an array with all parts of a unique_name rather than a string |
