Skip to content

Commit 829ad88

Browse files
committed
Merge pull request #159 from dblock/master
RAILS_ENV fix + rewrite_relative_paths option
2 parents 2bcbdff + cea2b4e commit 829ad88

10 files changed

Lines changed: 61 additions & 19 deletions

Gemfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
source 'http://rubygems.org'
2+
3+
gemspec
4+
5+
group :development, :test do
6+
gem "rake", "0.9.2"
7+
gem "rails", "2.3.11"
8+
gem "yui-compressor", "0.9.3"
9+
gem "closure-compiler", "1.1.5"
10+
gem "uglifier", "0.4.0"
11+
end
12+
13+
group :development do
14+
gem "RedCloth", "4.2.9"
15+
end

index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ <h2 id="configuration">Configuration</h2>
236236
if you're packaging assets in development.
237237
</td>
238238
</tr>
239+
<tr>
240+
<td><b>rewrite_relative_paths</b></td>
241+
<td><tt>on&nbsp;|&nbsp;off</tt></td>
242+
<td class="definition">
243+
Defaults to <b>on</b>. When <b>off</b>, relative paths with CSS will be left unchanged.
244+
</td>
245+
</tr>
239246
<tr>
240247
<td><b>gzip_assets</b></td>
241248
<td><tt>on&nbsp;|&nbsp;off</tt></td>

lib/jammit.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ module Jammit
2525
COMPRESSORS = [:yui, :closure, :uglifier]
2626

2727
DEFAULT_COMPRESSOR = :yui
28-
28+
2929
# Extension matchers for JavaScript and JST, which need to be disambiguated.
3030
JS_EXTENSION = /\.js\Z/
3131
DEFAULT_JST_EXTENSION = "jst"
@@ -35,7 +35,7 @@ module Jammit
3535
class PackageNotFound < NameError; end
3636

3737
# Jammit raises a MissingConfiguration exception when you try to load the
38-
# configuration of an assets.yml file that doesn't exist, or are missing
38+
# configuration of an assets.yml file that doesn't exist, or are missing
3939
# a piece of required configuration.
4040
class MissingConfiguration < NameError; end
4141

@@ -52,7 +52,7 @@ class << self
5252
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
5353
:javascript_compressor, :compressor_options, :css_compressor_options,
5454
:template_extension, :template_extension_matcher, :allow_debugging,
55-
:public_root
55+
:rewrite_relative_paths, :public_root
5656
attr_accessor :compressors
5757
end
5858

@@ -71,14 +71,15 @@ def self.load_configuration(config_path, soft=false)
7171
conf = YAML.load(ERB.new(File.read(config_path)).result)
7272

7373
# Optionally overwrite configuration based on the environment.
74-
rails_env = defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
74+
rails_env = (defined?(Rails) ? ::Rails.env : ENV['RAILS_ENV'] || "development")
7575
conf.merge! conf.delete rails_env if conf.has_key? rails_env
7676

7777
@config_path = config_path
7878
@configuration = symbolize_keys(conf)
7979
@package_path = conf[:package_path] || DEFAULT_PACKAGE_PATH
8080
@embed_assets = conf[:embed_assets] || conf[:embed_images]
8181
@compress_assets = !(conf[:compress_assets] == false)
82+
@rewrite_relative_paths = !(conf[:rewrite_relative_paths] == false)
8283
@gzip_assets = !(conf[:gzip_assets] == false)
8384
@allow_debugging = !(conf[:allow_debugging] == false)
8485
@mhtml_enabled = @embed_assets && @embed_assets != "datauri"

lib/jammit/compressor.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ def with_mhtml(css, asset_url)
172172
end
173173
[MHTML_START, mhtml, MHTML_END, css].flatten.join('')
174174
end
175-
175+
176176
# Return a rewritten asset URL for a new stylesheet -- the asset should
177177
# be tagged for embedding if embeddable, and referenced at the correct level
178178
# if relative.
179179
def construct_asset_path(asset_path, css_path, variant)
180180
public_path = absolute_path(asset_path, css_path)
181181
return "__EMBED__#{public_path}" if embeddable?(public_path, variant)
182-
source = asset_path.absolute? ? asset_path.to_s : relative_path(public_path)
182+
source = asset_path.absolute? || ! Jammit.rewrite_relative_paths ? asset_path.to_s : relative_path(public_path)
183183
rewrite_asset_path(source, public_path)
184184
end
185185

test/config/assets-erb.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
embed_assets: <%= RAILS_ENV == 'test' %>
1+
embed_assets: <%= ENV['RAILS_ENV'] == 'test' %>
22

