Skip to content

Commit 038a1ea

Browse files
committed
Refactor load_user_roles into current_user_roles helper
Prior to this commit, we had a before action populating the `current_user_roles` instance variable, and then we used that instance variable in helpers. This refactors it into an actual helper that we can then use as we need.
1 parent 97c9bda commit 038a1ea

File tree

6 files changed

+20
-11
lines changed

6 files changed

+20
-11
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2025-05-28 15:33:32 UTC using RuboCop version 1.75.7.
3+
# on 2025-05-29 09:46:52 UTC using RuboCop version 1.75.7.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -51,7 +51,7 @@ Layout/ClosingHeredocIndentation:
5151
Exclude:
5252
- 'core/solidus_core.gemspec'
5353

54-
# Offense count: 94
54+
# Offense count: 95
5555
# This cop supports safe autocorrection (--autocorrect).
5656
Layout/EmptyLineAfterGuardClause:
5757
Enabled: false
@@ -343,12 +343,11 @@ Rails/FilePath:
343343
Rails/HasManyOrHasOneDependent:
344344
Enabled: false
345345

346-
# Offense count: 30
346+
# Offense count: 29
347347
# Configuration parameters: Include.
348348
# Include: **/app/helpers/**/*.rb
349349
Rails/HelperInstanceVariable:
350350
Exclude:
351-
- 'api/app/helpers/spree/api/api_helpers.rb'
352351
- 'backend/app/helpers/spree/admin/base_helper.rb'
353352
- 'backend/app/helpers/spree/admin/orders_helper.rb'
354353
- 'core/app/helpers/spree/base_helper.rb'

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class BaseController < ActionController::Base
2626
before_action :load_user
2727
before_action :authorize_for_order, if: proc { order_token.present? }
2828
before_action :authenticate_user
29-
before_action :load_user_roles
29+
# This is deprecated and will be removed in Spree 5.0
30+
before_action :load_deprecated_user_roles
3031

3132
rescue_from ActionController::ParameterMissing, with: :parameter_missing_error
3233
rescue_from ActiveRecord::RecordNotFound, with: :not_found
@@ -35,6 +36,7 @@ class BaseController < ActionController::Base
3536
rescue_from StateMachines::InvalidTransition, with: :invalid_transition
3637

3738
helper Spree::Api::ApiHelpers
39+
helper_method :current_user_roles
3840

3941
private
4042

@@ -75,8 +77,16 @@ def authenticate_user
7577
end
7678
end
7779

78-
def load_user_roles
79-
@current_user_roles = if @current_api_user
80+
def load_deprecated_user_roles
81+
@current_user_roles = if Rails.version < Gem::Version.new("7.2.0")
82+
ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :current_user_roles, :@current_user_roles, Spree.deprecator)
83+
else
84+
ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :current_user_roles, :@current_user_roles, deprecator: Spree.deprecator)
85+
end
86+
end
87+
88+
def current_user_roles
89+
@_current_user_roles ||= if @current_api_user
8090
@current_api_user.spree_roles.pluck(:name)
8191
else
8292
[]

api/app/controllers/spree/api/line_items_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def line_items_attributes
6666
def extract_metadata
6767
metadata = { customer_metadata: line_item_params[:customer_metadata] }
6868

69-
if @current_user_roles&.include?("admin")
69+
if current_user_roles&.include?("admin")
7070
metadata[:admin_metadata] = line_item_params[:admin_metadata]
7171
end
7272

api/app/helpers/spree/api/api_helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def required_fields_for(model)
6767

6868
def variant_attributes
6969
preference_attributes = Spree::Api::Config.variant_attributes
70-
if @current_user_roles&.include?("admin")
70+
if current_user_roles&.include?("admin")
7171
preference_attributes + [:cost_price]
7272
else
7373
preference_attributes

api/app/views/spree/api/payments/source_views/_gateway.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
attrs = [:id, :month, :year, :cc_type, :last_digits, :name]
4-
if @current_user_roles.include?("admin")
4+
if current_user_roles.include?("admin")
55
attrs += [:gateway_customer_profile_id, :gateway_payment_profile_id]
66
end
77

api/app/views/spree/api/products/_product.json.jbuilder

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
@product_attributes ||= product_attributes
4-
json.cache! [I18n.locale, @current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do
4+
json.cache! [I18n.locale, current_user_roles.include?('admin'), current_pricing_options, @product_attributes, @exclude_data, product] do
55
json.(product, *(@product_attributes - [:total_on_hand]))
66
json.total_on_hand(total_on_hand_for(product))
77
json.price(product.price_for_options(current_pricing_options)&.amount)

0 commit comments

Comments
 (0)