Skip to content

Commit

Permalink
Fix compatibility issue with Sprockets Rails 3.0.0
Browse files Browse the repository at this point in the history
Sprockets Rails now unset `Rails.application.assets` when
`config.assets.compile` is false, causing an issue as we were relying on
that hash. The workaround here is to read the manifest file directly
by using `Sprockets::Railtie.build_manifest`.

Note that this patch requires the manifest file to exists, so you have
to run `rake assets:precompile` to generate the manifest file first.

Also adding Rails 4.2 using Sprockets Rails 2.3 to the build matrix.

See issue rails/sprockets-rails#237 for more detail.

Fix #12
  • Loading branch information
sikachu committed Jan 26, 2016
1 parent 7790c00 commit ae7ee69
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ gemfile:
- gemfiles/rails_4_0.gemfile
- gemfiles/rails_4_1.gemfile
- gemfiles/rails_4_2.gemfile
- gemfiles/rails_4_2_sprockets_2.gemfile
- gemfiles/rails_5_0.gemfile

matrix:
Expand Down
5 changes: 5 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ appraise "rails-4-2" do
gem "rails", "~> 4.2.0"
end

appraise "rails-4-2-sprockets-2" do
gem "rails", "~> 4.2.0"
gem "sprockets-rails", "~> 2.3.0"
end

appraise "rails-5-0" do
gem "rails", "~> 5.0.0.beta1"
end
8 changes: 8 additions & 0 deletions gemfiles/rails_4_2_sprockets_2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rails", "~> 4.2.0"
gem "sprockets-rails", "~> 2.3.0"

gemspec :path => "../"
5 changes: 4 additions & 1 deletion lib/sprockets-redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module Sprockets
class RedirectRailtie < ::Rails::Railtie
initializer "insert_sprockets_redirect_middleware" do |app|
if !::Rails.configuration.assets.compile && ::Rails.configuration.assets.digest
app.middleware.insert 0, Sprockets::Redirect, ::Rails.application.assets,
app.middleware.insert 0,
Sprockets::Redirect,
::Rails.application.assets ||
Sprockets::Railtie.build_manifest(::Rails.application).assets,
:prefix => ::Rails.configuration.assets.prefix,
:asset_host => ::Rails.configuration.action_controller.asset_host
end
Expand Down
6 changes: 5 additions & 1 deletion lib/sprockets/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ def logical_path
end

def digest_path
@environment[logical_path].digest_path
if @environment[logical_path].respond_to?(:digest_path)
@environment[logical_path].digest_path
else
@environment[logical_path]
end
end

# Sends a redirect header back to browser
Expand Down
12 changes: 12 additions & 0 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_build_app_and_add_gem
create_rails_app
append_gemfile
create_test_file
precompile_assets
assert_middleware_injection
assert_asset_redirection
end
Expand Down Expand Up @@ -49,6 +50,13 @@ def append_gemfile
f.puts
f.puts 'gem "sprockets-redirect", :path => "../../"'
f.puts 'gem "test-unit"'

begin
require "sprockets/rails/version"
f.puts %(gem "sprockets-rails", "#{Sprockets::Rails::VERSION}")
rescue LoadError
# Unable to determine Sprockets::Rails version, so we just ignore it.
end
end

reset_bundler_environment_variables
Expand Down Expand Up @@ -80,6 +88,10 @@ def test_asset_redirection
end
end

def precompile_assets
in_app_dir { run_command "bundle exec rake assets:precompile" }
end

def assert_middleware_injection
output = in_app_dir { run_command 'bundle exec rake middleware' }
assert_match(/use Sprockets::Redirect/, output)
Expand Down

0 comments on commit ae7ee69

Please sign in to comment.