Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add functions that check for availaibility of spree components like b… #11847

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions core/app/controllers/spree/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Spree::BaseController < ApplicationController
include Spree::Core::ControllerHelpers::StrongParameters
include Spree::Core::ControllerHelpers::Locale
include Spree::Core::ControllerHelpers::Currency
include Spree::Core::ControllerHelpers::GemChecking

respond_to :html
end
1 change: 1 addition & 0 deletions core/lib/spree/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class DestroyWithOrdersError < StandardError; end
require 'spree/core/controller_helpers/strong_parameters'
require 'spree/core/controller_helpers/locale'
require 'spree/core/controller_helpers/currency'
require 'spree/core/controller_helpers/gem_checking'

require 'spree/core/preferences/store'
require 'spree/core/preferences/scoped_store'
26 changes: 26 additions & 0 deletions core/lib/spree/core/controller_helpers/gem_checking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Spree
module Core
module ControllerHelpers
module GemChecking
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a name like ComponentAvailability (I don't like this one 100% but maybe there's something similar) would be more clear here?

extend ActiveSupport::Concern

def backend_available?
@backend_available ||= Gem::Specification.find_all_by_name('spree_backend').any?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also do:

Suggested change
@backend_available ||= Gem::Specification.find_all_by_name('spree_backend').any?
@backend_available ||= Gem.loaded_specs.keys.include?('spree_backend')

But regardless, there is a potential issue here I think - this will return true when the Gem itself is present but it's not required anywhere i.e. it's in the Gemfile but not loaded into the application (I don't remember the semantics here but I'm pretty sure this is how it behaves). In that case, we say that something is available while it's not.

This shouldn't be an issue for most cases but I wouldn't be surprised if it yields something unexpected for some obscure use case. Probably that's why originally the functions checked whether the engines were loaded. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the moment it's done using:
::Rails::Engine.subclasses.map(&:instance).map { |e| e.class.to_s }.include?('Spree::Backend::Engine')
The reason for changing it to Gem is becouse we had some issues in the past with this way of checking in spree_auth_devise meaning that the order of gems in Gemfile had to be correct or it wouldn't work, which is not ideal.
I don't think it was ever an issue in spree_spree though, so maybe if spree_auth_devise uses functions provided by spree core it wouldn't be an issue (I could test it, try changing order of gems etc.).
Also I think those functions in spree_auth_devise were copied from here: https://github.com/spree/spree/blob/main/core/lib/spree/core/components.rb so I think we can just use that insted of moving it to controller helper.

end

def frontend_available?
@frontend_available ||= Gem::Specification.find_all_by_name('spree_frontend').any?
end

def api_available?
@api_available ||= Gem::Specification.find_all_by_name('spree_api').any?
end

def emails_available?
@emails_available ||= Gem::Specification.find_all_by_name('spree_emails').any?
end
end
end
end
end