Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Pass params such as current_store/current_currency/current user to API v2 serializers by default

Damian Legawiec99ac6e3cfd61
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index 0df7ee4287..d8debcb733 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -84,6 +84,10 @@ module Spree
fields.presence
end
+ def serializer_params
+ { currency: current_currency, store: current_store, user: spree_current_user }
+ end
+
def record_not_found
render_error_payload(I18n.t(:resource_not_found, scope: 'spree.api'), 404)
end
diff --git a/api/app/controllers/spree/api/v2/resource_controller.rb b/api/app/controllers/spree/api/v2/resource_controller.rb
index 96dd4ac21c..2730029e2b 100644
--- a/api/app/controllers/spree/api/v2/resource_controller.rb
+++ b/api/app/controllers/spree/api/v2/resource_controller.rb
@@ -17,14 +17,14 @@ module Spree
def serialize_collection(collection)
collection_serializer.new(
collection,
- collection_options(collection)
+ collection_options(collection).merge(params: serializer_params)
).serializable_hash
end
def serialize_resource(resource)
resource_serializer.new(
resource,
- params: { currency: current_currency, store: current_store, user: spree_current_user },
+ params: serializer_params,
include: resource_includes,
fields: sparse_fields
).serializable_hash