2022-06-30 01:54:20 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SidebarSectionLink < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :linkable, polymorphic: true
|
2023-02-02 21:44:40 -06:00
|
|
|
belongs_to :sidebar_section
|
2022-06-30 01:54:20 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
validates :user_id, presence: true, uniqueness: { scope: %i[linkable_type linkable_id] }
|
2022-06-30 01:54:20 -05:00
|
|
|
validates :linkable_id, presence: true
|
|
|
|
validates :linkable_type, presence: true
|
|
|
|
validate :ensure_supported_linkable_type, if: :will_save_change_to_linkable_type?
|
|
|
|
|
2023-02-02 21:44:40 -06:00
|
|
|
SUPPORTED_LINKABLE_TYPES = %w[Category Tag SidebarUrl]
|
|
|
|
|
2023-04-04 19:52:18 -05:00
|
|
|
before_validation :inherit_user_id
|
2023-03-20 20:23:28 -05:00
|
|
|
before_create do
|
|
|
|
if self.user_id && self.sidebar_section
|
|
|
|
self.position = self.sidebar_section.sidebar_section_links.maximum(:position).to_i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-02 21:44:40 -06:00
|
|
|
after_destroy { self.linkable.destroy! if self.linkable_type == "SidebarUrl" }
|
2022-06-30 01:54:20 -05:00
|
|
|
|
2023-04-04 19:52:18 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def inherit_user_id
|
|
|
|
self.user_id = sidebar_section.user_id if sidebar_section
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_supported_linkable_type
|
2023-01-09 06:20:10 -06:00
|
|
|
if (!SUPPORTED_LINKABLE_TYPES.include?(self.linkable_type)) ||
|
|
|
|
(self.linkable_type == "Tag" && !SiteSetting.tagging_enabled)
|
|
|
|
self.errors.add(
|
|
|
|
:linkable_type,
|
|
|
|
I18n.t("activerecord.errors.models.sidebar_section_link.attributes.linkable_type.invalid"),
|
|
|
|
)
|
2022-06-30 01:54:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: sidebar_section_links
|
|
|
|
#
|
2023-02-02 21:44:40 -06:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# linkable_id :integer not null
|
|
|
|
# linkable_type :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# sidebar_section_id :integer
|
2023-03-20 20:23:28 -05:00
|
|
|
# position :integer default(0), not null
|
2022-06-30 01:54:20 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2023-03-27 23:13:44 -05:00
|
|
|
# idx_sidebar_section_links_on_sidebar_section_id (sidebar_section_id,user_id,position) UNIQUE
|
2023-02-09 18:14:22 -06:00
|
|
|
# idx_unique_sidebar_section_links (user_id,linkable_type,linkable_id) UNIQUE
|
|
|
|
# index_sidebar_section_links_on_linkable_type_and_linkable_id (linkable_type,linkable_id)
|
2022-06-30 01:54:20 -05:00
|
|
|
#
|