Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A precompile hook to precompile CKEditor assets.
require 'fileutils'
desc "Create nondigest versions of all ckeditor digest assets"
task "assets:precompile" => :environment do
fingerprint = /\-([0-9a-f]{32})\./
for file in Dir["public/assets/ckeditor/**/*"]
# Skip file unless it has a fingerprint
next unless file =~ fingerprint
# Get filename of this file without the digest
# (example) public/assets/ckeditor/config.js
nondigest = file.sub fingerprint, '.'
# Create a filename relative to public/assets
# (example) public/assets/ckeditor/config.js => ckeditor/config.js
filename = nondigest.sub 'public/assets/', ''
filename = filename.sub /.gz$/, '' # Remove .gz for correct asset checking
# Fetch the latest digest for this file from assets
latest_digest = Rails.application.assets.find_asset(filename).digest
# Debug information
STDOUT.puts '---- ' + file + ' ----'
# Compare digest of this file to latest digest
# [1] is the enclosed capture in the fingerprint regex above
this_digest = file.match(fingerprint)[1]
if (this_digest == latest_digest)
# This file's digest matches latest digest, copy
STDOUT.puts 'Matching digest, copying ' + file
FileUtils.cp file, nondigest, verbose: true
# This file's digest doesn't match latest digest, ignore
STDOUT.puts 'Latest digest: ' + latest_digest
STDOUT.puts 'This digest: ' + this_digest
STDOUT.puts 'Non-matching digest, not copying ' + file
# Debug information
STDOUT.puts '---- end ----'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment