summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/node.rb51
-rw-r--r--app/models/occurrence.rb2
-rw-r--r--app/models/page.rb6
3 files changed, 37 insertions, 22 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
diff --git a/app/models/occurrence.rb b/app/models/occurrence.rb
index 0760d5e..8457ffd 100644
--- a/app/models/occurrence.rb
+++ b/app/models/occurrence.rb
@@ -26,7 +26,7 @@ class Occurrence < ActiveRecord::Base
26 # variables are set to save repetitive queries. The occurrences of the given 26 # variables are set to save repetitive queries. The occurrences of the given
27 # event are then calculated and created. 27 # event are then calculated and created.
28 def self.generate event 28 def self.generate event
29 self.delete_all(:event_id => event.id) 29 self.where(:event_id => event.id).delete_all
30 30
31 node = event.node 31 node = event.node
32 duration = (event.end_time - event.start_time) 32 duration = (event.end_time - event.start_time)
diff --git a/app/models/page.rb b/app/models/page.rb
index e2cbee5..93debf8 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -83,7 +83,7 @@ class Page < ActiveRecord::Base
83 # outdated_translations? for more information. 83 # outdated_translations? for more information.
84 # Takes :locale => <locale> and :delta_time => 12.hours as options 84 # Takes :locale => <locale> and :delta_time => 12.hours as options
85 def self.find_with_outdated_translations options = {} 85 def self.find_with_outdated_translations options = {}
86 Page.all(:include => :translations).select do |page| 86 Page.includes(:translations).select do |page|
87 page.outdated_translations? options 87 page.outdated_translations? options
88 end 88 end
89 end 89 end
@@ -182,8 +182,8 @@ class Page < ActiveRecord::Base
182 182
183 translations = self.translations 183 translations = self.translations
184 184
185 default = *(translations.select {|x| x.locale == I18n.default_locale}) 185 default = translations.find {|x| x.locale.to_s == I18n.default_locale.to_s }
186 custom = *(translations.select {|x| x.locale == options[:locale]}) 186 custom = translations.find {|x| x.locale.to_s == options[:locale].to_s }
187 187
188 if translations.size > 1 && default && custom 188 if translations.size > 1 && default && custom
189 difference = (default.updated_at - custom.updated_at).to_i.abs 189 difference = (default.updated_at - custom.updated_at).to_i.abs