2023-02-02 22:44:40 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SidebarUrl < ActiveRecord::Base
|
2023-05-22 19:53:32 -04:00
|
|
|
enum :segment, { primary: 0, secondary: 1 }, scopes: false, suffix: true
|
|
|
|
|
2023-03-26 22:03:16 -04:00
|
|
|
MAX_ICON_LENGTH = 40
|
|
|
|
MAX_NAME_LENGTH = 80
|
2023-08-17 00:46:24 -04:00
|
|
|
MAX_VALUE_LENGTH = 1000
|
2023-05-03 22:14:09 -04:00
|
|
|
COMMUNITY_SECTION_LINKS = [
|
2023-05-22 19:53:32 -04:00
|
|
|
{
|
2023-06-14 21:36:38 -04:00
|
|
|
name: "Topics",
|
2023-05-22 19:53:32 -04:00
|
|
|
path: "/latest",
|
|
|
|
icon: "layer-group",
|
|
|
|
segment: SidebarUrl.segments["primary"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "My Posts",
|
|
|
|
path: "/my/activity",
|
|
|
|
icon: "user",
|
|
|
|
segment: SidebarUrl.segments["primary"],
|
|
|
|
},
|
|
|
|
{ name: "Review", path: "/review", icon: "flag", segment: SidebarUrl.segments["primary"] },
|
|
|
|
{ name: "Admin", path: "/admin", icon: "wrench", segment: SidebarUrl.segments["primary"] },
|
2024-10-29 22:31:14 -04:00
|
|
|
{
|
|
|
|
name: "Invite",
|
|
|
|
path: "/new-invite",
|
|
|
|
icon: "paper-plane",
|
|
|
|
segment: SidebarUrl.segments["primary"],
|
|
|
|
},
|
2023-05-22 19:53:32 -04:00
|
|
|
{ name: "Users", path: "/u", icon: "users", segment: SidebarUrl.segments["secondary"] },
|
|
|
|
{
|
|
|
|
name: "About",
|
|
|
|
path: "/about",
|
2024-09-13 11:50:52 -04:00
|
|
|
icon: "circle-info",
|
2023-05-22 19:53:32 -04:00
|
|
|
segment: SidebarUrl.segments["secondary"],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FAQ",
|
|
|
|
path: "/faq",
|
2024-09-13 11:50:52 -04:00
|
|
|
icon: "circle-question",
|
2023-05-22 19:53:32 -04:00
|
|
|
segment: SidebarUrl.segments["secondary"],
|
|
|
|
},
|
2024-09-13 11:50:52 -04:00
|
|
|
{ name: "Groups", path: "/g", icon: "user-group", segment: SidebarUrl.segments["secondary"] },
|
2023-05-22 19:53:32 -04:00
|
|
|
{
|
|
|
|
name: "Badges",
|
|
|
|
path: "/badges",
|
|
|
|
icon: "certificate",
|
|
|
|
segment: SidebarUrl.segments["secondary"],
|
|
|
|
},
|
2023-05-03 22:14:09 -04:00
|
|
|
]
|
2023-03-21 00:58:42 -04:00
|
|
|
|
2023-03-26 22:03:16 -04:00
|
|
|
validates :icon, presence: true, length: { maximum: MAX_ICON_LENGTH }
|
|
|
|
validates :name, presence: true, length: { maximum: MAX_NAME_LENGTH }
|
|
|
|
validates :value, presence: true, length: { maximum: MAX_VALUE_LENGTH }
|
2023-02-07 19:45:34 -05:00
|
|
|
|
2023-02-02 22:44:40 -05:00
|
|
|
validate :path_validator
|
|
|
|
|
2023-03-08 18:44:54 -05:00
|
|
|
before_validation :remove_internal_hostname, :set_external
|
2023-03-06 19:47:18 -05:00
|
|
|
|
2023-02-02 22:44:40 -05:00
|
|
|
def path_validator
|
2023-03-31 01:26:56 -04:00
|
|
|
return true if !external?
|
|
|
|
raise ActionController::RoutingError.new("Not Found") if value !~ Discourse::Utils::URI_REGEXP
|
2023-02-02 22:44:40 -05:00
|
|
|
rescue ActionController::RoutingError
|
|
|
|
errors.add(
|
|
|
|
:value,
|
|
|
|
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
|
|
|
|
)
|
|
|
|
end
|
2023-03-06 19:47:18 -05: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-02 22:44:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: sidebar_urls
|
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2023-02-07 19:45:34 -05:00
|
|
|
# name :string(80) not null
|
2023-08-17 00:46:24 -04:00
|
|
|
# value :string(1000) not null
|
2023-02-02 22:44:40 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2023-02-07 19:45:34 -05:00
|
|
|
# icon :string(40) not null
|
2023-03-06 19:47:18 -05:00
|
|
|
# external :boolean default(FALSE), not null
|
2023-05-03 22:14:09 -04:00
|
|
|
# segment :integer default("primary"), not null
|
2023-02-02 22:44:40 -05:00
|
|
|
#
|