summaryrefslogtreecommitdiff
path: root/app/models/node.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/node.rb')
-rw-r--r--app/models/node.rb51
1 files changed, 33 insertions, 18 deletions
diff --git a/app/models/node.rb b/app/models/node.rb
index 8be6daf..d760f0a 100644
--- a/app/models/node.rb
+++ b/app/models/node.rb
@@ -16,10 +16,10 @@ class Node < ActiveRecord::Base
16 after_save :update_unique_names_of_children 16 after_save :update_unique_names_of_children
17 17
18 # Validations 18 # Validations
19 validates_length_of :slug, :within => 1..255, :unless => "parent_id.nil?" 19 validates_length_of :slug, :within => 1..255, :unless => -> { parent_id.nil? }
20 validates_presence_of :slug, :unless => "parent_id.nil?" 20 validates_presence_of :slug, :unless => -> { parent_id.nil? }
21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => "parent_id.nil?" 21 validates_uniqueness_of :slug, :scope => :parent_id, :unless => -> { parent_id.nil? }
22 validates_presence_of :parent_id, :unless => "Node.root.nil?" 22 validates_presence_of :parent_id, :unless => -> { Node.root.nil? }
23 23
24 validate :borders # This should never ever happen. 24 validate :borders # This should never ever happen.
25 25
@@ -95,26 +95,37 @@ class Node < ActiveRecord::Base
95 end 95 end
96 96
97 def publish_draft! 97 def publish_draft!
98 # Return nil if nothing to publish and no staged changes
99 return nil unless self.draft || staged_slug || staged_parent_id
100
98 if self.draft 101 if self.draft
99 self.head = self.draft 102 self.head = self.draft
100 self.head.published_at ||= Time.now 103 self.head.published_at ||= Time.now
101 self.head.save! 104 self.head.save!
102
103 self.draft = nil 105 self.draft = nil
106 end
104 107
105 if staged_slug && (staged_slug != slug) 108 if staged_slug && (staged_slug != slug)
106 self.slug = staged_slug 109 self.slug = staged_slug
107 end 110 self.staged_slug = nil
108 111 end
109 if staged_parent_id && (staged_parent_id != parent_id)
110 self.parent_id = staged_parent_id
111 end
112 112
113 if staged_parent_id && (staged_parent_id != parent_id)
114 new_parent = Node.find(staged_parent_id)
115 self.staged_parent_id = nil
113 self.save! 116 self.save!
114 self.update_unique_name 117 self.move_to_child_of(new_parent)
115 self.unlock! 118 else
116 self 119 unless self.save
120 raise ActiveRecord::RecordInvalid.new(self)
121 end
117 end 122 end
123
124 self.reload
125 self.update_unique_name
126 self.send(:update_unique_names_of_children)
127 self.unlock!
128 self
118 end 129 end
119 130
120 # removes a draft and the lock if it is older than a day and still 131 # removes a draft and the lock if it is older than a day and still
@@ -146,7 +157,7 @@ class Node < ActiveRecord::Base
146 157
147 # returns an array with all parts of a unique_name rather than a string 158 # returns an array with all parts of a unique_name rather than a string
148 def unique_path 159 def unique_path
149 unique_name.to_s 160 unique_name.to_s.split("/")
150 end 161 end
151 162
152 # returns array with pages up to root excluding root 163 # returns array with pages up to root excluding root
@@ -228,8 +239,12 @@ class Node < ActiveRecord::Base
228 # Hopefully until no childrens occur 239 # Hopefully until no childrens occur
229 def update_unique_names_of_children 240 def update_unique_names_of_children
230 unless root? 241 unless root?
231 self.descendants.each do |descendant| 242 # Use parent_id-based traversal instead of lft/rgt descendants
232 descendant.update_unique_name 243 # due to awesome_nested_set not refreshing parent lft/rgt in memory
244 Node.where(:parent_id => self.id).each do |child|
245 child.reload
246 child.update_unique_name
247 child.send(:update_unique_names_of_children)
233 end 248 end
234 end 249 end
235 end 250 end