2018-10-15 12:55:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
class JavascriptCache < ActiveRecord::Base
|
|
|
|
belongs_to :theme_field
|
2019-06-03 17:41:00 +08:00
|
|
|
belongs_to :theme
|
2018-10-15 12:55:23 +08:00
|
|
|
|
|
|
|
validate :content_cannot_be_nil
|
|
|
|
|
|
|
|
before_save :update_digest
|
|
|
|
|
|
|
|
def url
|
2022-04-07 05:58:10 +08:00
|
|
|
"#{GlobalSetting.cdn_url}#{Discourse.base_path}#{path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def local_url
|
2022-07-29 05:20:52 +08:00
|
|
|
"#{Discourse.base_path}#{path}"
|
2018-10-15 12:55:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-04-07 05:58:10 +08:00
|
|
|
def path
|
|
|
|
"/theme-javascripts/#{digest}.js?__ws=#{Discourse.current_hostname}"
|
|
|
|
end
|
|
|
|
|
2018-10-15 12:55:23 +08:00
|
|
|
def update_digest
|
2023-11-28 19:28:40 +08:00
|
|
|
self.digest =
|
|
|
|
Digest::SHA1.hexdigest("#{content}|#{GlobalSetting.asset_url_salt}") if content_changed?
|
2018-10-15 12:55:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def content_cannot_be_nil
|
|
|
|
errors.add(:content, :empty) if content.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: javascript_caches
|
|
|
|
#
|
2019-05-03 06:34:12 +08:00
|
|
|
# id :bigint not null, primary key
|
2019-06-03 17:41:00 +08:00
|
|
|
# theme_field_id :bigint
|
2018-10-15 12:55:23 +08:00
|
|
|
# digest :string
|
|
|
|
# content :text not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2019-06-03 17:41:00 +08:00
|
|
|
# theme_id :bigint
|
2022-10-19 01:20:10 +08:00
|
|
|
# source_map :text
|
2018-10-15 12:55:23 +08:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_javascript_caches_on_digest (digest)
|
2023-08-18 02:57:04 +08:00
|
|
|
# index_javascript_caches_on_theme_field_id (theme_field_id) UNIQUE
|
|
|
|
# index_javascript_caches_on_theme_id (theme_id) UNIQUE
|
2019-06-03 17:41:00 +08:00
|
|
|
#
|
|
|
|
# Foreign Keys
|
|
|
|
#
|
|
|
|
# fk_rails_... (theme_field_id => theme_fields.id) ON DELETE => cascade
|
|
|
|
# fk_rails_... (theme_id => themes.id) ON DELETE => cascade
|
2018-10-15 12:55:23 +08:00
|
|
|
#
|