debian-mirror-gitlab/app/models/appearance.rb

64 lines
1.7 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2016-04-02 18:10:28 +05:30
class Appearance < ActiveRecord::Base
2018-11-08 19:23:39 +05:30
include CacheableAttributes
2016-11-03 12:29:30 +05:30
include CacheMarkdownField
2018-05-09 12:01:36 +05:30
include ObjectStorage::BackgroundMove
2018-11-08 19:23:39 +05:30
include WithUploads
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
2016-11-03 12:29:30 +05:30
2016-04-02 18:10:28 +05:30
validates :logo, file_size: { maximum: 1.megabyte }
validates :header_logo, file_size: { maximum: 1.megabyte }
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
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?
errors.add(:single_appearance_row, 'Only 1 appearances row can exist')
end
end
2019-02-15 15:39:39 +05:30
def logo_path
logo_system_path(logo, 'logo')
end
def header_logo_path
logo_system_path(header_logo, 'header_logo')
end
def favicon_path
logo_system_path(favicon, 'favicon')
end
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