diff options
| author | hukl <contact@smyck.org> | 2009-03-22 17:14:08 +0100 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-03-22 17:14:08 +0100 |
| commit | 06ec666fa8fad0aafe7d8e505f6e92b729fccbce (patch) | |
| tree | 8ae948c50ffd9ebcf264f1b02dcea150c6eb96e1 /app/models | |
| parent | c2f581ac675a869c2f0600c738332c0674a3606c (diff) | |
Finally! The cloning of pages for creating new drafts is now a lot cleaner. I had to search for a while to figure out a better way. Thank you svenfuchs and joshmh for the support!
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/node.rb | 25 | ||||
| -rw-r--r-- | app/models/page.rb | 24 |
2 files changed, 28 insertions, 21 deletions
diff --git a/app/models/node.rb b/app/models/node.rb index 0f34b73..04d98b5 100644 --- a/app/models/node.rb +++ b/app/models/node.rb | |||
| @@ -11,7 +11,8 @@ class Node < ActiveRecord::Base | |||
| 11 | belongs_to :user, :foreign_key => :locking_user_id | 11 | belongs_to :user, :foreign_key => :locking_user_id |
| 12 | 12 | ||
| 13 | # Callbacks | 13 | # Callbacks |
| 14 | after_create :initialize_empty_page | 14 | after_create :initialize_empty_page |
| 15 | before_save :check_for_changed_slug | ||
| 15 | 16 | ||
| 16 | # Validations | 17 | # Validations |
| 17 | # validates_length_of :slug, :within => 3..40 | 18 | # validates_length_of :slug, :within => 3..40 |
| @@ -59,7 +60,7 @@ class Node < ActiveRecord::Base | |||
| 59 | end | 60 | end |
| 60 | 61 | ||
| 61 | def create_new_draft user | 62 | def create_new_draft user |
| 62 | empty_page = self.pages.new | 63 | empty_page = self.pages.create |
| 63 | empty_page.user = user | 64 | empty_page.user = user |
| 64 | empty_page.clone_attributes_from self.head | 65 | empty_page.clone_attributes_from self.head |
| 65 | 66 | ||
| @@ -96,17 +97,17 @@ class Node < ActiveRecord::Base | |||
| 96 | self.save | 97 | self.save |
| 97 | end | 98 | end |
| 98 | 99 | ||
| 100 | def unlock! | ||
| 101 | self.user = nil | ||
| 102 | self.save | ||
| 103 | end | ||
| 104 | |||
| 99 | protected | 105 | protected |
| 100 | def lock_for! current_user | 106 | def lock_for! current_user |
| 101 | self.user = current_user | 107 | self.user = current_user |
| 102 | self.save | 108 | self.save |
| 103 | end | 109 | end |
| 104 | 110 | ||
| 105 | def unlock! | ||
| 106 | self.user = nil | ||
| 107 | self.save | ||
| 108 | end | ||
| 109 | |||
| 110 | # Creates an empty page and associates it to the given node. This means | 111 | # Creates an empty page and associates it to the given node. This means |
| 111 | # freshly created node has an empty draft. A user can create nodes as he | 112 | # freshly created node has an empty draft. A user can create nodes as he |
| 112 | # wants to which will not appear on the public page until the author edits | 113 | # wants to which will not appear on the public page until the author edits |
| @@ -117,6 +118,16 @@ class Node < ActiveRecord::Base | |||
| 117 | self.save | 118 | self.save |
| 118 | end | 119 | end |
| 119 | end | 120 | end |
| 121 | |||
| 122 | def check_for_changed_slug | ||
| 123 | if parent and changed.include? "slug" | ||
| 124 | self.update_unique_name | ||
| 125 | |||
| 126 | if tmp_descendants = descendants | ||
| 127 | tmp_descendants.each { |descendant| descendant.update_unique_name } | ||
| 128 | end | ||
| 129 | end | ||
| 130 | end | ||
| 120 | end | 131 | end |
| 121 | 132 | ||
| 122 | 133 | ||
diff --git a/app/models/page.rb b/app/models/page.rb index 4c4536e..58f9682 100644 --- a/app/models/page.rb +++ b/app/models/page.rb | |||
| @@ -87,27 +87,23 @@ class Page < ActiveRecord::Base | |||
| 87 | 87 | ||
| 88 | def clone_attributes_from page | 88 | def clone_attributes_from page |
| 89 | return nil unless page | 89 | return nil unless page |
| 90 | 90 | ||
| 91 | self.reload | ||
| 92 | |||
| 91 | # Clone untranslated attributes | 93 | # Clone untranslated attributes |
| 92 | |||
| 93 | self.tag_list = page.tag_list.join(", ") | 94 | self.tag_list = page.tag_list.join(", ") |
| 94 | self.template_name = page.template_name | 95 | self.template_name = page.template_name |
| 95 | self.published_at = page.published_at | 96 | self.published_at = page.published_at |
| 96 | 97 | ||
| 97 | # Clone translated attributes | 98 | # Getting rid of the auto-generated empty translations |
| 99 | self.globalize_translations.delete_all | ||
| 98 | 100 | ||
| 99 | locale_before = I18n.locale | 101 | # Clone translated attributes |
| 100 | 102 | page.globalize_translations.each do |translation| | |
| 101 | I18n.available_locales.each do |l| | 103 | self.globalize_translations.create!(translation.attributes) |
| 102 | next if l == :root | ||
| 103 | I18n.locale = l | ||
| 104 | page.reload | ||
| 105 | self.title = page.title unless page.title.try(:fallback?) | ||
| 106 | self.abstract = page.abstract unless page.abstract.try(:fallback?) | ||
| 107 | self.body = page.body unless page.body.try(:fallback?) | ||
| 108 | end | 104 | end |
| 109 | 105 | ||
| 110 | I18n.locale = locale_before | 106 | self.save |
| 111 | end | 107 | end |
| 112 | 108 | ||
| 113 | def public? | 109 | def public? |