33
javascripts:
44
js_test:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
javascript_compressor: yui
2+
embed_assets: on
3+
rewrite_relative_paths: off
4+
5+
javascripts:
6+
js_test:
7+
- fixtures/src/*.js
8+
jst_test:
9+
- fixtures/src/*.jst
10+
11+
stylesheets:
12+
css_test:
13+
- fixtures/src/*.css

test/fixtures/jammed/css_test-no-rewrite-relative-paths.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
ASSET_ROOT = File.expand_path(File.dirname(__FILE__))
66

77
devnull = RUBY_PLATFORM =~ /mswin|mingw|bccwin|wince|emx/ ? 'nul' : '/dev/null'
8-
RAILS_DEFAULT_LOGGER = Logger.new(devnull)
9-
RAILS_ENV = "test"
10-
RAILS_ROOT = File.expand_path(File.dirname(__FILE__))
8+
ENV['RAILS_ENV'] ||= "test"
119
ENV["RAILS_ASSET_ID"] = "101"
1210

1311
require './lib/jammit'

test/unit/test_configuration.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,42 @@ def test_disabled_compression
2424
@compressor = Compressor.new
2525
# Should not compress js.
2626
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
27-
assert packed == File.read('test/fixtures/jammed/js_test-uncompressed.js')
27+
assert_equal packed, File.read('test/fixtures/jammed/js_test-uncompressed.js')
2828
# Nothing should change with jst.
2929
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
30-
assert packed == File.read('test/fixtures/jammed/jst_test.js')
30+
assert_equal packed, File.read('test/fixtures/jammed/jst_test.js')
3131
packed = @compressor.compress_css(glob('test/fixtures/src/*.css'))
32-
assert packed == File.open('test/fixtures/jammed/css_test-uncompressed.css', 'rb') {|f| f.read }
32+
assert_equal packed, File.open('test/fixtures/jammed/css_test-uncompressed.css', 'rb') {|f| f.read }
3333
end
3434

3535
def test_css_compression
3636
assert Jammit.compress_assets
3737
assert Jammit.gzip_assets
3838
packed = @compressor.compress_css(glob('test/fixtures/src/*.css'))
39-
assert packed == File.read('test/fixtures/jammed/css_test.css')
39+
assert_equal packed, File.read('test/fixtures/jammed/css_test.css')
4040
end
4141

4242
def test_erb_configuration
4343
Jammit.load_configuration('test/config/assets-erb.yml')
4444
assert Jammit.compress_assets
4545
packed = @compressor.compress_css(glob('test/fixtures/src/*.css'))
46-
assert packed == File.read('test/fixtures/jammed/css_test.css')
46+
assert_equal packed, File.read('test/fixtures/jammed/css_test.css')
4747
end
4848

4949
def test_css_configuration
5050
Jammit.load_configuration('test/config/assets-css.yml')
5151
packed = Compressor.new.compress_css(glob('test/fixtures/src/*.css'))
52-
assert packed == File.read('test/fixtures/jammed/css_test-line-break.css')
52+
assert_equal packed, File.read('test/fixtures/jammed/css_test-line-break.css')
5353
end
5454

5555
def test_javascript_compression
5656
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
57-
assert packed == File.read('test/fixtures/jammed/js_test.js')
57+
assert_equal packed, File.read('test/fixtures/jammed/js_test.js')
5858
end
5959

6060
def test_jst_compilation
6161
packed = @compressor.compile_jst(glob('test/fixtures/src/*.jst'))
62-
assert packed == File.read('test/fixtures/jammed/jst_test.js')
62+
assert_equal packed, File.read('test/fixtures/jammed/jst_test.js')
6363
end
6464

6565
def test_environment_specific_configuration
@@ -72,4 +72,11 @@ def test_environment_specific_configuration
7272
ENV['RAILS_ENV'] = 'test'
7373
end
7474

75+
def test_no_rewrite_relative_paths
76+
Jammit.load_configuration('test/config/assets-no-rewrite-relative-paths.yml')
77+
assert !Jammit.rewrite_relative_paths
78+
packed = @compressor.compress_css(glob('test/fixtures/src/*.css'))
79+
assert_equal packed, File.read('test/fixtures/jammed/css_test-no-rewrite-relative-paths.css')
80+
end
81+
7582
end

test/unit/test_uglifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def teardown
1313

1414
def test_javascript_compression
1515
packed = @compressor.compress_js(glob('test/fixtures/src/*.js'))
16-
assert packed == File.read('test/fixtures/jammed/js_test-uglifier.js')
16+
assert_equal packed, File.read('test/fixtures/jammed/js_test-uglifier.js')
1717
end
1818

1919
def test_jst_compilation

0 commit comments

Comments
 (0)