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

Commit

Permalink
Ensure that CDN path is emitted with built assets
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhitley committed May 1, 2012
1 parent 03a6d07 commit 3ae866c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion app/helpers/requirejs_helper.rb
Expand Up @@ -54,7 +54,10 @@ def requirejs_include_tag(name=nil, &block)
paths = {}
modules.each { |m| paths[m] = _javascript_path(m).sub /\.js$/,'' }

# Override uesr paths, whose mappings are only relevant in dev mode
# Add paths for assets specified by full URL (on a CDN)
run_config['paths'].each { |k,v| paths[k] = v if v =~ /^https?:/ }

# Override user paths, whose mappings are only relevant in dev mode
# and in the build_config.
run_config['paths'] = paths
end
Expand Down
2 changes: 1 addition & 1 deletion lib/requirejs/rails/config.rb
Expand Up @@ -114,7 +114,7 @@ def build_config
end

def run_config
unless self.has_key(:run_config)
unless self.has_key?(:run_config)
self[:run_config] = { "baseUrl" => "/assets" }
self[:run_config].merge!(self.user_config).slice!(*self.run_config_whitelist)
end
Expand Down
27 changes: 26 additions & 1 deletion test/requirejs-rails_test.rb
Expand Up @@ -51,7 +51,7 @@ def setup

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

test "run_config should allow settings to be overridden" do
@cfg.run_config['baseUrl'] = 'http://cdn.example.com/assets'
assert_equal 'http://cdn.example.com/assets', @cfg.run_config['baseUrl']
end

test "build_config should inherit user_config settings" do
@cfg.user_config = { 'paths' => { 'jquery' => 'lib/jquery-1.7.2.min' } }
refute_nil @cfg.build_config['paths']
Expand Down Expand Up @@ -121,6 +126,10 @@ class RequirejsHelperTest < ActionView::TestCase

def setup
controller.requirejs_included = false
Rails.application.config.requirejs.user_config = { 'paths' =>
{ 'jquery' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js' }
}

end

def wrap(tag)
Expand Down Expand Up @@ -153,4 +162,20 @@ def wrap(tag)
render :text => "#{requirejs_include_tag}\n#{requirejs_include_tag}"
end
end

test "requirejs_include_tag with CDN asset in paths" do
render :text => wrap(requirejs_include_tag)
assert_select "script:first-of-type", :text => %r{var require =.*paths.*http://ajax}
end

test "requirejs_include_tag with CDN asset and digested asset paths" do
begin
saved_digest = Rails.application.config.assets.digest
Rails.application.config.assets.digest = true
render :text => wrap(requirejs_include_tag)
assert_select "script:first-of-type", :text => %r{var require =.*paths.*http://ajax}
ensure
Rails.application.config.assets.digest = saved_digest
end
end
end

0 comments on commit 3ae866c

Please sign in to comment.