Skip to content

Instantly share code, notes, and snippets.

@ilpoldo
Created March 8, 2010 18:46
Show Gist options
  • Select an option

  • Save ilpoldo/325452 to your computer and use it in GitHub Desktop.

Select an option

Save ilpoldo/325452 to your computer and use it in GitHub Desktop.
require 'grit'
module ApplicationAssetsHelper
ASSETS_FOLDERS = %w[javascripts stylesheets]
ASSET_FORMATS = {'javascripts' => :js,
'stylesheets' => :css}
#LEDO?: create some sort of compile directives for the asset packager
def self.included(base)
load_asset_tag_methods
end
def self.load_asset_tag_methods
ASSETS_FOLDERS.each do |asset_folder|
assets_type = bundles_up_to_date? ? :bundled : :development
raise ApplicationAssetsError::StaleProductionAssetsBundle if (ENV['RAILS_ENV'] == "production" and assets_type == :development)
verb = (asset_folder == 'javascripts') ? :include : :link
for group in groups_for(asset_folder) do
class_eval <<-EOV
def #{asset_folder.singularize}_#{group}_bundle(*args)
#{asset_folder.singularize}_#{verb}_tag #{recursive_asset_file_list(ASSET_FORMATS[asset_folder], group, assets_type).inspect}, *args
end
EOV
end
end
end
private
# Returns true if there is an up to date version of the packaged javascripts
# find out if the bundled group is up to date using the commit id of both!
def self.bundles_up_to_date?
development_assets_commit = find_most_recent_commit
development_assets_commit and find_most_recent_commit(:bundled) == development_assets_commit
end
# Returns a list of the folders contained in each _asset_type_/development folder
def self.groups_for(asset_folder)
Dir.chdir File.join(RAILS_ROOT, 'public', asset_folder, 'development') do
dirs = Dir.glob("*")
end
end
# Lists all the files with the specified extension in a specified folder or bundled file depending
# if you are looking for development or bundled assets
#LEDO: reverse lookup on formats to determine the assets_folder
def self.recursive_asset_file_list(ext, group, assets_type = :development)
assets_folder = ASSET_FORMATS.index(ext)
Dir.chdir File.join(RAILS_ROOT, 'public', assets_folder) do
case assets_type
when :development
assets_path = File.join('development', "#{group}", "*.#{ext}")
when :bundled
assets_path = File.join('bundled', "#{group}_*.#{ext}")
end
files = Dir.glob(assets_path)
end
end
# LEDOUBT: use it for graphics as well?
def self.last_asset_commit_id
ENV['RAILS_ASSET_ID'] ||= find_most_recent_commit
end
# Returns the hash of the most recent commit among the assets as the asset cache id
def self.find_most_recent_commit(assets_type = :development)
repository = Grit::Repo.new(RAILS_ROOT+'/.git')
folders = []
for folder in ASSETS_FOLDERS do
folders << repository.log(repository.head.name,File.join("public", folder, "#{assets_type}"),:max_count => 1).first
end
folders.compact.empty? ? nil : folders.sort_by { |f| f.committed_date }.last.id
end
end
module ApplicationAssetsError
class Base < Exception; end
class StaleProductionAssetsBundle < ApplicationAssetsError::Base; end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment