rails2_asset_pipeline 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails2_asset_pipeline (0.1.11)
4
+ rails2_asset_pipeline (0.1.12)
5
5
  sprockets
6
6
 
7
7
  GEM
data/Readme.md CHANGED
@@ -4,7 +4,7 @@ Familiar asset handling for those stuck on Rails 2.
4
4
 
5
5
  - sprockets/coffee/sass etc goodness
6
6
  - application.js?time for development
7
- - application-MD5.js for production (and development without config.ru)
7
+ - application-MD5.js for production (and development without config.ru, read from public/assets/manifest.json)
8
8
  - old asset versions can stay around during deploys
9
9
  - converter for jammit asset.yml
10
10
  - no forced monkey-patching, everything is opt-in
@@ -95,6 +95,7 @@ You can also use `Rails2AssetPipeline::ViewHelpers.asset_path("application.js")`
95
95
  # Todo
96
96
  - read config from Rails 3 style config.assets
97
97
  - asset image helpers for inside css/scss
98
+ - make output location configurable in .setup and use it for manifest location and rake tasks
98
99
 
99
100
  Author
100
101
  ======
@@ -32,4 +32,8 @@ module Rails2AssetPipeline
32
32
  end
33
33
  end
34
34
  end
35
+
36
+ def self.static?
37
+ not Rails2AssetPipeline.dynamic_assets_available or Rails2AssetPipeline::STATIC_ENVIRONMENTS.include?(Rails.env)
38
+ end
35
39
  end
@@ -1,3 +1,3 @@
1
1
  module Rails2AssetPipeline
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
@@ -23,16 +23,26 @@ module Rails2AssetPipeline
23
23
  end
24
24
  end
25
25
 
26
- def asset_path(asset)
27
- data = Rails2AssetPipeline.env[asset]
28
- return "/assets/NOT_FOUND" unless data
29
- asset = "/assets/#{asset}"
26
+ def rails_asset_id(file)
27
+ if file.start_with?("/assets/")
28
+ nil
29
+ else
30
+ super
31
+ end
32
+ end
30
33
 
31
- if not Rails2AssetPipeline.dynamic_assets_available or Rails2AssetPipeline::STATIC_ENVIRONMENTS.include?(Rails.env)
32
- asset.sub(/(\.[\.a-z]+$)/, "-#{data.digest}\\1")
34
+ def asset_path(asset)
35
+ asset_with_id = if Rails2AssetPipeline.static?
36
+ manifest = "#{Rails.root}/public/assets/manifest.json"
37
+ raise "No dynamic assets available and no manifest found, run rake assets:precompile" unless File.exist?(manifest)
38
+ @sprockets_manifest ||= Sprockets::Manifest.new(Rails2AssetPipeline.env, manifest)
39
+ @sprockets_manifest.files[asset] || "NOT_FOUND_IN_MANIFEST"
33
40
  else
34
- "#{asset}?#{data.mtime.to_i}"
41
+ data = Rails2AssetPipeline.env[asset]
42
+ data ? "#{asset}?#{data.mtime.to_i}" : "NOT_FOUND_IN_ASSETS"
35
43
  end
44
+
45
+ "/assets/#{asset_with_id}"
36
46
  end
37
47
  module_function :asset_path
38
48
  end
@@ -1,18 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe "Rails2AssetPipeline Tasks" do
4
- def run(cmd)
5
- result = `#{cmd} 2>&1`
6
- raise "FAILED #{cmd} --> #{result}" unless $?.success?
7
- result
8
- end
9
-
10
- def write(file, content)
11
- folder = File.dirname(file)
12
- run "mkdir -p #{folder}" unless File.exist?(folder)
13
- File.open(file, 'w'){|f| f.write content }
14
- end
15
-
16
4
  def cleanup
17
5
  run "rm -rf public/assets"
18
6
  run "rm -rf public/javascripts"
@@ -6,12 +6,21 @@ describe Rails2AssetPipeline::ViewHelpers do
6
6
  @compute_public_path = args
7
7
  :super
8
8
  end
9
+
10
+ def rails_asset_id(*args)
11
+ @rails_asset_id = args
12
+ :super
13
+ end
9
14
  end
10
15
 
11
16
  include FakeSuper
12
17
  include Rails2AssetPipeline::ViewHelpers
13
18
 
14
- let(:env){ {} }
19
+ let(:env){
20
+ env = {}
21
+ env.stub(:logger).and_return mock()
22
+ env
23
+ }
15
24
 
16
25
  before do
17
26
  Rails2AssetPipeline.stub(:env).and_return env
@@ -19,13 +28,34 @@ describe Rails2AssetPipeline::ViewHelpers do
19
28
  env["xxx.js"] = mock(:digest => "abc", :mtime => Time.at(123456))
20
29
  end
21
30
 
