Skip to content

Commit c410165

Browse files
authored
Merge pull request #6246 from mamhoff/current-api-user-helper
Refactor "current_api_user" into instacached helper
2 parents 6426bb3 + 5dad30f commit c410165

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

api/app/controllers/spree/api/base_controller.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class BaseController < ActionController::Base
2121
class_attribute :admin_metadata_attributes
2222
self.admin_metadata_attributes = [{ admin_metadata: {} }]
2323

24-
attr_accessor :current_api_user
25-
26-
before_action :load_user
24+
before_action :deprecated_load_user
2725
before_action :authorize_for_order, if: proc { order_token.present? }
2826
before_action :authenticate_user
2927
# This is deprecated and will be removed in Spree 5.0
@@ -36,7 +34,7 @@ class BaseController < ActionController::Base
3634
rescue_from StateMachines::InvalidTransition, with: :invalid_transition
3735

3836
helper Spree::Api::ApiHelpers
39-
helper_method :current_user_roles
37+
helper_method :current_user_roles, :current_api_user
4038

4139
private
4240

@@ -63,12 +61,12 @@ def permitted_user_attributes
6361
can?(:admin, Spree.user_class) ? super + admin_metadata_attributes : super
6462
end
6563

66-
def load_user
67-
@current_api_user ||= Spree.user_class.find_by(spree_api_key: api_key.to_s)
64+
def current_api_user
65+
@_current_api_user ||= Spree.user_class.find_by(spree_api_key: api_key.to_s)
6866
end
6967

7068
def authenticate_user
71-
unless @current_api_user
69+
unless current_api_user
7270
if requires_authentication? && api_key.blank? && order_token.blank?
7371
render "spree/api/errors/must_specify_api_key", status: :unauthorized
7472
elsif order_token.blank? && (requires_authentication? || api_key.present?)
@@ -85,9 +83,17 @@ def load_deprecated_user_roles
8583
end
8684
end
8785

86+
def deprecated_load_user
87+
@current_api_user = if Rails.version < Gem::Version.new("7.2.0")
88+
ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :current_api_user, :@current_api_user, Spree.deprecator)
89+
else
90+
ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :current_api_user, :@current_api_user, deprecator: Spree.deprecator)
91+
end
92+
end
93+
8894
def current_user_roles
89-
@_current_user_roles ||= if @current_api_user
90-
@current_api_user.spree_roles.pluck(:name)
95+
@_current_user_roles ||= if current_api_user
96+
current_api_user.spree_roles.pluck(:name)
9197
else
9298
[]
9399
end

api/lib/spree/api/testing_support/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def stub_authentication!
3030
# This method can be overridden (with a let block) inside a context
3131
# For instance, if you wanted to have an admin user instead.
3232
def current_api_user
33-
@current_api_user ||= stub_model(Spree::LegacyUser, email: "[email protected]", spree_roles: [])
33+
@_current_api_user ||= stub_model(Spree::LegacyUser, email: "[email protected]", spree_roles: [])
3434
end
3535

3636
def image(filename)

api/spec/requests/spree/api/orders_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ module Spree::Api
512512

513513
it "assigns email when creating a new order" do
514514
post spree.api_orders_path, params: { order: { email: "[email protected]" } }
515-
expect(json_response['email']).not_to eq controller.current_api_user
515+
expect(json_response['email']).not_to eq current_api_user.email
516516
expect(json_response['email']).to eq "[email protected]"
517517
end
518518

0 commit comments

Comments
 (0)