# rails provide mechanisms for caching all css and js assets into one bundle
# to decrease page load times via reducing the number of requests a client
# needs to make.  however it does not provide a mechanism for clearing the
# generated (concatenated really) css/js cache file on deployment, meaning
# that changes to an asset file won't be automatically picked up on a
# redeploy, which is a constant PIMA.  anyhow, here is a super simple and ever
# so slightly brute force method to easily clear the css/js cache (or any
# cache really) automatically on a deploy.
#


# 1) pick something that is unique about each deploy.  i happen to use the
# git-rev but you might svn version number, or something else unique about
# each deploy.  note that timestamp won't work since you need to be able to
# replicate this unique number over time.  i use this in
# app/controllers/application_controller.rb
#

  class ApplicationController

  # ...

    def ApplicationController.git_rev
      @git_rev ||= `git rev-parse HEAD 2>/dev/null`.to_s.strip
    end

    def git_rev
      ApplicationController.git_rev
    end

    helper_method :git_rev

  # ...
  
  end


# 2)  name your cache files using this unique value, for instance, in #
# app/views/layouts/application.html.erb you might have
#
    javascript_include_tag(
      'jquery-1.3.2.min',
      'jquery-ui-1.7.2.min',
      'jrails',
      'jquery.form.js',
      'jquery.corners',
      'jquery.expandable',
      'jquery.sizes.min.js',
      'facebox/facebox.js',
      'jquery.toggleformtext.js',
      'jQuery.dPassword.js',
      'jquery.tablesorter.js',
      'jquery.jeditable.mini.js',
      'jquery-position-footer.js',
      '/jquery.uploadify-v2.1.0/swfobject.js',
      '/jquery.uploadify-v2.1.0/jquery.uploadify.v2.1.0.min.js',
      'application', :cache => "all-#{ git_rev }"
    )
    
    stylesheet_link_tag(
      '/yui/reset-fonts-grids/reset-fonts-grids',
      'facebox/facebox',
      'sprites',
      '/jquery.uploadify-v2.1.0/uploadify.css',
      'jquery-ui.all',
      'application', :cache => "all-#{ git_rev }"
    )

# meaning you'll end up with js/css cache filenames that look like
#
#   public/javascripts/all-0893045c8222e211888c7dbdca581f9d412a3044.js
#
# and
#
#   public/stylesheets/all-0893045c8222e211888c7dbdca581f9d412a3044.css
#
# in other words each deploy will create a new cache (the first client request
# to be accurate).
#


# 3) lastly, you may have observed that nothing will ever clean up old js/css
# cache files, that's fine, or you could write a rake task to do it, but the
# whole point of my code is to prevent my brain from needing to intervene, so
# i like everything automatic.  this code, in
# config/initializers/clear_js_and_css_cache.rb will do the trick nicely
#
#
  if RAILS_ENV=='production' or ActionController::Base.perform_caching
    git_rev = `git rev-parse HEAD 2>/dev/null`.to_s.strip
    glob = File.join(RAILS_ROOT, "public", "javascripts", "all-#{ git_rev }*")

    Dir.glob(glob).each do |all_js|
      next if File.basename(all_js) == "all-#{ git_rev }.js"
      FileUtils.rm_rf(all_js)
    end
  end


# and that's it.  now you can deploy and any changes to js or css will make
# into brand spanking new asset caches.