From 6424e10be5a89f175a74c71c55660412a169b8b8 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 23 Jun 2026 18:04:37 +0200 Subject: Update deployed state to what's currently running --- app/models/node.rb | 29 ++++++++++++++++++++++++----- app/models/page.rb | 15 ++++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) (limited to 'app/models') diff --git a/app/models/node.rb b/app/models/node.rb index 6c11fed..75122d1 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -3,11 +3,11 @@ class Node < ActiveRecord::Base acts_as_nested_set # Associations - has_many :pages, :order => "revision ASC" - belongs_to :head, :class_name => "Page", :foreign_key => :head_id - belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id - has_many :permissions - has_one :event + has_many :pages, :order => "revision ASC", :dependent => :destroy + belongs_to :head, :class_name => "Page", :foreign_key => :head_id, :dependent => :destroy + belongs_to :draft, :class_name => "Page", :foreign_key => :draft_id, :dependent => :destroy + has_many :permissions, :dependent => :destroy + has_one :event, :dependent => :destroy belongs_to :lock_owner, :class_name => "User", :foreign_key => :locking_user_id # Callbacks @@ -60,6 +60,7 @@ class Node < ActiveRecord::Base # Instance Methods def find_or_create_draft current_user + self.wipe_draft! if draft && self.lock_owner == current_user draft elsif draft && self.lock_owner.nil? @@ -115,6 +116,24 @@ class Node < ActiveRecord::Base end end + # removes a draft and the lock if it is older than a day and still + # identical to head + def wipe_draft! + unless self.draft + self.unlock! + return + end + return unless self.head + return unless self.draft.updated_at < 1.day.ago + return if Page.find(self.head).has_changes_to? self.draft + + self.draft.destroy + self.draft_id = nil + self.unlock! + self.save! + self.reload + end + def restore_revision! revision if page = self.pages.find_by_revision(revision) self.head = page diff --git a/app/models/page.rb b/app/models/page.rb index 0cfad53..f804353 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -64,7 +64,7 @@ class Page < ActiveRecord::Base Page.heads.paginate( find_options_for_find_tagged_with( - options[:tags].gsub(/\s/, ","), :match_all => true + options[:tags].gsub(/\s/, ","), :match_all => true, :conditions => options[:conditions] ).merge( :page => page, :per_page => options[:limit], @@ -94,6 +94,19 @@ class Page < ActiveRecord::Base end end + # Is used to compare a node's head with the node's draft + + def has_changes_to? draft + return true unless node == draft.node + return true unless assets == draft.assets + return true unless tag_list == draft.tag_list + return true unless template_name == draft.template_name + return true unless translated_locales.sort_by(&:to_s) == draft.translated_locales.sort_by(&:to_s) + changed = false + translated_locales.each { |locale| I18n.with_locale(locale) { changed = true unless title == draft.title && abstract == draft.abstract && body == draft.body } } + return changed + end + # Instance Methods def public_template_path -- cgit v1.3