Files
leadchat/app/models/portal.rb
Sojan Jose 2a90652f05 feat: Add draft status for help center locales (#13768)
This adds a draft status for Help Center locales so teams can prepare
localized content in the dashboard without exposing those locales in the
public portal switcher until they are ready to publish.

Fixes: https://github.com/chatwoot/chatwoot/issues/10412
Closes: https://github.com/chatwoot/chatwoot/issues/10412

## Why

Teams need a way to work on locale-specific Help Center content ahead of
launch. The public portal should only show ready locales, while the
admin dashboard should continue to expose every allowed locale for
ongoing article and category work.

## What this change does

- Adds `draft_locales` to portal config as a subset of `allowed_locales`
- Hides drafted locales from the public portal language switchers while
keeping direct locale URLs working
- Keeps drafted locales fully visible in the admin dashboard for article
and category management
- Adds locale actions to move an existing locale to draft, publish a
drafted locale, and keep the default locale protected from drafting
- Adds a status dropdown when creating a locale so new locales can be
created as `Published` or `Draft`
- Returns each admin locale with a `draft` flag so the locale UI can
reflect the public visibility state

## Validation

- Seed a portal with multiple locales, draft one locale, and confirm the
public portal switcher hides it while `/hc/:slug/:locale` still loads
directly
- In the admin dashboard, confirm drafted locales still appear in the
locale list and remain selectable for articles and categories
- Create a new locale with `Draft` status and confirm it stays out of
the public switcher until published
- Move an existing locale back and forth between `Published` and `Draft`
and confirm the public switcher updates accordingly


## Demo 



https://github.com/user-attachments/assets/ba22dc26-c2e7-463a-b1f5-adf1fda1f9be

---------

Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
2026-03-17 12:45:54 +04:00

135 lines
3.9 KiB
Ruby

# == Schema Information
#
# Table name: portals
#
# id :bigint not null, primary key
# archived :boolean default(FALSE)
# color :string
# config :jsonb
# custom_domain :string
# header_text :text
# homepage_link :string
# name :string not null
# page_title :string
# slug :string not null
# ssl_settings :jsonb not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# channel_web_widget_id :bigint
#
# Indexes
#
# index_portals_on_channel_web_widget_id (channel_web_widget_id)
# index_portals_on_custom_domain (custom_domain) UNIQUE
# index_portals_on_slug (slug) UNIQUE
#
class Portal < ApplicationRecord
include Rails.application.routes.url_helpers
DEFAULT_COLOR = '#1f93ff'.freeze
belongs_to :account
has_many :categories, dependent: :destroy_async
has_many :folders, through: :categories
has_many :articles, dependent: :destroy_async
has_one_attached :logo
has_many :inboxes, dependent: :nullify
belongs_to :channel_web_widget, class_name: 'Channel::WebWidget', optional: true
before_validation -> { normalize_empty_string_to_nil(%i[custom_domain homepage_link]) }
validates :account_id, presence: true
validates :name, presence: true
validates :slug, presence: true, uniqueness: true
validates :custom_domain, uniqueness: true, allow_nil: true
validate :config_json_format
scope :active, -> { where(archived: false) }
CONFIG_JSON_KEYS = %w[allowed_locales default_locale draft_locales website_token].freeze
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.signed_id,
filename: logo.filename.to_s
}
end
def default_locale
config_value('default_locale').presence || allowed_locale_codes.first || 'en'
end
def allowed_locale_codes
allowed_locale_codes = normalize_locale_codes(config_value('allowed_locales'))
return allowed_locale_codes if allowed_locale_codes.present?
[config_value('default_locale').presence || 'en']
end
def draft_locale_codes
allowed_locales = allowed_locale_codes
drafted_locales = normalize_locale_codes(drafted_locale_values)
allowed_locales.select { |locale| drafted_locales.include?(locale) }
end
def public_locale_codes
allowed_locale_codes - draft_locale_codes
end
def draft_locale?(locale)
draft_locale_codes.include?(locale)
end
def color
self[:color].presence || DEFAULT_COLOR
end
def display_title
page_title.presence || name
end
private
def config_json_format
self.config = (config || {}).deep_stringify_keys
config['allowed_locales'] = allowed_locale_codes
config['default_locale'] = default_locale
config['draft_locales'] = draft_locale_codes
denied_keys = config.keys - CONFIG_JSON_KEYS
errors.add(:cofig, "in portal on #{denied_keys.join(',')} is not supported.") if denied_keys.any?
errors.add(:config, 'default locale cannot be drafted.') if draft_locale?(default_locale)
end
def normalize_locale_codes(locale_codes)
Array(locale_codes).filter_map(&:presence).uniq
end
def persisted_config
(attribute_in_database('config') || {}).deep_stringify_keys
end
def drafted_locale_values
return config_value('draft_locales') if config_has_key?('draft_locales')
persisted_config['draft_locales']
end
def config_has_key?(key)
config.is_a?(Hash) && (config.key?(key) || config.key?(key.to_sym))
end
def config_value(key)
return unless config.is_a?(Hash)
config[key] || config[key.to_sym]
end
end
Portal.include_mod_with('Concerns::Portal')