discourse/app/models/sidebar_section_link.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

59 lines
2 KiB
Ruby
Vendored

# frozen_string_literal: true
class SidebarSectionLink < ActiveRecord::Base
belongs_to :user
belongs_to :linkable, polymorphic: true
belongs_to :sidebar_section
validates :user_id, presence: true, uniqueness: { scope: %i[linkable_type linkable_id] }
validates :linkable_id, presence: true
validates :linkable_type, presence: true
validate :ensure_supported_linkable_type, if: :will_save_change_to_linkable_type?
SUPPORTED_LINKABLE_TYPES = %w[Category Tag SidebarUrl]
before_validation :inherit_user_id
before_create do
if self.user_id && self.sidebar_section
self.position = self.sidebar_section.sidebar_section_links.maximum(:position).to_i + 1
end
end
after_destroy { self.linkable.destroy! if self.linkable_type == "SidebarUrl" }
private
def inherit_user_id
self.user_id = sidebar_section.user_id if sidebar_section
end
def ensure_supported_linkable_type
if (!SUPPORTED_LINKABLE_TYPES.include?(self.linkable_type)) ||
(self.linkable_type == "Tag" && !SiteSetting.tagging_enabled)
self.errors.add(
:linkable_type,
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
)
end
end
end
# == Schema Information
#
# Table name: sidebar_section_links
#
# id :bigint not null, primary key
# linkable_type :string not null
# position :integer default(0), not null
# created_at :datetime not null
# updated_at :datetime not null
# linkable_id :integer not null
# sidebar_section_id :integer
# user_id :integer not null
#
# Indexes
#
# idx_sidebar_section_links_on_sidebar_section_id (sidebar_section_id,user_id,position) UNIQUE
# idx_unique_sidebar_section_links (user_id,linkable_type,linkable_id) UNIQUE
# index_sidebar_section_links_on_linkable_type_and_linkable_id (linkable_type,linkable_id)
#