-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Match Dev Asset Experience to Production #84
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ module Helper | |
# support for Ruby 1.9.3 && Rails 3.0.x | ||
@_config = ActiveSupport::InheritableOptions.new({}) unless defined?(ActiveSupport::Configurable::Configuration) | ||
include ActiveSupport::Configurable | ||
config_accessor :raise_runtime_errors | ||
config_accessor :precompile, :assets, :raise_runtime_errors | ||
|
||
class DependencyError < StandardError | ||
def initialize(path, dep) | ||
|
@@ -18,6 +18,15 @@ def initialize(path, dep) | |
end | ||
end | ||
|
||
class AssetFilteredError < StandardError | ||
def initialize(source) | ||
msg = "Asset filtered out and will not be served: " << | ||
"add `config.assets.precompile += %w( #{source} )` " << | ||
"to `config/application.rb` and restart your server" | ||
super(msg) | ||
end | ||
end | ||
|
||
if defined? ActionView::Helpers::AssetUrlHelper | ||
include ActionView::Helpers::AssetUrlHelper | ||
include ActionView::Helpers::AssetTagHelper | ||
|
@@ -62,6 +71,21 @@ def compute_asset_path(path, options = {}) | |
end | ||
end | ||
|
||
# Computes the full URL to a asset in the public directory. This | ||
# method checks for errors before returning path. | ||
def asset_path(source, options = {}) | ||
check_errors_for(source) | ||
path_to_asset(source, options) | ||
end | ||
alias :path_to_asset_with_errors :asset_path | ||
|
||
# Computes the full URL to a asset in the public directory. This | ||
# will use +asset_path+ internally, so most of their behaviors | ||
# will be the same. | ||
def asset_url(source, options = {}) | ||
path_to_asset_with_errors(source, options.merge(:protocol => :request)) | ||
end | ||
|
||
# Get digest for asset path. | ||
# | ||
# path - String path | ||
|
@@ -104,6 +128,7 @@ def javascript_include_tag(*sources) | |
|
||
if options["debug"] != false && request_debug_assets? | ||
sources.map { |source| | ||
check_errors_for(source) | ||
if asset = lookup_asset_for_path(source, :type => :javascript) | ||
asset.to_a.map do |a| | ||
super(path_to_javascript(a.logical_path, :debug => true), options) | ||
|
@@ -123,9 +148,9 @@ def javascript_include_tag(*sources) | |
# Eventually will be deprecated and replaced by source maps. | ||
def stylesheet_link_tag(*sources) | ||
options = sources.extract_options!.stringify_keys | ||
|
||
if options["debug"] != false && request_debug_assets? | ||
sources.map { |source| | ||
check_errors_for(source) | ||
if asset = lookup_asset_for_path(source, :type => :stylesheet) | ||
asset.to_a.map do |a| | ||
super(path_to_stylesheet(a.logical_path, :debug => true), options) | ||
|
@@ -141,14 +166,33 @@ def stylesheet_link_tag(*sources) | |
end | ||
|
||
protected | ||
|
||
# Checks if the asset is included in the dependencies list. | ||
def check_dependencies!(dep) | ||
if raise_runtime_errors && !_dependency_assets.detect { |asset| asset.include?(dep) } | ||
raise DependencyError.new(self.pathname, dep) | ||
end | ||
end | ||
|
||
# Raise errors when source does not exist or is not in the precompiled list | ||
def check_errors_for(source) | ||
source = source.to_s | ||
return source if !self.raise_runtime_errors || source.blank? || source =~ URI_REGEXP | ||
asset = lookup_asset_for_path(source) | ||
|
||
if asset && asset_needs_precompile?(source, asset.pathname.to_s) | ||
raise AssetFilteredError.new(source) | ||
end | ||
end | ||
|
||
# Returns true when an asset will not be available after precompile is run | ||
def asset_needs_precompile?(source, filename) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of sort-circuits ifs. I think this would work: if assets && assets.send(:matches_filter, precompile || [], source, filename)
false
else
true
end There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i ususally like them if they let me get rid of a nested if/else or cut down on lines. In this case your suggestion is good. I will do that. |
||
if assets_environment && assets_environment.send(:matches_filter, precompile || [], source, filename) | ||
false | ||
else | ||
true | ||
end | ||
end | ||
|
||
# Enable split asset debugging. Eventually will be deprecated | ||
# and replaced by source maps in Sprockets 3.x. | ||
def request_debug_assets? | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<%= asset_path("bar.js") %> | ||
<%= asset_path("bar.js") %> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if we didn't have to nest that if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no problem with this too.