Skip to content

Disable Rails.application.assets when compile=false #220

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

Merged
merged 1 commit into from
Feb 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 41 additions & 40 deletions lib/sprockets/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,7 @@ class Configuration
remove_possible_method :assets=

# Returns Sprockets::Environment for app config.
def assets
@assets ||= Sprockets::Environment.new(root.to_s) do |env|
env.version = ::Rails.env
env.cache = Sprockets::Cache::FileStore.new("#{root}/tmp/cache")

env.context_class.class_eval do
include ::Sprockets::Rails::Context
end
end
end
attr_writer :assets
attr_accessor :assets

# Returns Sprockets::Manifest for app config.
attr_accessor :assets_manifest
Expand Down Expand Up @@ -77,47 +67,70 @@ def configure(&block)
Sprockets::Rails::Task.new(app)
end

config.after_initialize do |app|
def self.build_environment(app)
config = app.config
env = Sprockets::Environment.new(app.root.to_s)

# Copy config.assets.paths to Sprockets
config.assets.paths.each do |path|
env.append_path path
end

env.js_compressor = config.assets.js_compressor
env.css_compressor = config.assets.css_compressor

env.context_class.class_eval do
include ::Sprockets::Rails::Context
self.assets_prefix = config.assets.prefix
self.digest_assets = config.assets.digest
self.config = config.action_controller
end

# Configuration options that should invalidate
# the Sprockets cache when changed.
app.assets.version = [
env.version = [
::Rails.env,
app.assets.version,
::Rails.env, # TODO: Remove duplicate key
config.assets.version,
config.action_controller.relative_url_root,
(config.action_controller.asset_host unless config.action_controller.asset_host.respond_to?(:call)),
Sprockets::Rails::VERSION
].compact.join('-')

# Copy config.assets.paths to Sprockets
config.assets.paths.each do |path|
app.assets.append_path path
end

app.assets.js_compressor = config.assets.js_compressor
app.assets.css_compressor = config.assets.css_compressor
env.cache = Sprockets::Cache::FileStore.new("#{app.root}/tmp/cache")

# Run app.assets.configure blocks
config.assets._blocks.each do |block|
block.call app.assets
block.call(env)
end

# No more configuration changes at this point.
# With cache classes on, Sprockets won't check the FS when files
# change. Preferable in production when the FS only changes on
# deploys when the app restarts.
if config.cache_classes
app.assets = app.assets.cached
env = env.cached
end

manifest_assets_path = File.join(config.paths['public'].first, config.assets.prefix)
env
end

def self.build_manifest(app)
config = app.config
path = File.join(config.paths['public'].first, config.assets.prefix)
Sprockets::Manifest.new(app.assets, path, config.assets.manifest)
end

config.after_initialize do |app|
config = app.config

if config.assets.compile
app.assets_manifest = Sprockets::Manifest.new(app.assets, manifest_assets_path, config.assets.manifest)
else
app.assets_manifest = Sprockets::Manifest.new(manifest_assets_path, config.assets.manifest)
app.assets = self.build_environment(app)
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end
app.assets_manifest = build_manifest(app)

ActiveSupport.on_load(:action_view) do
include Sprockets::Rails::Helper
Expand All @@ -128,21 +141,9 @@ def configure(&block)
self.assets_prefix = config.assets.prefix
self.assets_precompile = config.assets.precompile

# Copy over to Sprockets as well
context = app.assets.context_class
context.assets_prefix = config.assets.prefix
context.digest_assets = config.assets.digest
context.config = config.action_controller

self.assets_environment = app.assets if config.assets.compile
self.assets_environment = app.assets
self.assets_manifest = app.assets_manifest
end

if config.assets.compile
app.routes.prepend do
mount app.assets => config.assets.prefix
end
end
end
end
end
4 changes: 2 additions & 2 deletions test/test_railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_app_asset_manifest_available_when_compile
assert_equal File.join(ROOT, "public/assets"), manifest.dir
end

def test_app_asset_available_when_no_compile
def test_app_asset_not_available_when_no_compile
app.configure do
config.assets.compile = false
end
Expand All @@ -85,7 +85,7 @@ def test_app_asset_available_when_no_compile

app.initialize!

assert app.assets
refute app.assets
end

def test_app_asset_manifest_available_when_no_compile
Expand Down