Merge pull request #631 from DonovanDMC/wikipages-normalize-title

[WikiPages] normalize title in show_or_new & edit
This commit is contained in:
Earlopain 2024-04-09 19:56:16 +02:00 committed by GitHub
commit 40ff649250
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 10 additions and 12 deletions

View File

@ -15,7 +15,7 @@ class WikiPagesController < ApplicationController
if params[:id] =~ /\A\d+\z/
@wiki_page = WikiPage.find(params[:id])
else
@wiki_page = WikiPage.find_by_title(params[:id])
@wiki_page = WikiPage.titled(params[:id])
if @wiki_page.nil? && request.format.symbol == :html
redirect_to show_or_new_wiki_pages_path(:title => params[:id])
return
@ -51,7 +51,7 @@ class WikiPagesController < ApplicationController
if params[:id] =~ /\A\d+\z/
@wiki_page = WikiPage.find(params[:id])
else
@wiki_page = WikiPage.titled(params[:id]).first
@wiki_page = WikiPage.titled(params[:id])
end
if @wiki_page.present?
@ -96,7 +96,7 @@ class WikiPagesController < ApplicationController
end
def show_or_new
@wiki_page = WikiPage.find_by_title(params[:title])
@wiki_page = WikiPage.titled(params[:title])
if @wiki_page
redirect_to wiki_page_path(@wiki_page)
else

View File

@ -55,6 +55,6 @@ class RelatedTagQuery
end
def wiki_page
WikiPage.titled(query).first
WikiPage.titled(query)
end
end

View File

@ -351,9 +351,9 @@ class Artist < ApplicationRecord
end
def update_wiki
if persisted? && saved_change_to_name? && attribute_before_last_save("name").present? && WikiPage.titled(attribute_before_last_save("name")).exists?
if persisted? && saved_change_to_name? && attribute_before_last_save("name").present? && WikiPage.titled(attribute_before_last_save("name"))
# we're renaming the artist, so rename the corresponding wiki page
old_page = WikiPage.titled(name_before_last_save).first
old_page = WikiPage.titled(name_before_last_save)
if wiki_page.nil?
# a wiki page doesn't already exist for the new name, so rename the old one
old_page.update(title: name, body: @notes || old_page.body)

View File

@ -50,7 +50,7 @@ class WikiPage < ApplicationRecord
module SearchMethods
def titled(title)
where("title = ?", title.downcase.tr(" ", "_"))
find_by(title: title.downcase.tr(" ", "_"))
end
def active

View File

@ -6,7 +6,7 @@
<div class="dtext-container">
<%= format_text(WikiPage.titled(Danbooru.config.flag_notice_wiki_page).first.try(&:body)) %>
<%= format_text(WikiPage.titled(Danbooru.config.flag_notice_wiki_page)&.body) %>
</div>
</div>

View File

@ -1,6 +1,6 @@
<div class="replace-image-dialog-body">
<div class="dtext-container">
<%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page).first.try(&:body)) %>
<%= format_text(WikiPage.titled(Danbooru.config.replacement_notice_wiki_page)&.body) %>
</div>
<div>

View File

@ -60,9 +60,7 @@ class WikiPageTest < ActiveSupport::TestCase
end
should "search by title" do
matches = WikiPage.titled("hot potato")
assert_equal(1, matches.count)
assert_equal("hot_potato", matches.first.title)
assert_equal("hot_potato", WikiPage.titled("hot potato").title)
end
should "search other names with wildcards" do