discourse/app/models/sitemap.rb
David Taylor 3332e9f4c3
DEV: Always pass --force to annotaterb and reorder annotations (#39977)
`.annotaterb.yml` has carried `classified_sort: true` since the project
switched from `annotate` to `annotaterb` (commit 0eab7daea4, July 2025),
but annotaterb's default behaviour is to compare the existing schema
block against what it would generate and skip the rewrite when the
column list matches — even when the *ordering* of those columns differs.
The result is that models which haven't had a schema change since the
config landed never get reordered, and `classified_sort` drift
accumulates indefinitely.

`--force` makes annotaterb always rewrite, so a single `bin/rake
annotate:clean` run brings every model into the canonical format and
keeps them there. Every schema block is now grouped primary-key →
regular columns → timestamps → foreign keys (alphabetical within each
group). Pure annotation comment change — no code modifications.

Also cleans up the rake task to avoid string interpolation for `system`
calls.
2026-05-13 14:12:48 +01:00

82 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
class Sitemap < ActiveRecord::Base
RECENT_SITEMAP_NAME = "recent"
NEWS_SITEMAP_NAME = "news"
class << self
def regenerate_sitemaps
names_used = [RECENT_SITEMAP_NAME, NEWS_SITEMAP_NAME]
names_used.each { |name| touch(name) }
count = Category.where(read_restricted: false).sum(:topic_count)
max_page_size = SiteSetting.sitemap_page_size
size, mod = count.divmod(max_page_size)
size += 1 if mod > 0
size.times do |index|
page_name = (index + 1).to_s
touch(page_name)
names_used << page_name
end
where.not(name: names_used).update_all(enabled: false)
end
def touch(name)
find_or_initialize_by(name: name).tap do |sitemap|
sitemap.update!(last_posted_at: sitemap.last_posted_topic || 3.days.ago, enabled: true)
end
end
end
def topics
if name == RECENT_SITEMAP_NAME
sitemap_topics.pluck(:id, :slug, :bumped_at, :updated_at, :posts_count)
elsif name == NEWS_SITEMAP_NAME
sitemap_topics.pluck(:id, :title, :slug, :created_at)
else
sitemap_topics.pluck(:id, :slug, :bumped_at, :updated_at)
end
end
def last_posted_topic
sitemap_topics.maximum(:updated_at)
end
def max_page_size
SiteSetting.sitemap_page_size
end
private
def sitemap_topics
indexable_topics =
Topic.where(visible: true).joins(:category).where(categories: { read_restricted: false })
if name == RECENT_SITEMAP_NAME
indexable_topics.where("bumped_at > ?", 3.days.ago).order(bumped_at: :desc)
elsif name == NEWS_SITEMAP_NAME
indexable_topics.where("bumped_at > ?", 72.hours.ago).order(bumped_at: :desc)
else
offset = (name.to_i - 1) * max_page_size
indexable_topics.order(id: :asc).limit(max_page_size).offset(offset)
end
end
end
# == Schema Information
#
# Table name: sitemaps
#
# id :bigint not null, primary key
# enabled :boolean default(TRUE), not null
# last_posted_at :datetime not null
# name :string not null
#
# Indexes
#
# index_sitemaps_on_name (name) UNIQUE
#