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-07-08 11:54:38 +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
|
|
|
|
# associated_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-07-08 11:54:38 +00:00
|
|
|
# index_categories_on_associated_category_id (associated_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
|
|
|
|
#
|
2022-07-08 11:54:38 +00:00
|
|
|
# fk_rails_... (associated_category_id => categories.id)
|
2022-06-28 05:53:20 +00:00
|
|
|
# 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'
|
2022-07-08 11:54:38 +00:00
|
|
|
has_many :associated_categories,
|
2022-06-28 05:53:20 +00:00
|
|
|
class_name: :Category,
|
2022-07-08 11:54:38 +00:00
|
|
|
foreign_key: :associated_category_id,
|
2022-06-28 05:53:20 +00:00
|
|
|
dependent: :nullify,
|
2022-07-08 11:54:38 +00:00
|
|
|
inverse_of: 'root_category'
|
2022-06-28 05:53:20 +00:00
|
|
|
belongs_to :parent_category, class_name: :Category, optional: true
|
2022-07-08 11:54:38 +00:00
|
|
|
belongs_to :root_category,
|
|
|
|
class_name: :Category,
|
|
|
|
foreign_key: :associated_category_id,
|
|
|
|
inverse_of: :associated_categories,
|
|
|
|
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-07-11 07:13:24 +00:00
|
|
|
validate :allowed_locales
|
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
|
2022-07-11 07:13:24 +00:00
|
|
|
|
|
|
|
def allowed_locales
|
|
|
|
return if portal.blank?
|
|
|
|
|
|
|
|
allowed_locales = portal.config['allowed_locales']
|
|
|
|
|
|
|
|
return true if allowed_locales.include?(locale)
|
|
|
|
|
|
|
|
errors.add(:locale, "#{locale} of category is not part of portal's #{allowed_locales}.")
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|