Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Introduced Fingerprinter to simplify and shrink AssetTagHelper mods

  • Loading branch information...
commit 09e82232086e7720af627b0d03a1fc700160aa30 1 parent 5ca2bec
@eliotsykes authored
View
17 README
@@ -45,16 +45,19 @@ Create a config/initializers/asset_fingerprint.rb file and add these lines:
# config/initializers/asset_fingerprint.rb start
require 'asset_fingerprint/asset_tag_helper'
+AssetFingerprint.fingerprinter = :md5
+
ActionView::Helpers::AssetTagHelper.asset_fingerprint_strategy =
- { :fingerprint => :md5, :path => :file_name }
+ { :path => :file_name }
# config/initializers/asset_fingerprint.rb end
-Change the asset_fingerprint_strategy in this initializer file to suit
-your needs. Set :fingerprint to :timestamp and :path to :query_string if
+Configure this initializer file to suit your needs.
+
+Set fingerprinter to :timestamp and :path to :query_string if
you want the same behaviour that ships with Rails, however, the recommended
-strategy is { :fingerprint => :md5, :path => :file_name } for maximum
-cacheability.
+strategy is to use the :md5 fingerprinter and a :path value of :file_name for
+maximum cacheability.
If you're using file_name fingerprinting (recommended)
======================================================
@@ -72,13 +75,13 @@ Option 1 - Apache Rewrite Rule
Add *only* one of these rewrite rules to Apache to get it to serve asset files
which are requested with fingerprinted file names.
-# For md5 file_name strategy: { :fingerprint => :md5, :path => :file_name }
+# For md5 file_name strategy fingerprinter == :md5, :path == :file_name
# md5 checksums are 32 characters long.
RewriteRule ^(.+)-fp-[0-9a-z]{32}(.*)$ $1$2 [QSA,PT,L]
-OR-
-# For timestamp file_name strategy: { :fingerprint => :timestamp, :path => :file_name }
+# For timestamp file_name strategy: fingerprinter == :timestamp, :path == :file_name }
# You should check that the timestamps returned by your production operating
# system are 10 digits long. If they are not, change the {10} in the regex
# below to the correct length.
View
30 lib/asset_fingerprint/asset_tag_helper.rb
@@ -1,5 +1,5 @@
-require 'digest/md5'
require 'asset_fingerprint/symlinker'
+require 'asset_fingerprint/fingerprinter'
module ActionView
module Helpers
@@ -36,8 +36,8 @@ def rails_asset_fingerprint(source)
asset_fingerprint
else
path = AssetTagHelper.abs_path_to_asset(source)
- asset_fingerprint = calculate_asset_fingerprint(path)
-
+ asset_fingerprint = AssetFingerprint.calculate_fingerprint(path)
+
if AssetTagHelper.cache_asset_fingerprints
@@asset_fingerprints_cache_guard.synchronize do
@@asset_fingerprints_cache[source] = asset_fingerprint
@@ -82,33 +82,13 @@ def rewrite_asset_path(source)
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 }
+ @@asset_fingerprint_strategy = { :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
View
39 lib/asset_fingerprint/fingerprinter.rb
@@ -0,0 +1,39 @@
+require 'digest/md5'
+
+module AssetFingerprint
+
+ def self.calculate_fingerprint(path)
+ fingerprinter.fingerprint(path)
+ end
+
+ def self.fingerprinter=(value)
+ if :timestamp == value
+ @@fingerprinter = TimestampFingerprinter
+ elsif :md5 == value
+ @@fingerprinter = Md5Fingerprinter
+ else
+ @@fingerprinter = value
+ end
+ end
+
+ def self.fingerprinter
+ @@fingerprinter
+ end
+
+ module TimestampFingerprinter
+
+ def self.fingerprint(path)
+ File.exist?(path) ? File.mtime(path).to_i.to_s : ''
+ end
+
+ end
+
+ module Md5Fingerprinter
+
+ def self.fingerprint(path)
+ Digest::MD5.hexdigest(File.read(path))
+ end
+
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.