2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
class Appearance < MainClusterwide::ApplicationRecord
|
2018-11-08 19:23:39 +05:30
|
|
|
include CacheableAttributes
|
2016-11-03 12:29:30 +05:30
|
|
|
include CacheMarkdownField
|
2018-11-08 19:23:39 +05:30
|
|
|
include WithUploads
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
ALLOWED_PWA_ICON_SCALER_WIDTHS = [192, 512].freeze
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
attribute :title, default: ''
|
|
|
|
attribute :description, default: ''
|
2023-04-23 21:23:45 +05:30
|
|
|
attribute :pwa_name, default: ''
|
|
|
|
attribute :pwa_short_name, default: ''
|
|
|
|
attribute :pwa_description, default: ''
|
2023-01-13 00:05:48 +05:30
|
|
|
attribute :new_project_guidelines, default: ''
|
|
|
|
attribute :profile_image_guidelines, default: ''
|
|
|
|
attribute :header_message, default: ''
|
|
|
|
attribute :footer_message, default: ''
|
|
|
|
attribute :message_background_color, default: '#E75E40'
|
|
|
|
attribute :message_font_color, default: '#FFFFFF'
|
|
|
|
attribute :email_header_and_footer_enabled, default: false
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
cache_markdown_field :description
|
2018-03-17 18:26:18 +05:30
|
|
|
cache_markdown_field :new_project_guidelines
|
2020-05-24 23:13:21 +05:30
|
|
|
cache_markdown_field :profile_image_guidelines
|
2019-07-07 11:18:12 +05:30
|
|
|
cache_markdown_field :header_message, pipeline: :broadcast_message
|
|
|
|
cache_markdown_field :footer_message, pipeline: :broadcast_message
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
validates :pwa_name,
|
2023-07-09 08:55:56 +05:30
|
|
|
length: {
|
|
|
|
maximum: 255,
|
|
|
|
too_long: ->(object, data) { N_("is too long (maximum is %{count} characters)") }
|
|
|
|
},
|
|
|
|
allow_blank: true
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
validates :pwa_short_name,
|
2023-07-09 08:55:56 +05:30
|
|
|
length: {
|
|
|
|
maximum: 255,
|
|
|
|
too_long: ->(object, data) { N_("is too long (maximum is %{count} characters)") }
|
|
|
|
},
|
|
|
|
allow_blank: true
|
2023-04-23 21:23:45 +05:30
|
|
|
|
|
|
|
validates :pwa_description,
|
2023-07-09 08:55:56 +05:30
|
|
|
length: {
|
|
|
|
maximum: 2048,
|
|
|
|
too_long: ->(object, data) { N_("is too long (maximum is %{count} characters)") }
|
|
|
|
},
|
|
|
|
allow_blank: true
|
2023-04-23 21:23:45 +05:30
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
validates :logo, file_size: { maximum: 1.megabyte }
|
2023-03-17 16:20:25 +05:30
|
|
|
validates :pwa_icon, file_size: { maximum: 1.megabyte }
|
2016-04-02 18:10:28 +05:30
|
|
|
validates :header_logo, file_size: { maximum: 1.megabyte }
|
2019-07-07 11:18:12 +05:30
|
|
|
validates :message_background_color, allow_blank: true, color: true
|
|
|
|
validates :message_font_color, allow_blank: true, color: true
|
2020-05-24 23:13:21 +05:30
|
|
|
validates :profile_image_guidelines, length: { maximum: 4096 }
|
2016-04-02 18:10:28 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
validate :single_appearance_row, on: :create
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
mount_uploader :logo, AttachmentUploader
|
2023-03-17 16:20:25 +05:30
|
|
|
mount_uploader :pwa_icon, AttachmentUploader
|
2016-04-02 18:10:28 +05:30
|
|
|
mount_uploader :header_logo, AttachmentUploader
|
2018-11-08 19:23:39 +05:30
|
|
|
mount_uploader :favicon, FaviconUploader
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
# Overrides CacheableAttributes.current_without_cache
|
|
|
|
def self.current_without_cache
|
|
|
|
first
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def single_appearance_row
|
|
|
|
if self.class.any?
|
2020-04-08 14:13:33 +05:30
|
|
|
errors.add(:base, _('Only 1 appearances row can exist'))
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
def pwa_icon_path_scaled(width)
|
|
|
|
return unless pwa_icon_path.present?
|
|
|
|
|
|
|
|
pwa_icon_path + "?width=#{width}"
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def logo_path
|
|
|
|
logo_system_path(logo, 'logo')
|
|
|
|
end
|
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
def pwa_icon_path
|
|
|
|
logo_system_path(pwa_icon, 'pwa_icon')
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
def header_logo_path
|
|
|
|
logo_system_path(header_logo, 'header_logo')
|
|
|
|
end
|
|
|
|
|
|
|
|
def favicon_path
|
|
|
|
logo_system_path(favicon, 'favicon')
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
def show_header?
|
|
|
|
header_message.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_footer?
|
|
|
|
footer_message.present?
|
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def logo_system_path(logo, mount_type)
|
|
|
|
# Legacy attachments may not have have an associated Upload record,
|
|
|
|
# so fallback to the AttachmentUploader#url if this is the
|
|
|
|
# case. AttachmentUploader#path doesn't work because for a local
|
|
|
|
# file, this is an absolute path to the file.
|
|
|
|
return logo&.url unless logo&.upload
|
|
|
|
|
|
|
|
# If we're using a CDN, we need to use the full URL
|
|
|
|
asset_host = ActionController::Base.asset_host
|
|
|
|
local_path = Gitlab::Routing.url_helpers.appearance_upload_path(
|
|
|
|
filename: logo.filename,
|
|
|
|
id: logo.upload.model_id,
|
|
|
|
model: 'appearance',
|
|
|
|
mounted_as: mount_type)
|
|
|
|
|
|
|
|
Gitlab::Utils.append_path(asset_host, local_path)
|
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|