Skip to content

Commit

Permalink
Tidying up methods
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotsykes committed Apr 27, 2010
1 parent 5b31cf9 commit 6bbde50
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
38 changes: 33 additions & 5 deletions README
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
AssetFingerprint
================
Ruby on Rails plugin to fingerprint asset files using md5 checksum in filenames
to improve cacheability compared to default Rails asset caching strategy.

Introduction goes here.
Originally developed to improve the performance of MissedConnections.com.

Plugin can also be configured to use the default Rails asset caching strategy of
putting file timestamps in the query string.

Example
=======
Based on guidelines recommended by Google Page Speed here:
http://code.google.com/speed/page-speed/docs/caching.html

Example goes here.
Rails Versions
==============
Tested with Rails 2.2.3 though there's a good chance it'll work with earlier
and newer versions with little or no modification.

HOWTO
=====
Install the plugin at vendor/plugins/asset_fingerprint in your app.

Create a config/initializers/asset_fingerprint.rb file and add these lines:

# config/initializers/asset_fingerprint.rb start
require 'asset_fingerprint/asset_tag_helper'

ActionView::Helpers::AssetTagHelper.asset_fingerprint_strategy =
{ :fingerprint => :timestamp, :path => :query_string }

# 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
you want the same behaviour that ships with Rails, however, recommended strategy
is { :fingerprint => :md5, :path => :file_name } for maximum cacheability.

Copyright (c) 2010 Eliot Sykes, released under the MIT license

More projects at http://blog.eliotsykes.com/my-projects/

Copyright (c) 2010 [name of plugin creator], released under the MIT license
20 changes: 16 additions & 4 deletions lib/asset_fingerprint/asset_tag_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,27 @@ def rails_asset_fingerprint(source)
end
end

def fingerprint_in_query_string(source, asset_fingerprint)
source + "?#{asset_fingerprint}"
end

def fingerprint_in_file_name(source, asset_fingerprint)
# Insert the fingerprinted string as part of the filename
# The -1 value causes the fingerprint to be appended, happens
# if there is no period in source.
# Example result if source = 'images/logo.png' the result would
# be "images/logo-fp-#{asset_fingerprint}.png"
fingerprint_index = source.rindex('.') || -1
String.new(source).insert(fingerprint_index, "-fp-#{asset_fingerprint}")
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}"
result = fingerprint_in_query_string(source, asset_fingerprint)
elsif :file_name == @@asset_fingerprint_strategy[:path]
# Replace the file extension with -asset_fingerprint
#result = source.replace
result = source
result = fingerprint_in_file_name(source, asset_fingerprint)
else
raise RuntimeError.new "Unknown :path strategy '#{@@asset_fingerprint_strategy[:path]}'"
end
Expand Down

0 comments on commit 6bbde50

Please sign in to comment.