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

53 lines
1.4 KiB
Ruby

# frozen_string_literal: true
class SidebarSection < ActiveRecord::Base
MAX_TITLE_LENGTH = 30
belongs_to :user
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
has_many :sidebar_urls,
through: :sidebar_section_links,
source: :linkable,
source_type: "SidebarUrl"
accepts_nested_attributes_for :sidebar_urls, allow_destroy: true
before_save :set_system_user_for_public_section
validates :title,
presence: true,
uniqueness: {
scope: %i[user_id],
},
length: {
maximum: MAX_TITLE_LENGTH,
}
scope :public_sections, -> { where("public") }
enum :section_type, { community: 0 }, scopes: false, suffix: true
private
def set_system_user_for_public_section
self.user_id = Discourse.system_user.id if self.public
end
end
# == Schema Information
#
# Table name: sidebar_sections
#
# id :bigint not null, primary key
# user_id :integer not null
# title :string(30) not null
# created_at :datetime not null
# updated_at :datetime not null
# public :boolean default(FALSE), not null
# section_type :integer
#
# Indexes
#
# index_sidebar_sections_on_section_type (section_type) UNIQUE
# index_sidebar_sections_on_user_id_and_title (user_id,title) UNIQUE
#