Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 5b31cf95d77cef6a760b5bcd3b01933f03cbf06d @eliotsykes committed Apr 27, 2010
@@ -0,0 +1,20 @@
+Copyright (c) 2010 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 README
@@ -0,0 +1,13 @@
+AssetFingerprint
+================
+
+Introduction goes here.
+
+
+Example
+=======
+
+Example goes here.
+
+
+Copyright (c) 2010 [name of plugin creator], released under the MIT license
@@ -0,0 +1,23 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
+desc 'Test the asset_fingerprint plugin.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
+desc 'Generate documentation for the asset_fingerprint plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'AssetFingerprint'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
@@ -0,0 +1 @@
+# Include hook code here
@@ -0,0 +1 @@
+# Install hook code here
@@ -0,0 +1 @@
+# AssetFingerprint
@@ -0,0 +1,95 @@
+require 'digest/md5'
+
+module ActionView
+ module Helpers
+ module AssetTagHelper
+
+ def self.cache_asset_fingerprints
+ if @@cache_asset_fingerprints.nil?
+ # Asset fingerprints cache behaviour same as cache_asset_timestamps
+ # if no value set.
+ return ActionView::Helpers::AssetTagHelper.cache_asset_timestamps
+ end
+ @@cache_asset_fingerprints
+ end
+
+ # You can enable or disable the asset fingerprints cache.
+ # With the cache enabled, the asset tag helper methods will make fewer
+ # expensive calls. However this prevents you from modifying
+ # any asset files while the server is running.
+ #
+ def self.cache_asset_fingerprints=(value)
+ @@cache_asset_fingerprints = value
+ end
+
+ @@cache_asset_fingerprints = nil
+
+ @@asset_fingerprints_cache = {}
+ @@asset_fingerprints_cache_guard = Mutex.new
+
+ def rails_asset_fingerprint(source)
+ if asset_fingerprint = ENV["RAILS_ASSET_ID"]
+ asset_fingerprint
+ else
+ if AssetTagHelper.cache_asset_fingerprints && (asset_fingerprint = @@asset_fingerprints_cache[source])
+ asset_fingerprint
+ else
+ path = File.join(ASSETS_DIR, source)
+ asset_fingerprint = calculate_asset_fingerprint(path)
+
+ if AssetTagHelper.cache_asset_fingerprints
+ @@asset_fingerprints_cache_guard.synchronize do
+ @@asset_fingerprints_cache[source] = asset_fingerprint
+ end
+ end
+
+ asset_fingerprint
+ end
+ end
+ end
+
+ # Replaces the Rails method of the same name in AssetTagHelper.
+ def rewrite_asset_path(source)
+ asset_fingerprint = rails_asset_fingerprint(source)
+ if :query_string == @@asset_fingerprint_strategy[:path]
+ result = source + "?#{asset_fingerprint}"
+ elsif :file_name == @@asset_fingerprint_strategy[:path]
+ # Replace the file extension with -asset_fingerprint
+ #result = source.replace
+ result = source
+ else
+ raise RuntimeError.new "Unknown :path strategy '#{@@asset_fingerprint_strategy[:path]}'"
+ end
+ result = source if asset_fingerprint.blank?
+ result
+ end
+
+ # Use this to set how fingerprints are calculated and how the
+ # fingerprints are put into the asset path.
+ #
+ # Default strategy is as follows:
+ # {:fingerprint => :timestamp, :path => :query_string}
+ #
+ # Valid :fingerprint values are :timestamp, :md5
+ # Valid :path values are :query_string, :file_name
+ def self.asset_fingerprint_strategy=(value)
+ @@asset_fingerprint_strategy = value
+ end
+
+ @@asset_fingerprint_strategy = { :fingerprint => :timestamp, :path => :query_string }
+
+ # This is where the asset fingerprint is calculated, where the path
+ # argument is the path to the asset file.
+ def calculate_asset_fingerprint(path)
+ if :timestamp == @@asset_fingerprint_strategy[:fingerprint]
+ return File.exist?(path) ? File.mtime(path).to_i.to_s : ''
+ elsif :md5 == @@asset_fingerprint_strategy[:fingerprint]
+ return Digest::MD5.hexdigest(File.read(path))
+ else
+ raise RuntimeError.new "Unknown :fingerprint strategy '#{@@asset_fingerprint_strategy[:fingerprint]}'"
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :asset_fingerprint do
+# # Task goes here
+# end
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class AssetFingerprintTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
@@ -0,0 +1,3 @@
+require 'rubygems'
+require 'active_support'
+require 'active_support/test_case'
@@ -0,0 +1 @@
+# Uninstall hook code here

0 comments on commit 5b31cf9

Please sign in to comment.