fingerprintless-assets 1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ Fingerprintless Assets
2
+ ======================
3
+
4
+ The asset pipeline is a great new component of Rails 3.1. However it has a feature known as fingerprinting that makes it impossible to properly incorporate many popular JavaScript libraries (including [TinyMCE](http://tinymce.moxiecode.com/), [CKEditor](http://ckeditor.com/) and [FancyZoom](https://github.com/jnunemaker/fancy-zoom) to name just a few) into the asset pipeline.
5
+
6
+ This gem patches the asset pipeline to allow these libraries to be used, by disabling the fingerprinting functionality for specific files or paths.
7
+
8
+
9
+ Installation & Usage
10
+ --------------------
11
+
12
+ Add the following line to your Gemfile and run `bundle install`:
13
+
14
+ gem 'fingerprintless-assets'
15
+
16
+ The `fingerprintless-assets` gem does not change any fingerprinting options by default but allows you to set the following options in your application configuration:
17
+
18
+ # Disable asset fingerprinting entirely
19
+ config.assets.fingerprinting.enabled = false
20
+
21
+ # or exclude specific assets from fingerprinting (use a path, glob or regex)
22
+ config.assets.fingerprinting.exclude << "nofingerprints/*"
23
+
24
+ Rails plugins can require this gem and set these options in an initializer, making the whole process transparent to the end-developer. See the [tinymce-rails](https://github.com/spohlenz/tinymce-rails) project for an example of this.
25
+
26
+
27
+ The Problem With Fingerprinting
28
+ -------------------------------
29
+
30
+ Fingerprinting is used to improve caching of assets by including a version-specific fingerprint in the asset filename. From the [Asset Pipeline Rails Guide](http://ryanbigg.com/guides/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care):
31
+
32
+ > When a filename is unique and based on its content, http headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep there own copy of the content. When the content is updated, the fingerprint will change and the remote clients will request the new file. This is generally known as cachebusting.
33
+
34
+ In the Rails asset pipeline (in production mode), the MD5 hash of the file contents is inserted into the filename, e.g. `application-d41d8cd98f00b204e9800998ecf8427e.js`. This requires that the relevant `asset_url` helpers are used when referencing assets in your HTML, JavaScript or CSS (or CoffeeScript, SASS, etc).
35
+
36
+ When adding third-party code to your application, it is not always practical to make these changes. Worse still, more advanced libraries like TinyMCE load in plugins and other assets dynamically, making these changes virtually impossible. In these situations, the third-party code will work fine whilst in development but break suddenly when deployed to production.
37
+
38
+ One option is to move the assets into the `public` directory. Whilst this approach will work, it has some major drawbacks:
39
+
40
+ 1. It prevents you from using the other great features of the asset pipeline such as asset concatenation and minification.
41
+ 2. It leaves your assets scattered across multiple locations, making maintenance difficult.
42
+ 3. A gem that provides assets must provide an additional rake task to copy its assets into `public`.
43
+
44
+ Asset fingerprinting is a great feature and it is recommended you use it wherever possible. However without this gem, it is impossible to disable it for those libraries that are incompatible.
@@ -0,0 +1,11 @@
1
+ require "sprockets"
2
+ require 'sprockets/base'
3
+
4
+ require "fingerprintless_assets/monkey_patches/configuration"
5
+ require "fingerprintless_assets/monkey_patches/environment"
6
+ require "fingerprintless_assets/monkey_patches/index"
7
+ require "fingerprintless_assets/monkey_patches/server"
8
+ require "fingerprintless_assets/monkey_patches/static_compilation"
9
+ require "fingerprintless_assets/monkey_patches/utils"
10
+
11
+ require "fingerprintless_assets/railtie" if defined?(Rails)
@@ -0,0 +1,38 @@
1
+ module FingerprintlessAssets
2
+ module Configuration
3
+ # Checks if asset path fingerprinting is enabled.
4
+ def fingerprinting_enabled?
5
+ @fingerprinting_enabled
6
+ end
7
+
8
+ # Enable or disable asset path fingerprinting.
9
+ def fingerprinting_enabled=(enabled)
10
+ @fingerprinting_enabled = enabled
11
+ end
12
+
13
+ # Returns an `Array` of paths, globs or `Regexp`s that
14
+ # should be excluded from asset path fingerprinting.
15
+ def fingerprinting_exclusions
16
+ @fingerprinting_exclusions ||= []
17
+ end
18
+
19
+ # Set the paths that should be excluded from asset path fingerprinting.
20
+ def fingerprinting_exclusions=(exclusions)
21
+ @fingerprinting_exclusions = exclusions
22
+ end
23
+
24
+ # Checks if the path should be fingerprinted.
25
+ def fingerprint_path?(logical_path)
26
+ fingerprinting_enabled? && !exclude_path_from_fingerprinting?(logical_path)
27
+ end
28
+
29
+ private
30
+ # Checks if the given path is in the current list
31
+ # of fingerprinting exclusions.
32
+ def exclude_path_from_fingerprinting?(logical_path)
33
+ fingerprinting_exclusions.any? { |path| match_path?(logical_path, path) }
34
+ end
35
+ end
36
+ end
37
+
38
+ Sprockets::Base.send(:include, FingerprintlessAssets::Configuration)
@@ -0,0 +1,12 @@
1
+ Sprockets::Environment.class_eval do
2
+ alias_method :original_initialize, :initialize
3
+
4
+ def initialize(root = '.')
5
+ original_initialize(root)
6
+
7
+ # Enable fingerprinting by default
8
+ @fingerprinting_enabled = true
9
+
10
+ yield self if block_given?
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ Sprockets::Index.class_eval do
2
+ alias_method :original_initialize, :initialize
3
+
4
+ def initialize(environment)
5
+ original_initialize(environment)
6
+
7
+ # Inherit fingerprinting values from environment
8
+ @fingerprinting_enabled = environment.fingerprinting_enabled?
9
+ @fingerprinting_exclusions = environment.fingerprinting_exclusions
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module FingerprintlessAssets
2
+ module Server
3
+ def path(logical_path, _ignored_fingerprint = true, prefix = nil)
4
+ super(logical_path, fingerprint_path?(logical_path), prefix)
5
+ end
6
+ end
7
+ end
8
+
9
+ Sprockets::Base.send(:include, FingerprintlessAssets::Server)
@@ -0,0 +1,38 @@
1
+ module FingerprintlessAssets
2
+ module StaticCompilation
3
+ def precompile(*paths)
4
+ raise "missing static root" unless static_root
5
+
6
+ paths.each do |path|
7
+ files.each do |logical_path|
8
+ next unless match_path?(logical_path, path)
9
+
10
+ if asset = find_asset(logical_path)
11
+ precompile_asset(asset)
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ def precompile_asset(asset)
18
+ if fingerprint_path?(asset.logical_path)
19
+ attributes = attributes_for(asset.logical_path)
20
+ digest_path = attributes.path_with_fingerprint(asset.digest)
21
+ filename = static_root.join(digest_path)
22
+ else
23
+ filename = static_root.join(asset.logical_path)
24
+ end
25
+
26
+ # Ensure directory exists
27
+ FileUtils.mkdir_p filename.dirname
28
+
29
+ # Write file
30
+ asset.write_to(filename)
31
+
32
+ # Write compressed file if its a bundled asset like .js or .css
33
+ asset.write_to("#{filename}.gz") if asset.is_a?(Sprockets::BundledAsset)
34
+ end
35
+ end
36
+ end
37
+
38
+ Sprockets::Base.send(:include, FingerprintlessAssets::StaticCompilation)
@@ -0,0 +1,15 @@
1
+ module FingerprintlessAssets
2
+ module Utils
3
+ def match_path?(path, match)
4
+ if match.is_a?(Regexp)
5
+ # Match path against `Regexp`
6
+ match.match(path)
7
+ else
8
+ # Otherwise use fnmatch glob syntax
9
+ File.fnmatch(match.to_s, path)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Sprockets::Base.send(:include, FingerprintlessAssets::Utils)
@@ -0,0 +1,21 @@
1
+ module FingerprintlessAssets
2
+ class Railtie < ::Rails::Railtie
3
+ initializer :setup_configuration, :before => :load_environment_config do |app|
4
+ app.config.assets.fingerprinting = ActiveSupport::OrderedOptions.new
5
+ app.config.assets.fingerprinting.exclude = []
6
+ end
7
+
8
+ initializer :set_configuration_defaults do |app|
9
+ # Unless explicitly set, enable fingerprinting if performing caching
10
+ unless app.config.assets.fingerprinting.key?(:enabled)
11
+ app.config.assets.fingerprinting.enabled = app.config.action_controller.perform_caching
12
+ end
13
+ end
14
+
15
+ config.after_initialize do |app|
16
+ # Apply configuration to the application's asset environment
17
+ app.assets.fingerprinting_enabled = app.config.assets.fingerprinting.enabled
18
+ app.assets.fingerprinting_exclusions = app.config.assets.fingerprinting.exclude
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fingerprintless-assets
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.pre
5
+ prerelease: 4
6
+ platform: ruby
7
+ authors:
8
+ - Sam Pohlenz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-03 00:00:00.000000000 +09:30
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: sprockets
17
+ requirement: &2153060000 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0.beta.12
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2153060000
26
+ description: Adds asset fingerprinting configuration options to Rails and Sprockets
27
+ so that paths can be excluded from fingerprinting.
28
+ email: sam@sampohlenz.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/fingerprintless-assets.rb
35
+ - lib/fingerprintless_assets/monkey_patches/configuration.rb
36
+ - lib/fingerprintless_assets/monkey_patches/environment.rb
37
+ - lib/fingerprintless_assets/monkey_patches/index.rb
38
+ - lib/fingerprintless_assets/monkey_patches/server.rb
39
+ - lib/fingerprintless_assets/monkey_patches/static_compilation.rb
40
+ - lib/fingerprintless_assets/monkey_patches/utils.rb
41
+ - lib/fingerprintless_assets/railtie.rb
42
+ has_rdoc: true
43
+ homepage:
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>'
59
+ - !ruby/object:Gem::Version
60
+ version: 1.3.1
61
+ requirements: []
62
+ rubyforge_project:
63
+ rubygems_version: 1.6.2
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Fingerprinting controls for the Rails 3.1 asset pipeline.
67
+ test_files: []