wiki pages: use names instead of ids in urls.

Switching to using wiki names in URLs instead of IDs:

* https://danbooru.donami.us/wiki_pages/vocaloid
* https://danbooru.donami.us/wiki_pages/hatsune_miku

ID numbers can still be used, but they redirect to the name instead:

* https://danbooru.donami.us/wiki_pages/11 (redirects to /wiki_pages/touhou).

Numeric tags are prefixed with '~' to distinguish them from IDs:

* https://danbooru.donami.us/wiki_pages/2019 (the wiki with id 2019)
* https://danbooru.donami.us/wiki_pages/~2019 (the wiki for the tag named 2019)

The tag names 'new' and 'search' are disallowed to prevent conflicts
with existing routes:

* https://danbooru.donami.us/wiki_pages/new
* https://danbooru.donami.us/wiki_pages/search
This commit is contained in:
evazion
2019-10-31 04:07:21 -05:00
parent 0ccfb3f5f6
commit 3a908f84bb
14 changed files with 76 additions and 38 deletions

View File

@@ -23,6 +23,14 @@ class WikiPage < ApplicationRecord
api_attributes including: [:category_name]
module SearchMethods
def find_by_id_or_title(id)
if id =~ /\A\d+\z/
[find_by_id(id), :id]
else
[find_by_title(normalize_title(id)), :title]
end
end
def titled(title)
where(title: normalize_title(title))
end
@@ -138,7 +146,7 @@ class WikiPage < ApplicationRecord
end
def self.normalize_title(title)
title.downcase.gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "")
title.downcase.delete_prefix("~").gsub(/[[:space:]]+/, "_").gsub(/__/, "_").gsub(/\A_|_\z/, "")
end
def normalize_title
@@ -235,4 +243,12 @@ class WikiPage < ApplicationRecord
def visible?
artist.blank? || !artist.is_banned? || CurrentUser.is_builder?
end
def to_param
if title =~ /\A\d+\z/
"~#{title}"
else
title
end
end
end