Skip to content

Commit

Permalink
Simplify urlname generating
Browse files Browse the repository at this point in the history
There is no need to rebuild every ancestors slug since we already have the parents urlname we can append the slug to.
  • Loading branch information
tvdeyen committed Jun 3, 2020
1 parent 1bb2b2c commit 46fd607
Showing 1 changed file with 11 additions and 33 deletions.
44 changes: 11 additions & 33 deletions app/models/alchemy/page/page_naming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit 46fd607

Please sign in to comment.