aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/asset_tag_helper.rb
diff options
context:
space:
mode:
authorChristos Zisopoulos <christos@42linesofcode.com>2009-05-29 15:42:44 +0200
committerMichael Koziarski <michael@koziarski.com>2009-06-26 16:47:15 +1200
commit18a97a66017452dbe6cf6881c69d7a7dedc7a7bd (patch)
tree627920ffa4d8f0083b17ea91811b6d946230f3e7 /actionpack/lib/action_view/helpers/asset_tag_helper.rb
parent8ee60660cec54f008ddaa54a4e8e06d099d8c7f5 (diff)
downloadrails-18a97a66017452dbe6cf6881c69d7a7dedc7a7bd.tar.gz
rails-18a97a66017452dbe6cf6881c69d7a7dedc7a7bd.tar.bz2
rails-18a97a66017452dbe6cf6881c69d7a7dedc7a7bd.zip
Handle missing javascript/stylesheets assets by raising an exception
An exception will be raised if a local javascript/stylesheet file included by the stylesheet_link_tag or javascript_include_tag can not be found. When caching is enabled, we use atomic_write to ensure that the cache file is not created with zero length. Signed-off-by: Michael Koziarski <michael@koziarski.com> [#2738 state:committed]
Diffstat (limited to 'actionpack/lib/action_view/helpers/asset_tag_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb32
1 files changed, 28 insertions, 4 deletions
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index babb9db38a..14cdc7a025 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -1,6 +1,7 @@
require 'cgi'
require 'action_view/helpers/url_helper'
require 'action_view/helpers/tag_helper'
+require 'active_support/core_ext/file'
module ActionView
module Helpers #:nodoc:
@@ -285,7 +286,7 @@ module ActionView
end
javascript_src_tag(joined_javascript_name, options)
else
- expand_javascript_sources(sources, recursive).collect { |source| javascript_src_tag(source, options) }.join("\n")
+ ensure_javascript_sources!(expand_javascript_sources(sources, recursive)).collect { |source| javascript_src_tag(source, options) }.join("\n")
end
end
@@ -434,7 +435,7 @@ module ActionView
end
stylesheet_tag(joined_stylesheet_name, options)
else
- expand_stylesheet_sources(sources, recursive).collect { |source| stylesheet_tag(source, options) }.join("\n")
+ ensure_stylesheet_sources!(expand_stylesheet_sources(sources, recursive)).collect { |source| stylesheet_tag(source, options) }.join("\n")
end
end
@@ -664,13 +665,28 @@ module ActionView
end
end
+ def ensure_stylesheet_sources!(sources)
+ sources.each do |source|
+ asset_file_path!(path_to_stylesheet(source))
+ end
+ return sources
+ end
+
+ def ensure_javascript_sources!(sources)
+ sources.each do |source|
+ asset_file_path!(path_to_javascript(source))
+ end
+ return sources
+ end
+
def join_asset_file_contents(paths)
- paths.collect { |path| File.read(asset_file_path(path)) }.join("\n\n")
+ paths.collect { |path| File.read(asset_file_path!(path)) }.join("\n\n")
end
def write_asset_file_contents(joined_asset_path, asset_paths)
+
FileUtils.mkdir_p(File.dirname(joined_asset_path))
- File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }
+ File.atomic_write(joined_asset_path) { |cache| cache.write(join_asset_file_contents(asset_paths)) }
# Set mtime to the latest of the combined files to allow for
# consistent ETag without a shared filesystem.
@@ -682,6 +698,14 @@ module ActionView
File.join(ASSETS_DIR, path.split('?').first)
end
+ def asset_file_path!(path)
+ unless path =~ %r{^[-a-z]+://}
+ absolute_path = asset_file_path(path)
+ raise(Errno::ENOENT, "Asset file not found at '#{absolute_path}'" ) unless File.exist?(absolute_path)
+ return absolute_path
+ end
+ end
+
def collect_asset_files(*path)
dir = path.first