mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 22:43:33 +08:00
DEV: extract inline js when baking theme fields (#6447)
* extract inline js when baking theme fields * destroy javascript cache when destroying theme fields This work is needed to support CSP work
This commit is contained in:
39
app/models/javascript_cache.rb
Normal file
39
app/models/javascript_cache.rb
Normal file
@ -0,0 +1,39 @@
|
||||
# frozen_string_literal: true
|
||||
class JavascriptCache < ActiveRecord::Base
|
||||
belongs_to :theme_field
|
||||
|
||||
validate :content_cannot_be_nil
|
||||
|
||||
before_save :update_digest
|
||||
|
||||
def url
|
||||
"#{GlobalSetting.cdn_url}#{GlobalSetting.relative_url_root}/javascripts/#{digest}.js"
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_digest
|
||||
self.digest = Digest::SHA1.hexdigest(content) if content_changed?
|
||||
end
|
||||
|
||||
def content_cannot_be_nil
|
||||
errors.add(:content, :empty) if content.nil?
|
||||
end
|
||||
end
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: javascript_caches
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# theme_field_id :bigint(8) not null
|
||||
# digest :string
|
||||
# content :text not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_javascript_caches_on_digest (digest)
|
||||
# index_javascript_caches_on_theme_field_id (theme_field_id)
|
||||
#
|
Reference in New Issue
Block a user