2020-09-25 21:02:34 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
2022-05-16 08:29:59 +00:00
|
|
|
# Table name: categories
|
2020-09-25 21:02:34 +00:00
|
|
|
#
|
2022-06-28 05:53:20 +00:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# description :text
|
|
|
|
# locale :string default("en")
|
|
|
|
# name :string
|
|
|
|
# position :integer
|
|
|
|
# slug :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# linked_category_id :bigint
|
|
|
|
# parent_category_id :bigint
|
|
|
|
# portal_id :integer not null
|
2020-09-25 21:02:34 +00:00
|
|
|
#
|
2022-04-20 10:30:37 +00:00
|
|
|
# Indexes
|
|
|
|
#
|
2022-06-28 05:53:20 +00:00
|
|
|
# index_categories_on_linked_category_id (linked_category_id)
|
2022-06-13 10:26:49 +00:00
|
|
|
# index_categories_on_locale (locale)
|
|
|
|
# index_categories_on_locale_and_account_id (locale,account_id)
|
2022-06-28 05:53:20 +00:00
|
|
|
# index_categories_on_parent_category_id (parent_category_id)
|
2022-06-13 10:26:49 +00:00
|
|
|
# index_categories_on_slug_and_locale_and_portal_id (slug,locale,portal_id) UNIQUE
|
2022-04-20 10:30:37 +00:00
|
|
|
#
|
2022-06-28 05:53:20 +00:00
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (linked_category_id => categories.id)
|
|
|
|
# fk_rails_... (parent_category_id => categories.id)
|
|
|
|
#
|
2022-05-16 08:29:59 +00:00
|
|
|
class Category < ApplicationRecord
|
2020-09-25 21:02:34 +00:00
|
|
|
belongs_to :account
|
|
|
|
belongs_to :portal
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :folders, dependent: :destroy_async
|
2020-09-25 21:02:34 +00:00
|
|
|
has_many :articles, dependent: :nullify
|
2022-06-28 05:53:20 +00:00
|
|
|
has_many :category_related_categories,
|
|
|
|
class_name: :RelatedCategory,
|
|
|
|
dependent: :destroy_async
|
|
|
|
has_many :related_categories,
|
|
|
|
through: :category_related_categories,
|
|
|
|
class_name: :Category,
|
|
|
|
dependent: :nullify
|
|
|
|
has_many :sub_categories,
|
|
|
|
class_name: :Category,
|
|
|
|
foreign_key: :parent_category_id,
|
|
|
|
dependent: :nullify,
|
|
|
|
inverse_of: 'parent_category'
|
|
|
|
has_many :linked_categories,
|
|
|
|
class_name: :Category,
|
|
|
|
foreign_key: :linked_category_id,
|
|
|
|
dependent: :nullify,
|
|
|
|
inverse_of: 'linked_category'
|
|
|
|
belongs_to :parent_category, class_name: :Category, optional: true
|
|
|
|
belongs_to :linked_category, class_name: :Category, optional: true
|
2020-09-25 21:02:34 +00:00
|
|
|
|
|
|
|
before_validation :ensure_account_id
|
|
|
|
validates :account_id, presence: true
|
2022-06-13 10:26:49 +00:00
|
|
|
validates :slug, presence: true
|
2020-09-25 21:02:34 +00:00
|
|
|
validates :name, presence: true
|
2022-06-13 10:26:49 +00:00
|
|
|
validates :locale, uniqueness: { scope: %i[slug portal_id],
|
|
|
|
message: 'should be unique in the category and portal' }
|
2022-06-28 05:53:20 +00:00
|
|
|
accepts_nested_attributes_for :related_categories
|
2022-06-13 10:26:49 +00:00
|
|
|
|
|
|
|
scope :search_by_locale, ->(locale) { where(locale: locale) if locale.present? }
|
|
|
|
|
|
|
|
def self.search(params)
|
|
|
|
search_by_locale(params[:locale]).page(current_page(params)).order(position: :asc)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.current_page(params)
|
|
|
|
params[:page] || 1
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ensure_account_id
|
|
|
|
self.account_id = portal&.account_id
|
|
|
|
end
|
|
|
|
end
|