From 71b47cc2007d991be5cfcd31d719fd14a15847cb Mon Sep 17 00:00:00 2001 From: Alexander Kravets Date: Sun, 7 Feb 2016 07:31:44 -0800 Subject: [PATCH] Add support for sprockets-rails 3 --- lib/konacha.rb | 15 +++++++++++++++ lib/konacha/engine.rb | 23 ++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/konacha.rb b/lib/konacha.rb index 36e3eff..661b408 100644 --- a/lib/konacha.rb +++ b/lib/konacha.rb @@ -60,5 +60,20 @@ def spec_paths }.sort end end + + def precompiled_assets + %W(konacha.css + chai.js + mocha.js + konacha/parent.js + konacha/iframe.js + konacha/runner.js).concat(spec_paths).map do |path| + path.gsub(/\.js\.coffee$/, ".js").gsub(/\.coffee$/, ".js") + end + end + + def asset_precompiled?(logical_path) + precompiled_assets.include? logical_path + end end end diff --git a/lib/konacha/engine.rb b/lib/konacha/engine.rb index 79234f9..1187efc 100644 --- a/lib/konacha/engine.rb +++ b/lib/konacha/engine.rb @@ -7,11 +7,17 @@ class Engine < ::Rails::Engine config.konacha = ActiveSupport::OrderedOptions.new def self.application(app) + # Compatibility workaround for supporting both sprockets 2 and 3 + if Sprockets::VERSION.start_with? '3' + app.config.cache_classes = false + sprockets_env = Sprockets::Railtie.build_environment(app) + end + Rack::Builder.app do use Rack::ShowExceptions map app.config.assets.prefix do - run app.assets + run Sprockets::VERSION.start_with?('3') ? sprockets_env : app.assets # Compatibility workaround for supporting both sprockets 2 and 3 end map "/" do @@ -37,7 +43,6 @@ def self.formatters options.spec_matcher ||= /_spec\.|_test\./ options.port ||= 3500 options.host ||= 'localhost' - options.application ||= self.class.application(app) options.driver ||= :selenium options.stylesheets ||= %w(application) options.javascripts ||= %w(chai konacha/iframe) @@ -47,7 +52,19 @@ def self.formatters spec_dirs = [options.spec_dir].flatten app.config.assets.paths += spec_dirs.map{|d| app.root.join(d).to_s} - app.config.assets.raise_runtime_errors = false + options.application ||= self.class.application(app) + end + + # Compatibility workaround for supporting both sprockets 2 and 3 + if Sprockets::VERSION.start_with? '3' + config.after_initialize do + ActiveSupport.on_load(:action_view) do + default_checker = ActionView::Base.precompiled_asset_checker + ActionView::Base.precompiled_asset_checker = -> logical_path do + default_checker[logical_path] || Konacha.asset_precompiled?(logical_path) + end + end + end end end end