[Tags] Fix category for non-existant tags

Regressed in 4cd32c5f87
Caching nil as a category can cause problems in a bunch of places
This commit is contained in:
Earlopain 2023-07-31 21:36:20 +02:00
parent 814b722c95
commit 12e996dc08
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
4 changed files with 7 additions and 3 deletions

View File

@ -129,7 +129,7 @@ class Pool < ApplicationRecord
if name =~ /\A\d+\z/
name.to_i
else
Pool.where("lower(name) = ?", name.downcase.tr(" ", "_")).pick(:id)
Pool.where("lower(name) = ?", name.downcase.tr(" ", "_")).pick(:id).to_i
end
end

View File

@ -42,7 +42,7 @@ class PostSet < ApplicationRecord
if name =~ /\A\d+\z/
name.to_i
else
PostSet.where("lower(shortname) = ?", name.downcase.tr(" ", "_")).pick(:id)
PostSet.where("lower(shortname) = ?", name.downcase.tr(" ", "_")).pick(:id).to_i
end
end

View File

@ -119,7 +119,7 @@ class Tag < ApplicationRecord
def category_for(tag_name)
Cache.fetch("tc:#{tag_name}") do
Tag.where(name: tag_name).pick(:category)
Tag.where(name: tag_name).pick(:category).to_i
end
end

View File

@ -17,6 +17,10 @@ class TagTest < ActiveSupport::TestCase
assert_equal(Tag.categories.artist, Tag.category_for("!@ab"))
end
should "return general for a tag that doesn't exist" do
assert_equal(Tag.categories.general, Tag.category_for("missing"))
end
should "fetch for multiple tags" do
create(:artist_tag, name: "aaa")
create(:copyright_tag, name: "bbb")