MishkaCmsCore. Runtime. Resources. Changes. NameFromTitle
(Mishka CMS Core v0.0.1)
Copy Markdown
View Source
Names a page after its title when the writer did not name it.
A page has two words for itself. title is what it is called, in the language it is written in,
and it is never slugified. name is the key unique_name_per_site is keyed on — it is not in any
URL, path is — so nobody reads it and nobody should have to type it.
Only on create
Renaming a page's title does not rename its key. A name derived on every save would be
re-slugified on every save, and the row's identity would move under an author who only fixed a
typo. So this runs in the create actions alone, and an update leaves name exactly as written.
What it derives from
title first, then path for a title that slugifies to nothing (a title of emoji, or of a script
Slug does not transliterate), then "page". A Persian title lands on its
transliteration — قیمتگذاری becomes qymtgdhry — and that is fine for a key nobody sees.
The suffix, and where it costs a query
Two pages on one site may be called the same thing. The first free member of base, base-2,
base-3 … is taken, measured against the names the site already holds, so the second
قیمتگذاری is qymtgdhry-2 rather than a constraint error naming a field the author never saw.
That measurement is the only part that reads the database, and it is deferred to a before_action
so it runs once per WRITE rather than once per changeset. AshPhoenix.Form builds a changeset on
every phx-change, so doing it inline would put a SELECT behind every keystroke in the Title
box. The hook is attached only when this module supplied the name — a caller who passed one never
pays for it.
create :create do
change MishkaCmsCore.Runtime.Resources.Changes.NameFromTitle
end