2020-09-25 21:02:34 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
2022-05-16 08:29:59 +00:00
|
|
|
# Table name: portals
|
2020-09-25 21:02:34 +00:00
|
|
|
#
|
|
|
|
# id :bigint not null, primary key
|
2022-05-16 08:29:59 +00:00
|
|
|
# archived :boolean default(FALSE)
|
2020-09-25 21:02:34 +00:00
|
|
|
# color :string
|
2022-04-20 10:30:37 +00:00
|
|
|
# config :jsonb
|
2020-09-25 21:02:34 +00:00
|
|
|
# custom_domain :string
|
|
|
|
# header_text :text
|
|
|
|
# homepage_link :string
|
|
|
|
# name :string not null
|
|
|
|
# page_title :string
|
|
|
|
# slug :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2022-04-20 10:30:37 +00:00
|
|
|
# account_id :integer not null
|
2020-09-25 21:02:34 +00:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2022-09-20 00:36:01 +00:00
|
|
|
# index_portals_on_custom_domain (custom_domain) UNIQUE
|
|
|
|
# index_portals_on_slug (slug) UNIQUE
|
2020-09-25 21:02:34 +00:00
|
|
|
#
|
2022-05-16 08:29:59 +00:00
|
|
|
class Portal < ApplicationRecord
|
2022-07-11 07:13:24 +00:00
|
|
|
include Rails.application.routes.url_helpers
|
|
|
|
|
2020-09-25 21:02:34 +00:00
|
|
|
belongs_to :account
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :categories, dependent: :destroy_async
|
2020-09-25 21:02:34 +00:00
|
|
|
has_many :folders, through: :categories
|
2021-11-18 05:02:29 +00:00
|
|
|
has_many :articles, dependent: :destroy_async
|
2022-07-04 14:59:44 +00:00
|
|
|
has_many :portal_members,
|
|
|
|
class_name: :PortalMember,
|
|
|
|
dependent: :destroy_async
|
|
|
|
has_many :members,
|
|
|
|
through: :portal_members,
|
|
|
|
class_name: :User,
|
|
|
|
dependent: :nullify,
|
|
|
|
source: :user
|
2022-07-11 07:13:24 +00:00
|
|
|
has_one_attached :logo
|
2020-09-25 21:02:34 +00:00
|
|
|
|
2022-09-22 21:08:48 +00:00
|
|
|
before_validation -> { normalize_empty_string_to_nil(%i[custom_domain homepage_link]) }
|
2020-09-25 21:02:34 +00:00
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :name, presence: true
|
2022-04-20 10:30:37 +00:00
|
|
|
validates :slug, presence: true, uniqueness: true
|
2022-09-20 00:36:01 +00:00
|
|
|
validates :custom_domain, uniqueness: true, allow_nil: true
|
2022-07-11 07:13:24 +00:00
|
|
|
validate :config_json_format
|
2022-07-04 14:59:44 +00:00
|
|
|
|
|
|
|
accepts_nested_attributes_for :members
|
2022-07-05 11:45:38 +00:00
|
|
|
|
|
|
|
scope :active, -> { where(archived: false) }
|
2022-07-11 07:13:24 +00:00
|
|
|
|
2022-07-28 08:29:16 +00:00
|
|
|
CONFIG_JSON_KEYS = %w[allowed_locales default_locale].freeze
|
2022-07-11 07:13:24 +00:00
|
|
|
|
|
|
|
def file_base_data
|
|
|
|
{
|
|
|
|
id: logo.id,
|
|
|
|
portal_id: id,
|
|
|
|
file_type: logo.content_type,
|
|
|
|
account_id: account_id,
|
|
|
|
file_url: url_for(logo),
|
|
|
|
blob_id: logo.blob_id,
|
|
|
|
filename: logo.filename.to_s
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2022-07-28 08:29:16 +00:00
|
|
|
def default_locale
|
|
|
|
config['default_locale'] || 'en'
|
|
|
|
end
|
|
|
|
|
2022-07-11 07:13:24 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def config_json_format
|
2022-09-01 11:14:06 +00:00
|
|
|
config['default_locale'] = default_locale
|
2022-07-11 07:13:24 +00:00
|
|
|
denied_keys = config.keys - CONFIG_JSON_KEYS
|
|
|
|
errors.add(:cofig, "in portal on #{denied_keys.join(',')} is not supported.") if denied_keys.any?
|
|
|
|
end
|
2020-09-25 21:02:34 +00:00
|
|
|
end
|