Created
February 1, 2011 06:11
-
-
Save adamloving/805499 to your computer and use it in GitHub Desktop.
Rails controller action for an HTML5 cache manifest file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class InstallerController < ApplicationController | |
# Rails controller action for an HTML5 cache manifest file. | |
# Generates a plain text list of files that changes | |
# when one of the listed files change... | |
# So the client knows when to refresh its cache. | |
def cache_manifest | |
@files = ["CACHE MANIFEST\n"] | |
@files << 'favicon.ico' | |
@files << './client/sencha/ext-touch.js' | |
@files << './client/sencha/resources/css/apple.css' | |
add_from('./client/views/','*.html') | |
add_from('./client/javascripts/','*.js') | |
add_from('./client/stylesheets/','*.css') | |
add_from('./client/images/','*.png') | |
@files << "\nNETWORK:" | |
@files << '*' | |
digest = Digest::SHA1.new | |
@files.each do |f| | |
actual_file = File.join(Rails.root,'public',f) | |
digest << "##{File.mtime(actual_file)}" if File.exist?(actual_file) | |
end | |
@files << "\n# Modification Digest: #{digest.hexdigest}" | |
render :text => @files.join("\n"), :content_type => 'text/cache-manifest', :layout => nil | |
end | |
protected | |
def add_from(loc,match) | |
Dir.glob(File.join(Rails.root,'public',loc, match)) do |file| | |
@files << "#{loc}#{File.basename(file)}" | |
end | |
end | |
end |
i ended with something like this:
def add_from(path, file_pattern)
actual_file = File.join(Rails.root,'public',path+"/"+file_pattern)
Dir[actual_file].each do |file|
@files << "#{path}/#{File.basename(file)}"
end
end
But i recently switched to a version using the assets manifest.yml
def add_from_asset_manifest
manifest_file = File.join(Rails.root,'public','assets','manifest.yml')
if FileTest.exist?(manifest_file)
File.open(manifest_file).each do |l|
if l.include? ":"
@files << "assets/#{l.split(":").last().strip()}"
end
end
end
end
Hey guys I suck for not replying. I like the yml approach as well. I added the original add_from call. @bsharpe gets credit for this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is the add_from call?