discourse/app/models/sidebar_url.rb
Krzysztof Kotlarek 709fa24558
DEV: move sidebar community section to database (#21166)
* DEV: move sidebar community section to database

Before, community section was hard-coded. In the future, we are planning to allow admins to edit it. Therefore, it has to be moved to database to `custom_sections` table.

Few steps and simplifications has to be made:
- custom section was hidden behind `enable_custom_sidebar_sections` feature flag. It has to be deleted so all forums, see community section;
- migration to add `section_type` column to sidebar section to show it is a special type;
- migration to add `segment` column to sidebar links to determine if link should be displayed in primary section or in more section;
- simplify more section to have one level only (secondary section links are merged);
- ensure that links like `everything` are correctly tracking state;
- make user an anonymous links position consistence. For example, from now on `faq` link for user and anonymous is visible in more tab;
- delete old community-section template.
2023-05-04 12:14:09 +10:00

66 lines
2.4 KiB
Ruby

# frozen_string_literal: true
class SidebarUrl < ActiveRecord::Base
FULL_RELOAD_LINKS_REGEX = [%r{\A/my/[a-z_\-/]+\z}, %r{\A/safe-mode\z}]
MAX_ICON_LENGTH = 40
MAX_NAME_LENGTH = 80
MAX_VALUE_LENGTH = 200
COMMUNITY_SECTION_LINKS = [
{ name: "Everything", path: "/latest", icon: "layer-group", segment: "primary" },
{ name: "My Posts", path: "/my/activity", icon: "user", segment: "primary" },
{ name: "Review", path: "/review", icon: "flag", segment: "primary" },
{ name: "Admin", path: "/admin", icon: "wrench", segment: "primary" },
{ name: "Users", path: "/u", icon: "users", segment: "secondary" },
{ name: "About", path: "/about", icon: "info-circle", segment: "secondary" },
{ name: "FAQ", path: "/faq", icon: "question-circle", segment: "secondary" },
{ name: "Groups", path: "/g", icon: "user-friends", segment: "secondary" },
{ name: "Badges", path: "/badges", icon: "certificate", segment: "secondary" },
]
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 }
validate :path_validator
before_validation :remove_internal_hostname, :set_external
enum :segment, { primary: 0, secondary: 1 }, scopes: false, suffix: true
def path_validator
return true if !external?
raise ActionController::RoutingError.new("Not Found") if value !~ Discourse::Utils::URI_REGEXP
rescue ActionController::RoutingError
errors.add(
:value,
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
)
end
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
def full_reload?
FULL_RELOAD_LINKS_REGEX.any? { |regex| value =~ regex }
end
end
# == Schema Information
#
# Table name: sidebar_urls
#
# id :bigint not null, primary key
# name :string(80) not null
# value :string(200) not null
# created_at :datetime not null
# updated_at :datetime not null
# icon :string(40) not null
# external :boolean default(FALSE), not null
# segment :integer default("primary"), not null
#