2023-02-03 11:44:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SidebarSection < ActiveRecord::Base
|
2023-03-27 10:03:16 +08:00
|
|
|
MAX_TITLE_LENGTH = 30
|
|
|
|
|
2023-02-03 11:44:40 +08:00
|
|
|
belongs_to :user
|
2023-03-21 09:23:28 +08:00
|
|
|
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
|
2023-05-25 09:10:32 +08:00
|
|
|
|
2023-02-03 11:44:40 +08:00
|
|
|
has_many :sidebar_urls,
|
|
|
|
through: :sidebar_section_links,
|
|
|
|
source: :linkable,
|
|
|
|
source_type: "SidebarUrl"
|
|
|
|
|
|
|
|
accepts_nested_attributes_for :sidebar_urls, allow_destroy: true
|
|
|
|
|
2023-04-05 08:52:18 +08:00
|
|
|
before_save :set_system_user_for_public_section
|
|
|
|
|
2023-03-27 10:03:16 +08:00
|
|
|
validates :title,
|
|
|
|
presence: true,
|
|
|
|
uniqueness: {
|
|
|
|
scope: %i[user_id],
|
|
|
|
},
|
|
|
|
length: {
|
|
|
|
maximum: MAX_TITLE_LENGTH,
|
|
|
|
}
|
2023-04-05 08:52:18 +08:00
|
|
|
|
2023-04-06 06:55:47 +08:00
|
|
|
scope :public_sections, -> { where("public") }
|
2023-05-04 10:14:09 +08:00
|
|
|
enum :section_type, { community: 0 }, scopes: false, suffix: true
|
2023-04-06 06:55:47 +08:00
|
|
|
|
2023-05-23 07:53:32 +08:00
|
|
|
def reset_community!
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
self.update!(title: "Community")
|
|
|
|
self.sidebar_section_links.destroy_all
|
|
|
|
community_urls =
|
|
|
|
SidebarUrl::COMMUNITY_SECTION_LINKS.map do |url_data|
|
|
|
|
"('#{url_data[:name]}', '#{url_data[:path]}', '#{url_data[:icon]}', '#{url_data[:segment]}', false, now(), now())"
|
|
|
|
end
|
|
|
|
|
|
|
|
result = DB.query <<~SQL
|
|
|
|
INSERT INTO sidebar_urls(name, value, icon, segment, external, created_at, updated_at)
|
|
|
|
VALUES #{community_urls.join(",")}
|
|
|
|
RETURNING sidebar_urls.id
|
|
|
|
SQL
|
|
|
|
|
|
|
|
sidebar_section_links =
|
|
|
|
result.map.with_index do |url, index|
|
|
|
|
"(-1, #{url.id}, 'SidebarUrl', #{self.id}, #{index}, now(), now())"
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.query <<~SQL
|
|
|
|
INSERT INTO sidebar_section_links(user_id, linkable_id, linkable_type, sidebar_section_id, position, created_at, updated_at)
|
|
|
|
VALUES #{sidebar_section_links.join(",")}
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-05 08:52:18 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_system_user_for_public_section
|
|
|
|
self.user_id = Discourse.system_user.id if self.public
|
|
|
|
end
|
2023-02-03 11:44:40 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: sidebar_sections
|
|
|
|
#
|
2023-05-04 10:14:09 +08:00
|
|
|
# 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
|
2023-02-03 11:44:40 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2023-05-04 10:14:09 +08:00
|
|
|
# index_sidebar_sections_on_section_type (section_type) UNIQUE
|
2023-02-03 11:44:40 +08:00
|
|
|
# index_sidebar_sections_on_user_id_and_title (user_id,title) UNIQUE
|
|
|
|
#
|