From b33699ddaadc33834ed307359ec8c044de66bad6 Mon Sep 17 00:00:00 2001 From: Martin Tomov Date: Wed, 3 May 2017 22:36:55 +0100 Subject: [PATCH] Configure Turbolinks for admin area - Enabled for new applications - Disabled for existing applications. Enable in an initializer with: # config/initializers/spree.rb Spree::Backend::Config.configure do |config| config.use_turbolinks = true end - Handles gracefully existing javascript code, which still uses jQuery.ready and gives a warning of the location of those functions. --- .../app/assets/javascripts/spree/backend.js | 1 + .../backend/turbolinks-configuration.js.erb | 20 +++++++++++++++++++ .../app/models/spree/backend_configuration.rb | 3 +++ .../spree/backend_configuration_spec.rb | 9 +++++++++ .../templates/config/initializers/solidus.rb | 1 + 5 files changed, 34 insertions(+) create mode 100644 backend/app/assets/javascripts/spree/backend/turbolinks-configuration.js.erb create mode 100644 backend/spec/models/spree/backend_configuration_spec.rb diff --git a/backend/app/assets/javascripts/spree/backend.js b/backend/app/assets/javascripts/spree/backend.js index cc6ba8ce52e..be4622932b6 100644 --- a/backend/app/assets/javascripts/spree/backend.js +++ b/backend/app/assets/javascripts/spree/backend.js @@ -18,6 +18,7 @@ //= require spree/backend/namespaces //= require spree/backend/translation //= require spree/backend/backbone-overrides +//= require spree/backend/turbolinks-configuration //= require spree/backend/format_money // //= require spree/backend/templates diff --git a/backend/app/assets/javascripts/spree/backend/turbolinks-configuration.js.erb b/backend/app/assets/javascripts/spree/backend/turbolinks-configuration.js.erb new file mode 100644 index 00000000000..097338eec6a --- /dev/null +++ b/backend/app/assets/javascripts/spree/backend/turbolinks-configuration.js.erb @@ -0,0 +1,20 @@ +// comment line necessary for correct interpolation of use_turbolinks +Turbolinks.supported = <%= Spree::Backend::Config.use_turbolinks %>; + +Spree.jQueryReady = $.fn.ready; + +// override jQuery.ready to use Spree.ready even if it was not used explicitly +$.fn.ready = function (callback) { + console.warn("jQuery.ready() is deprecated. Use Spree.ready() instead. Called from:", callback ); + Spree.ready(callback); +}; + +Spree.ready = function(callback) { + if (Turbolinks.supported) { + jQuery(document).on('turbolinks:load', function() { + callback(jQuery); + }); + } else { + Spree.jQueryReady(callback); + } +}; diff --git a/backend/app/models/spree/backend_configuration.rb b/backend/app/models/spree/backend_configuration.rb index 967508e2617..32f3c67e086 100644 --- a/backend/app/models/spree/backend_configuration.rb +++ b/backend/app/models/spree/backend_configuration.rb @@ -2,6 +2,9 @@ module Spree class BackendConfiguration < Preferences::Configuration preference :locale, :string, default: Rails.application.config.i18n.default_locale + # @!attribute [rw] use_turbolinks + preference :use_turbolinks, :boolean, default: false + ORDER_TABS ||= [:orders, :payments, :creditcard_payments, :shipments, :credit_cards, :return_authorizations, :customer_returns, :adjustments, :customer_details] diff --git a/backend/spec/models/spree/backend_configuration_spec.rb b/backend/spec/models/spree/backend_configuration_spec.rb new file mode 100644 index 00000000000..0cc41d1cdde --- /dev/null +++ b/backend/spec/models/spree/backend_configuration_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' + +describe Spree::BackendConfiguration, type: :model do + let(:prefs) { Spree::Backend::Config } + + describe '#use_turbolinks' do + specify { expect(prefs).to respond_to(:use_turbolinks) } + end +end diff --git a/core/lib/generators/spree/install/templates/config/initializers/solidus.rb b/core/lib/generators/spree/install/templates/config/initializers/solidus.rb index 931c891e094..99b7522b928 100644 --- a/core/lib/generators/spree/install/templates/config/initializers/solidus.rb +++ b/core/lib/generators/spree/install/templates/config/initializers/solidus.rb @@ -65,6 +65,7 @@ config.use_static_preferences! config.locale = 'en' + config.use_turbolinks = true end <% end -%>