2023-02-03 11:44:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SidebarUrl < ActiveRecord::Base
|
2023-02-08 08:45:34 +08:00
|
|
|
validates :icon, presence: true, length: { maximum: 40 }
|
|
|
|
validates :name, presence: true, length: { maximum: 80 }
|
|
|
|
validates :value, presence: true, length: { maximum: 200 }
|
|
|
|
|
2023-02-03 11:44:40 +08:00
|
|
|
validate :path_validator
|
|
|
|
|
2023-03-09 07:44:54 +08:00
|
|
|
before_validation :remove_internal_hostname, :set_external
|
2023-03-07 08:47:18 +08:00
|
|
|
|
2023-02-03 11:44:40 +08:00
|
|
|
def path_validator
|
2023-03-07 08:47:18 +08:00
|
|
|
if external?
|
|
|
|
raise ActionController::RoutingError if value !~ Discourse::Utils::URI_REGEXP
|
|
|
|
else
|
|
|
|
Rails.application.routes.recognize_path(value)
|
|
|
|
end
|
2023-02-03 11:44:40 +08:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
errors.add(
|
|
|
|
:value,
|
|
|
|
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
|
|
|
|
)
|
|
|
|
end
|
2023-03-07 08:47:18 +08:00
|
|
|
|
|
|
|
def remove_internal_hostname
|
|
|
|
self.value = self.value.sub(%r{\Ahttp(s)?://#{Discourse.current_hostname}}, "")
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_external
|
|
|
|
self.external = value.start_with?("http://", "https://")
|
|
|
|
end
|
2023-02-03 11:44:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: sidebar_urls
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2023-02-08 08:45:34 +08:00
|
|
|
# name :string(80) not null
|
|
|
|
# value :string(200) not null
|
2023-02-03 11:44:40 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2023-02-08 08:45:34 +08:00
|
|
|
# icon :string(40) not null
|
2023-03-07 08:47:18 +08:00
|
|
|
# external :boolean default(FALSE), not null
|
2023-02-03 11:44:40 +08:00
|
|
|
#
|