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

91 lines
2.6 KiB
Ruby

# frozen_string_literal: true
class Flag < ActiveRecord::Base
DEFAULT_VALID_APPLIES_TO = %w[Post Topic]
MAX_SYSTEM_FLAG_ID = 1000
MAX_NAME_LENGTH = 200
MAX_DESCRIPTION_LENGTH = 1000
scope :enabled, -> { where(enabled: true) }
scope :system, -> { where("id < 1000") }
scope :custom, -> { where("id >= 1000") }
before_save :set_position
before_save :set_name_key
after_commit { reset_flag_settings! if !skip_reset_flag_callback }
attr_accessor :skip_reset_flag_callback
default_scope do
order(:position).where(score_type: false).where.not(id: PostActionType::LIKE_POST_ACTION_ID)
end
def used?
PostAction.exists?(post_action_type_id: self.id) ||
ReviewableScore.exists?(reviewable_score_type: self.id)
end
def self.valid_applies_to_types
Set.new(DEFAULT_VALID_APPLIES_TO | DiscoursePluginRegistry.flag_applies_to_types)
end
def self.reset_flag_settings!
# Flags are cached in Redis for better performance. After the update,
# we need to reload them in all processes.
PostActionType.reload_types
end
def self.used_flag_ids(flag_ids)
sql =
flag_ids
.reduce([]) do |queries, flag_id|
queries << "(SELECT #{flag_id} FROM post_actions WHERE post_action_type_id = #{flag_id} LIMIT 1)"
queries << "(SELECT #{flag_id} FROM reviewable_scores WHERE reviewable_score_type = #{flag_id} LIMIT 1)"
queries
end
.join(" UNION ")
DB.query_single(sql)
end
def system?
self.id.present? && self.id < MAX_SYSTEM_FLAG_ID
end
def applies_to?(type)
self.applies_to.include?(type)
end
private
def reset_flag_settings!
self.class.reset_flag_settings!
end
def set_position
self.position = Flag.maximum(:position).to_i + 1 if !self.position
end
def set_name_key
prefix = self.system? ? "" : "custom_"
self.name_key = "#{prefix}#{self.name.squeeze(" ").gsub(" ", "_").gsub(/[^\w]/, "").downcase}"
end
end
# == Schema Information
#
# Table name: flags
#
# id :bigint not null, primary key
# applies_to :string not null, is an Array
# auto_action_type :boolean default(FALSE), not null
# description :text
# enabled :boolean default(TRUE), not null
# name :string
# name_key :string
# notify_type :boolean default(FALSE), not null
# position :integer not null
# require_message :boolean default(FALSE), not null
# score_type :boolean default(FALSE), not null
# created_at :datetime not null
# updated_at :datetime not null
#