digestion 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2011 Sam Pohlenz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ Digestion (Asset Pipeline Fingerprinting Controls)
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 'digestion'
15
+
16
+ The `digestion` gem does not change any fingerprinting options by default but allows you to set the following options in your application configuration:
17
+
18
+ # Exclude specific assets from fingerprinting (use a path, glob or regex)
19
+ config.assets.digest_exclusions << "nofingerprints/*"
20
+
21
+ 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.
22
+
23
+
24
+ The Problem With Fingerprinting
25
+ -------------------------------
26
+
27
+ Fingerprinting is used to improve caching of assets by including a version-specific fingerprint or digest in the asset filename. From the [Asset Pipeline Rails Guide](http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care):
28
+
29
+ > 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 their 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.
30
+
31
+ 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).
32
+
33
+ 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.
34
+
35
+ One option is to move the assets into the `public` directory. Whilst this approach will work, it has some major drawbacks:
36
+
37
+ 1. It prevents you from using the other great features of the asset pipeline such as asset concatenation and minification.
38
+ 2. It leaves your assets scattered across multiple locations, making maintenance difficult.
39
+ 3. A gem that provides assets must provide an additional rake task to copy its assets into `public`.
40
+
41
+ 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.
data/lib/digestion.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "digestion/helpers"
2
+ require "digestion/railtie" if defined?(Rails)
@@ -0,0 +1,66 @@
1
+ Rake::Task["assets:precompile"].clear
2
+
3
+ def digest_path?(logical_path)
4
+ return false unless Rails.application.config.assets.digest
5
+ return true if Rails.application.config.assets.digest_exclusions.blank?
6
+
7
+ Rails.application.config.assets.digest_exclusions.none? do |path|
8
+ if path.is_a?(Regexp)
9
+ # Match path against `Regexp`
10
+ path.match(logical_path)
11
+ else
12
+ # Otherwise use fnmatch glob syntax
13
+ File.fnmatch(path.to_s, logical_path)
14
+ end
15
+ end
16
+ end
17
+
18
+ namespace :assets do
19
+ task :precompile do
20
+ # We need to do this dance because RAILS_GROUPS is used
21
+ # too early in the boot process and changing here is already too late.
22
+ if ENV["RAILS_GROUPS"].to_s.empty? || ENV["RAILS_ENV"].to_s.empty?
23
+ ENV["RAILS_GROUPS"] ||= "assets"
24
+ ENV["RAILS_ENV"] ||= "production"
25
+ Kernel.exec $0, *ARGV
26
+ else
27
+ Rake::Task["environment"].invoke
28
+
29
+ # Ensure that action view is loaded and the appropriate sprockets hooks get executed
30
+ ActionView::Base
31
+
32
+ # Always compile files
33
+ Rails.application.config.assets.compile = true
34
+
35
+ config = Rails.application.config
36
+ env = Rails.application.assets
37
+ target = Pathname.new(File.join(Rails.public_path, config.assets.prefix))
38
+ manifest = {}
39
+ manifest_path = config.assets.manifest || target
40
+
41
+ config.assets.precompile.each do |path|
42
+ env.each_logical_path do |logical_path|
43
+ if path.is_a?(Regexp)
44
+ next unless path.match(logical_path)
45
+ else
46
+ next unless File.fnmatch(path.to_s, logical_path)
47
+ end
48
+
49
+ if asset = env.find_asset(logical_path)
50
+ asset_path = digest_path?(logical_path) ? asset.digest_path : logical_path
51
+ manifest[logical_path] = asset_path
52
+ filename = target.join(asset_path)
53
+
54
+ mkdir_p filename.dirname
55
+ asset.write_to(filename)
56
+ asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
57
+ end
58
+ end
59
+ end
60
+
61
+ File.open("#{manifest_path}/manifest.yml", 'w') do |f|
62
+ YAML.dump(manifest, f)
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,51 @@
1
+ require 'sprockets/helpers'
2
+
3
+ Sprockets::Helpers::RailsHelper.module_eval do
4
+ alias_method :original_asset_paths, :asset_paths
5
+
6
+ def asset_paths
7
+ @asset_paths ||= original_asset_paths.tap do |paths|
8
+ paths.digest_assets = digest_assets?
9
+ paths.digest_exclusions = digest_exclusions
10
+ end
11
+ end
12
+
13
+ private
14
+ def digest_assets?
15
+ Rails.application.config.assets.digest
16
+ end
17
+
18
+ def digest_exclusions
19
+ Rails.application.config.assets.digest_exclusions
20
+ end
21
+ end
22
+
23
+ Sprockets::Helpers::RailsHelper::AssetPaths.class_eval do
24
+ attr_accessor :digest_assets, :digest_exclusions
25
+
26
+ alias_method :original_digest_for, :digest_for
27
+
28
+ def digest_for(logical_path)
29
+ if digest_path?(logical_path)
30
+ original_digest_for(logical_path)
31
+ else
32
+ logical_path
33
+ end
34
+ end
35
+
36
+ private
37
+ def digest_path?(logical_path)
38
+ return false unless digest_assets
39
+ return true if digest_exclusions.blank?
40
+
41
+ digest_exclusions.none? do |path|
42
+ if path.is_a?(Regexp)
43
+ # Match path against `Regexp`
44
+ path.match(logical_path)
45
+ else
46
+ # Otherwise use fnmatch glob syntax
47
+ File.fnmatch(path.to_s, logical_path)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,13 @@
1
+ require 'sprockets/railtie'
2
+
3
+ module Digestion
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ load "digestion/assets.rake"
7
+ end
8
+
9
+ initializer :setup_configuration, :before => :load_environment_config do |app|
10
+ app.config.assets.digest_exclusions = []
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: digestion
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Pohlenz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: &2152759540 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152759540
25
+ description: Adds asset digest configuration options to Rails so that specific paths
26
+ can be excluded from fingerprinting.
27
+ email: sam@sampohlenz.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - README.md
33
+ - LICENSE
34
+ - lib/digestion/assets.rake
35
+ - lib/digestion/helpers.rb
36
+ - lib/digestion/railtie.rb
37
+ - lib/digestion.rb
38
+ homepage:
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.10
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Fine-grained digest controls for the Rails 3.1 asset pipeline.
62
+ test_files: []