22
- describe "#asset" do
31
+ describe "#asset_path" do
32
+ let(:manifest){ "#{Rails.root}/public/assets/manifest.json" }
33
+
34
+ before do
35
+ write manifest, <<-JSON
36
+ {
37
+ "files": {"xxx.js": "xxx-manifest.js"},
38
+ "assets": {
39
+ "xxx-manifest.js": {
40
+ "logical_path": "xxx.js",
41
+ "mtime": "2011-12-13T21:47:08-06:00",
42
+ "digest": "manifest"
43
+ }
44
+ }
45
+ }
46
+ JSON
47
+ end
48
+
49
+ after do
50
+ run "rm -rf public"
51
+ end
52
+
23
53
  it "is also static" do
24
54
  Rails2AssetPipeline::ViewHelpers.asset_path("xxx.js").should_not == nil
25
55
  end
26
56
 
27
57
  it "silently fails with unfound assets" do
28
- asset_path("yyy.js").should == "/assets/NOT_FOUND"
58
+ asset_path("yyy.js").should == "/assets/NOT_FOUND_IN_ASSETS"
29
59
  end
30
60
 
31
61
  context "development" do
@@ -33,9 +63,14 @@ describe Rails2AssetPipeline::ViewHelpers do
33
63
  asset_path("xxx.js").should == "/assets/xxx.js?123456"
34
64
  end
35
65
 
36
- it "returns a path with digest when dynamic loader is not available" do
66
+ it "does not care if manifest is missing" do
67
+ run "rm #{manifest}"
68
+ asset_path("xxx.js").should == "/assets/xxx.js?123456"
69
+ end
70
+
71
+ it "is digested when dynamic loader is not available" do
37
72
  Rails2AssetPipeline.dynamic_assets_available = false
38
- asset_path("xxx.js").should == "/assets/xxx-abc.js"
73
+ asset_path("xxx.js").should == "/assets/xxx-manifest.js"
39
74
  end
40
75
  end
41
76
 
@@ -44,17 +79,36 @@ describe Rails2AssetPipeline::ViewHelpers do
44
79
  Rails.env = "production"
45
80
  end
46
81
 
47
- it "returns a path with md5" do
48
- asset_path("xxx.js").should == "/assets/xxx-abc.js"
82
+ it "returns a path with digest" do
83
+ asset_path("xxx.js").should == "/assets/xxx-manifest.js"
84
+ end
85
+
86
+ it "fails if file is missing from the manifest" do
87
+ env["yyy.js"] = env["xxx.js"]
88
+ asset_path("yyy.js").should == "/assets/NOT_FOUND_IN_MANIFEST"
49
89
  end
50
90
 
51
- it "returns a path with md5 on production and complicated file" do
52
- env["xxx.yy.js"] = env["xxx.js"]
53
- asset_path("xxx.yy.js").should == "/assets/xxx-abc.yy.js"
91
+ it "fails if there is no manifest" do
92
+ run "rm #{manifest}"
93
+ expect{
94
+ asset_path("yyy.js")
95
+ }.to raise_error
54
96
  end
55
97
  end
56
98
  end
57
99
 
100
+ describe "#rails_asset_id" do
101
+ it "does not return ids for assets" do
102
+ rails_asset_id("/assets/xxx-abc.js").should == nil
103
+ @rails_asset_id.should == nil
104
+ end
105
+
106
+ it "does not return ids non-assets" do
107
+ rails_asset_id("/javascripts/xxx.js").should == :super
108
+ @rails_asset_id.should == ["/javascripts/xxx.js"]
109
+ end
110
+ end
111
+
58
112
  describe "#compute_public_path" do
59
113
  it "does nothing for symbols" do
60
114
  compute_public_path(:xxx, "a", "b").should == :super
data/spec/spec_helper.rb CHANGED
@@ -11,6 +11,18 @@ RSpec.configure do |config|
11
11
  end
12
12
  end
13
13
 
14
+ def run(cmd)
15
+ result = `#{cmd} 2>&1`
16
+ raise "FAILED #{cmd} --> #{result}" unless $?.success?
17
+ result
18
+ end
19
+
20
+ def write(file, content)
21
+ folder = File.dirname(file)
22
+ run "mkdir -p #{folder}" unless File.exist?(folder)
23
+ File.open(file, 'w'){|f| f.write content }
24
+ end
25
+
14
26
  module Rails
15
27
  def self.env
16
28
  @env || "test"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails2_asset_pipeline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -67,7 +67,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  segments:
69
69
  - 0
70
- hash: 2541193725320114517
70
+ hash: -82409223101824033
71
71
  required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  version: '0'
77
77
  segments:
78
78
  - 0
79
- hash: 2541193725320114517
79
+ hash: -82409223101824033
80
80
  requirements: []
81
81
  rubyforge_project:
82
82
  rubygems_version: 1.8.24