From 46fd6075b3b502a28e554229444713c7414cf26d Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Thu, 4 Jun 2020 00:40:20 +0200 Subject: [PATCH] Simplify urlname generating There is no need to rebuild every ancestors slug since we already have the parents urlname we can append the slug to. --- app/models/alchemy/page/page_naming.rb | 44 +++++++------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/app/models/alchemy/page/page_naming.rb b/app/models/alchemy/page/page_naming.rb index 4e4d77e2a4..6c6f8f61b5 100644 --- a/app/models/alchemy/page/page_naming.rb +++ b/app/models/alchemy/page/page_naming.rb @@ -35,7 +35,7 @@ def renamed? # Makes a slug of all ancestors urlnames including mine and delimit them be slash. # So the whole path is stored as urlname in the database. def update_urlname! - new_urlname = nested_url_name(slug) + new_urlname = nested_url_name if urlname != new_urlname legacy_urls.create(urlname: urlname) update_column(:urlname, new_urlname) @@ -47,19 +47,6 @@ def slug urlname.to_s.split("/").last end - # Returns an array of non-language_root ancestors. - def non_root_ancestors - return [] unless parent - - if new_record? - parent.non_root_ancestors.tap do |base| - base.push(parent) unless parent.language_root? - end - else - ancestors.contentpages.where(language_root: nil).to_a - end - end - private def update_descendants_urlnames @@ -71,7 +58,7 @@ def update_descendants_urlnames # Either from name, or if present, from urlname. # The urlname contains the whole path including parent urlnames. def set_urlname - self[:urlname] = nested_url_name(slug) + self[:urlname] = nested_url_name end def set_title @@ -83,26 +70,17 @@ def set_title # Names shorter than 3 will be filled up with dashes, # so it does not collidate with the language code. # - def convert_url_name(value) - url_name = convert_to_urlname(value.blank? ? name : value) - if url_name.length < 3 - ("-" * (3 - url_name.length)) + url_name - else - url_name - end - end - - def nested_url_name(value) - (ancestor_slugs << convert_url_name(value)).join("/") + def converted_url_name + url_name = convert_to_urlname(slug.blank? ? name : slug) + url_name.rjust(3, "-") end - # Slugs of all non-language_root ancestors. - # Returns [], if there is no parent, the parent is - # the root page itself. - def ancestor_slugs - return [] if parent.nil? - - non_root_ancestors.map(&:slug).compact + def nested_url_name + if parent&.language_root? + converted_url_name + else + [parent&.urlname, converted_url_name].compact.join("/") + end end end end