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