From f4ddfff03ca9f25d50f39a1971877362d85eb9cb Mon Sep 17 00:00:00 2001 From: erdgeist Date: Fri, 7 Aug 2026 06:15:14 +0200 Subject: Move the pending address from the node onto the draft --- app/models/page.rb | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'app/models/page.rb') diff --git a/app/models/page.rb b/app/models/page.rb index 8313b1d4..4635f1b4 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -20,9 +20,13 @@ class Page < ApplicationRecord :inclusion => { :in => ->(_) { Page.custom_templates } }, :allow_blank => true, :if => :template_name_changed? + validates_format_of :slug, :with => /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/, + :unless => -> { slug.blank? } + validate :page_slug_not_reserved # Associations belongs_to :node, optional: true + belongs_to :parent_node, :class_name => "Node", :optional => true belongs_to :user, optional: true belongs_to :editor, :class_name => "User", optional: true has_many :related_assets, :dependent => :destroy @@ -202,6 +206,8 @@ class Page < ApplicationRecord page.translations.reload # Clone untranslated attributes + self.slug = page.slug + self.parent_node_id = page.parent_node_id self.tag_list = page.tag_list self.template_name ||= page.template_name self.published_at = page.published_at @@ -292,6 +298,20 @@ class Page < ApplicationRecord published_at.nil? ? true : published_at < Time.now end + # The address this page will have once published. + def prospective_unique_name + return nil if parent_node_id.nil? + + parent = Node.find_by(:id => parent_node_id) + return nil unless parent + + [parent.unique_name.presence, slug].compact.join("/") + end + + def parent_node_missing? + parent_node_id.present? && !Node.exists?(:id => parent_node_id) + end + def effective_lang if translated_locales.empty? return 'de' @@ -333,6 +353,14 @@ class Page < ApplicationRecord end end + def page_slug_not_reserved + return unless slug == CccConventions::TRASH_SLUG + return if node&.trash_node? + return unless parent_node_id && Node.find_by(:id => parent_node_id)&.root? + + errors.add(:slug, :reserved_for_trash) + end + # Installs (or re-installs) the trigger that keeps page_translations' # search_vector in sync. Idempotent, safe to call on every boot. # search_vector is populated by a raw Postgres trigger, not anything -- cgit v1.3