From 1237e6aa69b964ad4d533d1310ec321558733106 Mon Sep 17 00:00:00 2001 From: Elia Schito Date: Tue, 14 Jan 2020 14:53:35 +0100 Subject: [PATCH] Only require the necessary Rails frameworks By requiring rails/all Solidus was loading a bunch of unnecessary code taking more time and consuming more memory. This change allows apps that want to cherry-pick Rails frameworks to benefit from the more attentive choice. --- backend/lib/spree/backend.rb | 5 ++++- backend/spec/lib/spree/backend_spec.rb | 21 +++++++++++++++++++++ core/lib/spree/core.rb | 9 ++++++++- core/lib/spree/testing_support/dummy_app.rb | 1 + core/spec/lib/spree/core_spec.rb | 21 +++++++++++++++++++++ frontend/lib/spree/frontend.rb | 6 +++++- frontend/spec/lib/spree/frontend_spec.rb | 21 +++++++++++++++++++++ 7 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 backend/spec/lib/spree/backend_spec.rb create mode 100644 core/spec/lib/spree/core_spec.rb create mode 100644 frontend/spec/lib/spree/frontend_spec.rb diff --git a/backend/lib/spree/backend.rb b/backend/lib/spree/backend.rb index 9fdf01fad4c..8b03724b502 100644 --- a/backend/lib/spree/backend.rb +++ b/backend/lib/spree/backend.rb @@ -1,6 +1,9 @@ # frozen_string_literal: true -require 'rails/all' +require "active_record/railtie" +require "action_view/railtie" +require "sprockets/railtie" + require 'jquery-rails' require 'coffee-rails' require 'sassc-rails' diff --git a/backend/spec/lib/spree/backend_spec.rb b/backend/spec/lib/spree/backend_spec.rb new file mode 100644 index 00000000000..0fbd323cce6 --- /dev/null +++ b/backend/spec/lib/spree/backend_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'spree/backend' + +RSpec.describe Spree::Backend do + it 'loads only the necessary Rails Frameworks' do + aggregate_failures do + expect(defined? ActionCable::Engine).to be_falsey + expect(defined? ActionController::Railtie).to be_truthy + expect(defined? ActionMailer::Railtie).to be_truthy + expect(defined? ActionView::Railtie).to be_truthy + expect(defined? ActiveJob::Railtie).to be_truthy + expect(defined? ActiveModel::Railtie).to be_truthy + expect(defined? ActiveRecord::Railtie).to be_truthy + expect(defined? ActiveStorage::Engine).to be_falsey + expect(defined? Rails::TestUnit::Railtie).to be_falsey + expect(defined? Sprockets::Railtie).to be_truthy + end + end +end diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb index 73953bb1ab5..e5c163668a9 100644 --- a/core/lib/spree/core.rb +++ b/core/lib/spree/core.rb @@ -1,6 +1,13 @@ # frozen_string_literal: true -require 'rails/all' +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_view/railtie" +require "sprockets/railtie" + require 'acts_as_list' require 'awesome_nested_set' require 'cancan' diff --git a/core/lib/spree/testing_support/dummy_app.rb b/core/lib/spree/testing_support/dummy_app.rb index 48e0b911b56..b080a6c7fac 100644 --- a/core/lib/spree/testing_support/dummy_app.rb +++ b/core/lib/spree/testing_support/dummy_app.rb @@ -7,6 +7,7 @@ require 'active_record/railtie' require 'action_controller/railtie' require 'action_mailer/railtie' +require 'active_storage/engine' Rails.env = 'test' diff --git a/core/spec/lib/spree/core_spec.rb b/core/spec/lib/spree/core_spec.rb new file mode 100644 index 00000000000..f8cea005358 --- /dev/null +++ b/core/spec/lib/spree/core_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'spree/core' + +RSpec.describe Spree::Core do + it 'loads only the necessary Rails Frameworks' do + aggregate_failures do + expect(defined? ActionCable::Engine).to be_falsey + expect(defined? ActionController::Railtie).to be_truthy + expect(defined? ActionMailer::Railtie).to be_truthy + expect(defined? ActionView::Railtie).to be_truthy + expect(defined? ActiveJob::Railtie).to be_truthy + expect(defined? ActiveModel::Railtie).to be_truthy + expect(defined? ActiveRecord::Railtie).to be_truthy + expect(defined? ActiveStorage::Engine).to be_truthy + expect(defined? Rails::TestUnit::Railtie).to be_falsey + expect(defined? Sprockets::Railtie).to be_truthy + end + end +end diff --git a/frontend/lib/spree/frontend.rb b/frontend/lib/spree/frontend.rb index 9362e9aaa6a..ca1e38ea617 100644 --- a/frontend/lib/spree/frontend.rb +++ b/frontend/lib/spree/frontend.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true -require 'rails/all' +require "active_record/railtie" +require "action_controller/railtie" +require "action_view/railtie" +require "sprockets/railtie" + require 'jquery-rails' require 'canonical-rails' require 'sassc-rails' diff --git a/frontend/spec/lib/spree/frontend_spec.rb b/frontend/spec/lib/spree/frontend_spec.rb new file mode 100644 index 00000000000..317775db74f --- /dev/null +++ b/frontend/spec/lib/spree/frontend_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'spree/frontend' + +RSpec.describe Spree::Frontend do + it 'loads only the necessary Rails Frameworks' do + aggregate_failures do + expect(defined? ActionCable::Engine).to be_falsey + expect(defined? ActionController::Railtie).to be_truthy + expect(defined? ActionMailer::Railtie).to be_truthy + expect(defined? ActionView::Railtie).to be_truthy + expect(defined? ActiveJob::Railtie).to be_truthy + expect(defined? ActiveModel::Railtie).to be_truthy + expect(defined? ActiveRecord::Railtie).to be_truthy + expect(defined? ActiveStorage::Engine).to be_falsey + expect(defined? Rails::TestUnit::Railtie).to be_falsey + expect(defined? Sprockets::Railtie).to be_truthy + end + end +end