Skip to content

Instantly share code, notes, and snippets.

@ikraamg
Created August 31, 2022 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ikraamg/47b6db5aecac1080b4608eb9ad0337d5 to your computer and use it in GitHub Desktop.
Save ikraamg/47b6db5aecac1080b4608eb9ad0337d5 to your computer and use it in GitHub Desktop.
diff --git a/core/lib/spree/core/controller_helpers/auth.rb b/core/lib/spree/core/controller_helpers/auth.rb
index 3f21ebb7c6..2112505391 100644
--- a/core/lib/spree/core/controller_helpers/auth.rb
+++ b/core/lib/spree/core/controller_helpers/auth.rb
@@ -54,6 +54,11 @@ module Spree
Spree::UserLastUrlStorer.new(self).store_location
end
+ # Auth extensions are expected to define it, otherwise it's a no-op
+ def spree_current_user
+ defined?(super) ? super : nil
+ end
+
# proxy method to *possible* spree_current_user method
# Authentication extensions (such as spree_auth_devise) are meant to provide spree_current_user
def try_spree_current_user
diff --git a/core/lib/spree/testing_support/dummy_app.rb b/core/lib/spree/testing_support/dummy_app.rb
index c1fc56b4e8..ddb3f39c91 100644
--- a/core/lib/spree/testing_support/dummy_app.rb
+++ b/core/lib/spree/testing_support/dummy_app.rb
@@ -18,9 +18,6 @@ RAILS_6_OR_ABOVE = Rails.gem_version >= Gem::Version.new('6.0')
# @private
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
-
- def spree_current_user
- end
end
# @private
diff --git a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
index 2c3716048a..9a127472c8 100644
--- a/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/auth_spec.rb
@@ -114,4 +114,27 @@ RSpec.describe Spree::Core::ControllerHelpers::Auth, type: :controller do
expect(response).to redirect_to('/unauthorized')
end
end
+
+ describe "#spree_current_user" do
+ context "when an ancestor defines it" do
+ it "delegates" do
+ controller = Class.new(ApplicationController) do
+ include (Module.new do
+ def spree_current_user
+ :user
+ end
+ end)
+ include Spree::Core::ControllerHelpers::Auth
+ end.new
+
+ expect(controller.spree_current_user).to eq :user
+ end
+ end
+
+ context "when no ancestor defines it" do
+ it "returns nil" do
+ expect(controller.spree_current_user).to eq nil
+ end
+ end
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment