Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit 3ae866c

Browse files
committed
Ensure that CDN path is emitted with built assets
1 parent 03a6d07 commit 3ae866c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

app/helpers/requirejs_helper.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ def requirejs_include_tag(name=nil, &block)
5454
paths = {}
5555
modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/,'' }
5656

57-
# Override uesr paths, whose mappings are only relevant in dev mode
57+
# Add paths for assets specified by full URL (on a CDN)
58+
run_config['paths'].each { |k,v| paths[k] = v if v =~ /^https?:/ }
59+
60+
# Override user paths, whose mappings are only relevant in dev mode
5861
# and in the build_config.
5962
run_config['paths'] = paths
6063
end

lib/requirejs/rails/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def build_config
114114
end
115115

116116
def run_config
117-
unless self.has_key(:run_config)
117+
unless self.has_key?(:run_config)
118118
self[:run_config] = { "baseUrl" => "/assets" }
119119
self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
120120
end

test/requirejs-rails_test.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def setup
5151

5252
test "user_config should reject baseUrl" do
5353
exc = assert_raises Requirejs::ConfigError do
54-
@cfg.user_config = { "baseUrl" => "/frobnitz" }
54+
@cfg.user_config = { 'baseUrl' => '/frobnitz' }
5555
end
5656
assert_match /baseUrl is not needed/, exc.message
5757
end
@@ -63,6 +63,11 @@ def setup
6363
assert_equal 'lib/jquery-1.7.2.min', @cfg.run_config['paths']['jquery']
6464
end
6565

66+
test "run_config should allow settings to be overridden" do
67+
@cfg.run_config['baseUrl'] = 'http://cdn.example.com/assets'
68+
assert_equal 'http://cdn.example.com/assets', @cfg.run_config['baseUrl']
69+
end
70+
6671
test "build_config should inherit user_config settings" do
6772
@cfg.user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
6873
refute_nil @cfg.build_config['paths']
@@ -121,6 +126,10 @@ class RequirejsHelperTest < ActionView::TestCase
121126

122127
def setup
123128
controller.requirejs_included = false
129+
Rails.application.config.requirejs.user_config = { 'paths' =>
130+
{ 'jquery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' }
131+
}
132+
124133
end
125134

126135
def wrap(tag)
@@ -153,4 +162,20 @@ def wrap(tag)
153162
render :text => "#{requirejs_include_tag}\n#{requirejs_include_tag}"
154163
end
155164
end
165+
166+
test "requirejs_include_tag with CDN asset in paths" do
167+
render :text => wrap(requirejs_include_tag)
168+
assert_select "script:first-of-type", :text => %r{var require =.*paths.*http://ajax}
169+
end
170+
171+
test "requirejs_include_tag with CDN asset and digested asset paths" do
172+
begin
173+
saved_digest = Rails.application.config.assets.digest
174+
Rails.application.config.assets.digest = true
175+
render :text => wrap(requirejs_include_tag)
176+
assert_select "script:first-of-type", :text => %r{var require =.*paths.*http://ajax}
177+
ensure
178+
Rails.application.config.assets.digest = saved_digest
179+
end
180+
end
156181
end

0 commit comments

Comments
 (0)