Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First pass Rails 4 asset pipeline support

- Remove terrible dependencies spec. Probably shoud be replaced with an
  integration suite or better objects.
  • Loading branch information...
commit bf3ed7ec3aa6fe42ea3bd49a65014313f38cdf8a 1 parent 343460e
@ragaskar ragaskar authored
View
2  lib/jasmine.rb
@@ -23,6 +23,6 @@
require File.join('jasmine', file)
end
-require File.join('jasmine', "railtie") if Jasmine::Dependencies.rails3?
+require File.join('jasmine', "railtie") if Jasmine::Dependencies.use_railties?
View
43 lib/jasmine/asset_bundle.rb
@@ -5,14 +5,23 @@ def self.factory
if Jasmine::Dependencies.rails3?
return Rails3AssetBundle
end
+ if Jasmine::Dependencies.rails4?
+ return Rails4AssetBundle
+ end
end
- class Rails3AssetBundle
-
+ class RailsAssetBundle
def initialize(pathname)
@pathname = pathname
end
+ private
+
+ attr_reader :pathname
+ end
+
+ class Rails3AssetBundle < RailsAssetBundle
+
def assets
context = get_asset_context
context.asset_paths.asset_for(pathname, 'js').to_a.map do |path|
@@ -21,15 +30,39 @@ def assets
end
private
-
- attr_reader :pathname
-
def get_asset_context
context = ::Rails.application.assets.context_class
context.extend(::Sprockets::Helpers::IsolatedHelper)
context.extend(::Sprockets::Helpers::RailsHelper)
end
+ end
+
+ class Rails4AssetBundle
+
+ def initialize(pathname)
+ @pathname = pathname
+ end
+
+ def assets
+ context.get_original_assets(pathname)
+ end
+ private
+ attr_reader :pathname
+
+ def context
+ return @context if @context
+ @context = ActionView::Base.new
+ @context.instance_eval do
+ def get_original_assets(pathname)
+ lookup_asset_for_path(pathname, :type => :javascript).to_a.map do |processed_asset|
+ path_to_javascript(processed_asset.logical_path)
+ end
+ end
+ end
+ @context
+
+ end
end
end
View
19 lib/jasmine/asset_pipeline_utility.rb
@@ -1,19 +0,0 @@
-module Jasmine
- class AssetPipelineUtility
- def self.bundled_asset_factory(pathname, ext)
- context.asset_paths.asset_for(pathname, 'js')
- end
-
- def self.asset_path_for(filepath)
- context.asset_path(filepath)
- end
-
- def self.context
- return @context if @context
- @context = ::Rails.application.assets.context_class
- @context.extend(::Sprockets::Helpers::IsolatedHelper)
- @context.extend(::Sprockets::Helpers::RailsHelper)
- end
-
- end
-end
View
11 lib/jasmine/config.rb
@@ -35,7 +35,7 @@ def self.initialize_config
@config.add_rack_app(Rack::Head)
@config.add_rack_app(Rack::Jasmine::CacheControl)
- if Jasmine::Dependencies.rails_3_asset_pipeline?
+ if Jasmine::Dependencies.use_asset_pipeline?
@config.add_path_mapper(lambda { |config|
asset_expander = Jasmine::AssetExpander.new(Jasmine::AssetBundle.factory)
Jasmine::AssetPipelineMapper.new(config, asset_expander.method(:expand))
@@ -45,8 +45,13 @@ def self.initialize_config
# triggers run_load_hooks on action_view which, in turn, causes sprockets/railtie to load the Sprockets asset
# helpers. Alternatively, you can include the helpers yourself without loading action_view/base:
Rails.application.assets.context_class.instance_eval do
- include ::Sprockets::Helpers::IsolatedHelper
- include ::Sprockets::Helpers::RailsHelper
+ if Jasmine::Dependencies.rails3?
+ include ::Sprockets::Helpers::IsolatedHelper
+ include ::Sprockets::Helpers::RailsHelper
+ end
+ if Jasmine::Dependencies.rails4?
+ include ::Sprockets::Rails::Helper
+ end
end
Rails.application.assets
})
View
18 lib/jasmine/dependencies.rb
@@ -15,15 +15,23 @@ def legacy_rails?
end
def rails3?
- safe_gem_check("rails", ">= 3.0") && running_rails3?
+ safe_gem_check("rails", "~> 3") && running_rails3?
+ end
+
+ def rails4?
+ safe_gem_check("rails", "~> 4.0.0.rc1") && running_rails4?
end
def legacy_rack?
!defined?(Rack::Server)
end
- def rails_3_asset_pipeline?
- rails3? && Rails.respond_to?(:application) && Rails.application.respond_to?(:assets) && Rails.application.assets
+ def use_asset_pipeline?
+ (rails3? || rails4?) && Rails.respond_to?(:application) && Rails.application.respond_to?(:assets) && Rails.application.assets
+ end
+
+ def use_railties?
+ rails3? || rails4?
end
private
@@ -40,6 +48,10 @@ def running_rails3?
running_rails? && Rails.version.to_i == 3
end
+ def running_rails4?
+ running_rails? && Rails.version.to_i == 4
+ end
+
def running_rails?
defined?(Rails) && Rails.respond_to?(:version)
end
View
2  lib/jasmine/tasks/jasmine.rake
@@ -26,7 +26,7 @@ namespace :jasmine do
RSpec::Core::RakeTask.new(:jasmine_continuous_integration_runner) do |t|
t.rspec_opts = ["--colour", "--format", ENV['JASMINE_SPEC_FORMAT'] || "progress"]
t.verbose = true
- if Jasmine::Dependencies.rails_3_asset_pipeline?
+ if Jasmine::Dependencies.use_asset_pipeline?
t.rspec_opts += ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
end
t.pattern = [Jasmine.runner_filepath]
View
315 spec/dependencies_spec.rb
@@ -1,315 +0,0 @@
-require 'spec_helper'
-
-#rspec 1 fails to stub respond_to
-if Jasmine::Dependencies.rspec2?
- describe Jasmine::Dependencies do
- module Rails
- end
-
- context "with ruby_gems > 1.8" do
- before do
- Gem::Specification.should_receive(:respond_to?).with(:find_by_name).and_return(true)
- end
-
- describe ".rspec2?" do
- subject { Jasmine::Dependencies.rspec2? }
- context "when rspec 2 is present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rspec", ">= 2.0").and_return(true)
- end
- it { should be_true }
- end
- context "when rspec 2 is not present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rspec", ">= 2.0").and_raise(Gem::LoadError)
- end
- it { should be_false }
- end
- end
-
- describe ".rails2?" do
- subject { Jasmine::Dependencies.rails2? }
- context "when rails 2 is present and running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "~> 2.3").and_return(true)
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:running_rails_version) { "2.3.11" }
- it { should be_true }
- end
- context "when rails 2 is present but not running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "~> 2.3").and_raise(Gem::LoadError)
- end
- it { should be_false }
- end
- context "when rails 2 is not present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "~> 2.3").and_raise(Gem::LoadError)
- end
- it { should be_false }
- end
- end
-
- describe ".legacy_rails?" do
- subject { Jasmine::Dependencies.legacy_rails? }
- context "when rails < 2.3.11 is present and running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "< 2.3.11").and_return(true)
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:running_rails_version) { "2.3.8" }
- it { should be_true }
- end
- context "when rails < 2.3.11 is present but not running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "< 2.3.11").and_return(true)
- end
- it { should be_false }
- end
- context "when rails < 2.3.11 is not present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", "< 2.3.11").and_raise(Gem::LoadError)
- end
- it { should be_false }
- end
- end
-
- describe ".rails3?" do
- subject { Jasmine::Dependencies.rails3? }
- context "when rails 3 is present and running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:running_rails_version) { "3.2" }
- it { should be_true }
- end
- context "when rails 3 is present but not running" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- end
- it { should be_false }
- end
- context "when rails 3 is not present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_raise(Gem::LoadError)
- end
- it { should be_false }
- end
- end
-
- describe ".rails_3_asset_pipeline?" do
- subject { Jasmine::Dependencies.rails_3_asset_pipeline? }
- let(:application) { double(:application) }
- let(:running_rails_version) { "3.2" }
- before do
- Rails.stub(:respond_to?).with(:application).and_return(respond_to_application)
- Rails.stub(:application).and_return(application)
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- context "when rails 3 is present and the application pipeline is in use" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- application.stub(:assets).and_return(rails_application_assets)
- end
- let(:rails3_present) { true }
- let(:respond_to_application) { true }
- let(:rails_application_assets) { true }
- it { should be_true }
- end
- context "when rails 3 is present and the application pipeline is not in use" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- application.stub(:assets).and_return(rails_application_assets)
- end
- let(:rails3_present) { true }
- let(:respond_to_application) { true }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- context "when rails 3 is present but not loaded" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- application.stub(:assets).and_return(rails_application_assets)
- end
- let(:rails3_present) { true }
- let(:respond_to_application) { false }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- context "when rails 3 is present but doesn't respond to assets" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_return(true)
- end
- let(:rails3_present) { true }
- let(:respond_to_application) { true }
- it { should be_false }
- end
- context "when rails 3 is not present" do
- before do
- Gem::Specification.should_receive(:find_by_name).with("rails", ">= 3.0").and_raise(Gem::LoadError)
- end
- let(:rails3_present) { false }
- let(:respond_to_application) { false }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- end
- end
-
- context "with ruby_gems < 1.8" do
- before do
- Gem::Specification.should_receive(:respond_to?).with(:find_by_name).and_return(false)
- end
-
- describe ".rspec2?" do
- subject { Jasmine::Dependencies.rspec2? }
- before do
- Gem.should_receive(:available?).with("rspec", ">= 2.0").and_return(rspec2_present)
- end
- context "when rspec 2 is present" do
- let(:rspec2_present) { true }
- it { should be_true }
- end
- context "when rspec 2 is not present" do
- let(:rspec2_present) { false }
- it { should be_false }
- end
- end
-
- describe ".rails2?" do
- subject { Jasmine::Dependencies.rails2? }
- before do
- Gem.should_receive(:available?).with("rails", "~> 2.3").and_return(rails2_present)
- end
- context "when rails 2 is present and running" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
-
- let(:running_rails_version) { "2.3" }
- let(:rails2_present) { true }
- it { should be_true }
- end
- context "when rails 2 is present but not running" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
-
- let(:running_rails_version) { "3.2" }
- let(:rails2_present) { true }
- it { should be_false }
- end
- context "when rails 2 is not present" do
- let(:rails2_present) { false }
- it { should be_false }
- end
- end
-
- describe ".legacy_rails?" do
- subject { Jasmine::Dependencies.legacy_rails? }
- before do
- Gem.should_receive(:available?).with("rails", "< 2.3.11").and_return(legacy_rails_present)
- end
- context "when rails < 2.3.11 is present and running" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:running_rails_version) { "2.3.8" }
- let(:legacy_rails_present) { true }
- it { should be_true }
- end
-
- context "when rails < 2.3.11 is present but not running" do
- let(:legacy_rails_present) { true }
- it { should be_false }
- end
-
- context "when rails < 2.3.11 is not present" do
- let(:legacy_rails_present) { false }
- it { should be_false }
- end
- end
-
- describe ".rails3?" do
- subject { Jasmine::Dependencies.rails3? }
- before do
- Gem.should_receive(:available?).with("rails", ">= 3.0").and_return(rails3_present)
- end
- context "when rails 3 is present" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:running_rails_version) { "3.2" }
- let(:rails3_present) { true }
- it { should be_true }
- end
- context "when rails 3 is not present" do
- let(:rails3_present) { false }
- it { should be_false }
- end
- end
-
- describe ".rails_3_asset_pipeline?" do
- subject { Jasmine::Dependencies.rails_3_asset_pipeline? }
- let(:application) { double(:application, :assets => rails_application_assets)}
- before do
- Gem.should_receive(:available?).with("rails", ">= 3.0").and_return(rails3_present)
- Rails.stub(:respond_to?).with(:application).and_return(respond_to_application)
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:application).and_return(application)
- end
-
- context "when rails 3 is present, running, and the application pipeline is in use" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:rails3_present) { true }
- let(:running_rails_version) { "3.2" }
- let(:respond_to_application) { true }
- let(:rails_application_assets) { true }
- it { should be_true }
- end
- context "when rails 3 is present, running, and the application pipeline is not in use" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(true)
- Rails.stub(:version).and_return(running_rails_version)
- end
- let(:rails3_present) { true }
- let(:running_rails_version) { "3.2" }
- let(:respond_to_application) { true }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- context "when rails 3 is present but not loaded" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(false)
- end
- let(:rails3_present) { true }
- let(:respond_to_application) { false }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- context "when rails 3 is not present" do
- before do
- Rails.stub(:respond_to?).with(:version).and_return(false)
- end
- let(:rails3_present) { false }
- let(:respond_to_application) { false }
- let(:rails_application_assets) { false }
- it { should be_false }
- end
- end
- end
- end
-end
Please sign in to comment.
Something went wrong with that request. Please try again.