mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-14 07:40:37 +08:00
`.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.
86 lines
2.2 KiB
Ruby
86 lines
2.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class EmbeddableHost < ActiveRecord::Base
|
|
validate :host_must_be_valid
|
|
belongs_to :category
|
|
belongs_to :user, optional: true
|
|
has_many :embeddable_host_tags
|
|
has_many :tags, through: :embeddable_host_tags
|
|
after_destroy :reset_embedding_settings
|
|
|
|
before_validation do
|
|
self.host.sub!(%r{\Ahttps?://}, "")
|
|
self.host.sub!(%r{/.*\z}, "")
|
|
end
|
|
|
|
def self.record_for_url(uri)
|
|
if uri.is_a?(String)
|
|
uri =
|
|
begin
|
|
URI(UrlHelper.normalized_encode(uri))
|
|
rescue URI::Error, Addressable::URI::InvalidURIError
|
|
end
|
|
end
|
|
|
|
return false if uri.blank?
|
|
|
|
host = uri.host
|
|
return false if host.blank?
|
|
|
|
host << ":#{uri.port}" if uri.port.present? && uri.port != 80 && uri.port != 443
|
|
|
|
path = uri.path
|
|
path << "?" << uri.query if uri.query.present?
|
|
|
|
where("lower(host) = ?", host).each do |eh|
|
|
return eh if eh.allowed_paths.blank?
|
|
|
|
path_regexp = Regexp.new(eh.allowed_paths)
|
|
return eh if path_regexp.match(path) || path_regexp.match(UrlHelper.unencode(path))
|
|
end
|
|
|
|
nil
|
|
end
|
|
|
|
def self.url_allowed?(url)
|
|
return false if url.nil?
|
|
|
|
uri =
|
|
begin
|
|
URI(UrlHelper.normalized_encode(url))
|
|
rescue URI::Error
|
|
end
|
|
|
|
uri.present? && record_for_url(uri).present?
|
|
end
|
|
|
|
private
|
|
|
|
def reset_embedding_settings
|
|
unless EmbeddableHost.exists?
|
|
Embedding.settings.each { |s| SiteSetting.set(s.to_s, SiteSetting.defaults[s]) }
|
|
end
|
|
end
|
|
|
|
def host_must_be_valid
|
|
if host !~ /\A[a-z0-9]+([\-\.]+{1}[a-z0-9]+)*\.[a-z]{2,24}(:[0-9]{1,5})?(\/.*)?\Z/i &&
|
|
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(:[0-9]{1,5})?(\/.*)?\Z/ &&
|
|
host !~ /\A([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.)?localhost(\:[0-9]{1,5})?(\/.*)?\Z/i
|
|
errors.add(:host, I18n.t("errors.messages.invalid"))
|
|
end
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: embeddable_hosts
|
|
#
|
|
# id :integer not null, primary key
|
|
# allowed_paths :string
|
|
# class_name :string
|
|
# host :string not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# category_id :integer not null
|
|
# user_id :integer
|
|
#
|