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.

Consolidate prefixed ID resolution at model level and simplify API controllers

Damian Legawiec8d7ddb74a96b
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/params_normalizer.rb b/spree/api/app/controllers/concerns/spree/api/v3/params_normalizer.rb
new file mode 100644
index 0000000000..e3eae4b8a4
--- /dev/null
+++ b/spree/api/app/controllers/concerns/spree/api/v3/params_normalizer.rb
@@ -0,0 +1,95 @@
+module Spree
+ module Api
+ module V3
+ # Normalizes flat API v3 JSON params for Rails model consumption:
+ #
+ # 1. **Prefixed ID resolution** — decodes Stripe-style prefixed IDs (e.g. "prod_86Rf07xd4z")
+ # to integer primary keys for any param ending in `_id` or `_ids`.
+ #
+ # 2. **Nested attributes normalization** — converts flat arrays (e.g. `taxon_rules: [...]`)
+ # to Rails `_attributes` format (e.g. `taxon_rules_attributes: [...]`) based on
+ # the model's `accepts_nested_attributes_for` declarations.
+ #
+ # This runs automatically in the ResourceController's `permitted_params` method,
+ # eliminating boilerplate ID resolution and nested attribute transformation in
+ # individual controllers and services.
+ module ParamsNormalizer
+ extend ActiveSupport::Concern
+
+ protected
+
+ # Override permitted_params to apply normalization after permitting
+ def permitted_params
+ normalize_params(super)
+ end
+
+ # Normalize params: resolve prefixed IDs and convert nested attributes
+ def normalize_params(permitted)
+ hash = permitted.to_h.with_indifferent_access
+ hash = resolve_prefixed_ids(hash)
+ hash = normalize_nested_attributes(hash)
+ ActionController::Parameters.new(hash).permit!
+ end
+
+ private
+
+ # Recursively resolve prefixed IDs in params.
+ #
+ # - Params ending in `_id` with string values matching prefix format → decoded to integer
+ # - Params ending in `_ids` with array values → each element decoded
+ # - Nested hashes and arrays of hashes are processed recursively
+ def resolve_prefixed_ids(hash)
+ hash.each_with_object({}.with_indifferent_access) do |(key, value), result|
+ result[key] = case
+ when key.to_s.end_with?('_id') && prefixed_id?(value)
+ decode_prefixed_id(value)
+ when key.to_s.end_with?('_ids') && value.is_a?(Array)
+ value.map { |v| prefixed_id?(v) ? decode_prefixed_id(v) : v }
+ when value.is_a?(Hash)
+ resolve_prefixed_ids(value)
+ when value.is_a?(Array)
+ value.map { |v| v.is_a?(Hash) ? resolve_prefixed_ids(v) : v }
+ else
+ value
+ end
+ end
+ end
+
+ # Convert flat nested arrays to Rails _attributes format.
+ #
+ # Uses the model's `nested_attributes_options` to detect which keys
+ # should be renamed. For example, if Spree::Taxon has
+ # `accepts_nested_attributes_for :taxon_rules`, then:
+ #
+ # { taxon_rules: [{type: "...", value: "..."}] }
+ #
+ # becomes:
+ #
+ # { taxon_rules_attributes: [{type: "...", value: "..."}] }
+ def normalize_nested_attributes(hash)
+ return hash unless model_class.respond_to?(:nested_attributes_options)
+
+ nested_keys = model_class.nested_attributes_options.keys.map(&:to_s)
+ return hash if nested_keys.empty?
+
+ hash.each_with_object({}.with_indifferent_access) do |(key, value), result|
+ key_str = key.to_s
+ if nested_keys.include?(key_str) && !key_str.end_with?('_attributes')
+ result["#{key_str}_attributes"] = value
+ else
+ result[key] = value
+ end
+ end
+ end
+
+ def prefixed_id?(value)
+ Spree::PrefixedId.prefixed_id?(value)
+ end
+
+ def decode_prefixed_id(prefixed_id_string)
+ Spree::PrefixedId.decode_prefixed_id(prefixed_id_string) || prefixed_id_string
+ end
+ end
+ end
+ end
+end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/option_types_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/option_types_controller.rb
index 45488c3118..aee062bc31 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/option_types_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/option_types_controller.rb
@@ -64,7 +64,7 @@ module Spree
def option_type_service_params
params.permit(
:name, :presentation, :position, :filterable,
- option_values: [:id, :name, :presentation, :position]
+ option_values: [:id, *Spree::PermittedAttributes.option_value_attributes]
)
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/line_items_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/line_items_controller.rb
index 5a790bdd07..b07e767f10 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/line_items_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/line_items_controller.rb
@@ -98,7 +98,7 @@ module Spree
end
def permitted_params
- params.permit(:variant_id, :quantity, { metadata: {}, options: {} })
+ params.permit(*Spree::PermittedAttributes.line_item_attributes, { options: {} })
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
index e108425115..4a00b9e64d 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/payments_controller.rb
@@ -83,7 +83,7 @@ module Spree
end
def permitted_params
- params.permit(:amount, :payment_method_id, :source_id)
+ params.permit(*Spree::PermittedAttributes.payment_attributes, :source_id)
end
private
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/shipments_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/shipments_controller.rb
index 091522cbb9..4705e8f5b5 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/shipments_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/shipments_controller.rb
@@ -111,7 +111,7 @@ module Spree
end
def permitted_params
- params.permit(:tracking, :selected_shipping_rate_id, :stock_location_id)
+ params.permit(*Spree::PermittedAttributes.shipment_attributes - [:id])
end
private
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
index cd9a165af8..533e56dbd0 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
@@ -171,7 +171,7 @@ module Spree
def resolve_user
return unless params[:user_id].present?
- Spree.user_class.find_by_prefix_id!(params[:user_id])
+ Spree.user_class.find_by_param!(params[:user_id])
end
def order_create_params
@@ -184,20 +184,12 @@ module Spree
def order_update_params
params.permit(
- :email, :special_instructions, :internal_note, :channel,
- ship_address: address_params,
- bill_address: address_params,
- line_items: [:variant_id, :quantity, { metadata: {} }]
+ *Spree::PermittedAttributes.checkout_attributes,
+ ship_address: Spree::PermittedAttributes.address_attributes,
+ bill_address: Spree::PermittedAttributes.address_attributes,
+ line_items: Spree::PermittedAttributes.line_item_attributes
)
end
-
- def address_params
- [
- :id, :firstname, :lastname, :address1, :address2,
- :city, :zipcode, :phone, :company,
- :country_iso, :state_abbr, :state_name
- ]
- end
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products/assets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products/assets_controller.rb
index 863c6e28da..3498251218 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products/assets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products/assets_controller.rb
@@ -50,7 +50,7 @@ module Spree
end
def permitted_params
- params.permit(:alt, :position, :attachment, :type, :viewable_type, :viewable_id, variant_ids: [])
+ params.permit(*Spree::PermittedAttributes.asset_attributes, variant_ids: [])
end
end
end
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index 3ada027c1b..37d589dac9 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -120,23 +120,12 @@ module Spree
CUSTOM_SORT_SCOPES.key?(sort_param)
end
- # Flat JSON params for create/update service.
- # Accepts the plan's payload format with nested variants array.
def product_service_params
params.permit(
- :name, :description, :slug, :status, :price, :sku, :barcode,
- :available_on, :make_active_at, :discontinue_on,
- :meta_description, :meta_keywords, :meta_title,
- :weight, :height, :width, :depth, :weight_unit, :dimensions_unit,
- :shipping_category_id, :tax_category_id,
- :cost_currency, :cost_price, :compare_at_price,
- :track_inventory, :backorderable,
+ *Spree::PermittedAttributes.product_attributes,
tags: [],
- taxon_ids: [],
- option_type_ids: [],
- store_ids: [],
variants: [
- :id, :sku, :barcode, :price, :compare_at_price,
+ :sku, :barcode, :price, :compare_at_price,
:cost_price, :cost_currency,
:weight, :height, :width, :depth, :weight_unit, :dimensions_unit,
:track_inventory, :tax_category_id,
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
index db37878c55..7fb565f56f 100644
--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -2,6 +2,8 @@ module Spree
module Api
module V3
class ResourceController < BaseController
+ include Spree::Api::V3::ParamsNormalizer
+
before_action :set_parent
before_action :set_resource, only: [:show, :update, :destroy]
diff --git a/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb b/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb
new file mode 100644
index 0000000000..2ae2652f3d
--- /dev/null
+++ b/spree/api/spec/controllers/spree/api/v3/params_normalizer_spec.rb
@@ -0,0 +1,117 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Api::V3::ParamsNormalizer do
+ let(:test_controller_class) do
+ Class.new(ActionController::API) do
+ include Spree::Api::V3::ParamsNormalizer
+
+ # Expose private methods for testing
+ public :resolve_prefixed_ids, :normalize_nested_attributes, :prefixed_id?, :decode_prefixed_id
+
+ def model_class
+ Spree::Taxon
+ end
+ end
+ end
+
+ let(:controller) { test_controller_class.new }
+
+ describe '#prefixed_id?' do
+ it 'returns true for prefixed IDs' do
+ expect(controller.prefixed_id?('prod_86Rf07xd4z')).to be true
+ expect(controller.prefixed_id?('variant_k5nR8xLq')).to be true
+ expect(controller.prefixed_id?('txn_abc123XYZ')).to be true
+ end
+
+ it 'returns false for non-prefixed values' do
+ expect(controller.prefixed_id?('12345')).to be false
+ expect(controller.prefixed_id?(123)).to be false
+ expect(controller.prefixed_id?(nil)).to be false
+ expect(controller.prefixed_id?('')).to be false
+ end
+ end
+
+ describe '#decode_prefixed_id' do
+ it 'decodes a prefixed ID to an integer' do
+ product = create(:product)
+ decoded = controller.decode_prefixed_id(product.prefixed_id)
+ expect(decoded).to eq(product.id)
+ end
+ end
+
+ describe '#resolve_prefixed_ids' do
+ it 'decodes _id params' do
+ product = create(:product)
+ hash = { 'product_id' => product.prefixed_id, 'name' => 'Test' }.with_indifferent_access
+ result = controller.resolve_prefixed_ids(hash)
+
+ expect(result['product_id']).to eq(product.id)
+ expect(result['name']).to eq('Test')
+ end
+
+ it 'decodes _ids array params' do
+ taxon1 = create(:taxon)
+ taxon2 = create(:taxon)
+ hash = { 'taxon_ids' => [taxon1.prefixed_id, taxon2.prefixed_id] }.with_indifferent_access
+ result = controller.resolve_prefixed_ids(hash)
+
+ expect(result['taxon_ids']).to eq([taxon1.id, taxon2.id])
+ end
+
+ it 'recurses into nested hashes' do
+ variant = create(:variant)
+ hash = { 'line_items' => [{ 'variant_id' => variant.prefixed_id, 'quantity' => 2 }] }.with_indifferent_access
+ result = controller.resolve_prefixed_ids(hash)
+
+ expect(result['line_items'][0]['variant_id']).to eq(variant.id)
+ expect(result['line_items'][0]['quantity']).to eq(2)
+ end
+
+ it 'leaves non-prefixed IDs unchanged' do
+ hash = { 'product_id' => 42, 'name' => 'Test' }.with_indifferent_access
+ result = controller.resolve_prefixed_ids(hash)
+
+ expect(result['product_id']).to eq(42)
+ end
+
+ it 'handles nested hash params' do
+ address = create(:address)
+ hash = { 'ship_address' => { 'id' => address.prefixed_id } }.with_indifferent_access
+ # 'id' doesn't end in '_id' so it stays unchanged
+ result = controller.resolve_prefixed_ids(hash)
+ expect(result['ship_address']['id']).to eq(address.prefixed_id)
+ end
+ end
+
+ describe '#normalize_nested_attributes' do
+ it 'converts flat nested keys to _attributes format' do
+ hash = {
+ 'name' => 'Categories',
+ 'taxon_rules' => [{ 'type' => 'Spree::TaxonRule::IncludeTag', 'value' => 'sale' }]
+ }.with_indifferent_access
+
+ result = controller.normalize_nested_attributes(hash)
+
+ expect(result).not_to have_key('taxon_rules')
+ expect(result['taxon_rules_attributes']).to eq([{ 'type' => 'Spree::TaxonRule::IncludeTag', 'value' => 'sale' }])
+ expect(result['name']).to eq('Categories')
+ end
+
+ it 'does not rename keys that already have _attributes suffix' do
+ hash = {
+ 'taxon_rules_attributes' => [{ 'type' => 'Spree::TaxonRule::IncludeTag' }]
+ }.with_indifferent_access
+
+ result = controller.normalize_nested_attributes(hash)
+
+ expect(result['taxon_rules_attributes']).to eq([{ 'type' => 'Spree::TaxonRule::IncludeTag' }])
+ end
+
+ it 'does not rename keys that are not nested attributes' do
+ hash = { 'name' => 'Test', 'position' => 1 }.with_indifferent_access
+ result = controller.normalize_nested_attributes(hash)
+
+ expect(result).to eq(hash)
+ end
+ end
+end
diff --git a/spree/core/app/models/concerns/spree/prefixed_id.rb b/spree/core/app/models/concerns/spree/prefixed_id.rb
index af948414d1..77c29822ea 100644
--- a/spree/core/app/models/concerns/spree/prefixed_id.rb
+++ b/spree/core/app/models/concerns/spree/prefixed_id.rb
@@ -20,6 +20,35 @@ module Spree
class_attribute :_prefix_id_prefix, instance_writer: false
end
+ # Automatically resolve prefixed ID strings for belongs_to foreign keys.
+ # e.g., product.assign_attributes(tax_category_id: "tc_86Rf07xd4z") will
+ # decode the prefixed ID to the integer primary key.
+ def assign_attributes(new_attributes)
+ return super if new_attributes.blank?
+
+ attrs = new_attributes.to_h
+ needs_resolution = attrs.any? do |key, value|
+ key_s = key.to_s
+ (value.is_a?(String) && key_s.end_with?('_id') && Spree::PrefixedId.prefixed_id?(value)) ||
+ (value.is_a?(Array) && key_s.end_with?('_ids') && value.any? { |v| Spree::PrefixedId.prefixed_id?(v) })
+ end
+
+ return super unless needs_resolution
+
+ resolved = attrs.each_with_object({}.with_indifferent_access) do |(key, value), hash|
+ key_s = key.to_s
+ if value.is_a?(String) && key_s.end_with?('_id') && Spree::PrefixedId.prefixed_id?(value)
+ hash[key] = self.class.resolve_prefixed_id_for_attribute(key_s, value)
+ elsif value.is_a?(Array) && key_s.end_with?('_ids')
+ hash[key] = self.class.resolve_prefixed_ids_for_attribute(key_s, value)
+ else
+ hash[key] = value
+ end
+ end
+
+ super(resolved)
+ end
+
# Returns the Stripe-style prefixed ID, or nil for unsaved records.
def prefixed_id
return nil unless id.present?
@@ -36,35 +65,82 @@ module Spree
prefixed_id.presence || super
end
+ # Module-level methods for use without a model context (e.g., from ParamsNormalizer)
+ def self.prefixed_id?(value)
+ value.is_a?(String) && value.match?(/\A[a-z]+_[a-zA-Z0-9]+\z/)
+ end
+
+ def self.decode_prefixed_id(prefixed_id_string)
+ return nil if prefixed_id_string.blank?
+
+ parts = prefixed_id_string.to_s.split('_', 2)
+ return nil if parts.length != 2
+
+ _prefix, encoded = parts
+ ids = SQIDS.decode(encoded)
+ ids.first
+ end
+
class_methods do
def has_prefix_id(prefix)
self._prefix_id_prefix = prefix.to_s
end
+ def prefixed_id?(value)
+ Spree::PrefixedId.prefixed_id?(value)
+ end
+
+ # Memoized map of foreign_key → belongs_to reflection for prefixed ID resolution.
+ def belongs_to_reflections_by_fk
+ @belongs_to_reflections_by_fk ||= reflect_on_all_associations(:belongs_to)
+ .reject(&:polymorphic?)
+ .index_by { |a| a.foreign_key.to_s }
+ end
+
+ # Resolve a prefixed ID string for a belongs_to foreign key attribute.
+ # Uses the association's target class to validate the record exists.
+ def resolve_prefixed_id_for_attribute(attribute_name, prefixed_id_value)
+ reflection = belongs_to_reflections_by_fk[attribute_name]
+
+ if reflection
+ reflection.klass.find_by_param!(prefixed_id_value).id
+ else
+ Spree::PrefixedId.decode_prefixed_id(prefixed_id_value) || prefixed_id_value
+ end
+ end
+
+ # Resolve an array of prefixed IDs for a has_many _ids setter.
+ # Infers the target class from the association name (e.g., taxon_ids → taxons → Spree::Taxon).
+ def resolve_prefixed_ids_for_attribute(attribute_name, values)
+ association_name = attribute_name.sub(/_ids$/, '').pluralize
+ reflection = reflect_on_association(association_name.to_sym)
+ klass = reflection&.klass
+
+ values.map do |v|
+ if Spree::PrefixedId.prefixed_id?(v)
+ klass ? klass.find_by_param!(v).id : (Spree::PrefixedId.decode_prefixed_id(v) || v)
+ else
+ v
+ end
+ end
+ end
+
def find_by_prefix_id!(prefixed_id)
- decoded = decode_prefixed_id(prefixed_id)
+ decoded = Spree::PrefixedId.decode_prefixed_id(prefixed_id)
raise ActiveRecord::RecordNotFound.new("Couldn't find #{name} with prefixed id=#{prefixed_id}", name) unless decoded
find(decoded)
end
def find_by_prefix_id(prefixed_id)
- decoded = decode_prefixed_id(prefixed_id)
+ decoded = Spree::PrefixedId.decode_prefixed_id(prefixed_id)
return nil unless decoded
find_by(id: decoded)
end
- # Decode a prefixed ID string (e.g., "prod_86Rf07xd4z") to the integer primary key.
def decode_prefixed_id(prefixed_id_string)
- return nil if prefixed_id_string.blank?
-
- parts = prefixed_id_string.to_s.split('_', 2)
- return nil if parts.length != 2
-
- _prefix, encoded = parts
- ids = Spree::PrefixedId::SQIDS.decode(encoded)
- ids.first
+ Spree::PrefixedId.decode_prefixed_id(prefixed_id_string)
end
# Find by prefixed ID first, falling back to integer id for backwards compatibility.
diff --git a/spree/core/app/models/spree/wished_item.rb b/spree/core/app/models/spree/wished_item.rb
index 42130bf896..40fa45f2c3 100644
--- a/spree/core/app/models/spree/wished_item.rb
+++ b/spree/core/app/models/spree/wished_item.rb
@@ -16,19 +16,6 @@ module Spree
validates :variant, uniqueness: { scope: [:wishlist] }
validates :quantity, numericality: { only_integer: true, greater_than: 0 }
- # This is a workaround to allow the variant_id to be set with a prefixed ID
- # in the API.
- #
- # @param id [String] the prefixed ID of the variant
- def variant_id=(id)
- if id.to_s.include?('_')
- decoded = Spree::Variant.decode_prefixed_id(id)
- super(decoded)
- else
- super(id)
- end
- end
-
def price(currency)
variant.amount_in(currency[:currency])
end
diff --git a/spree/core/app/services/spree/carts/upsert_items.rb b/spree/core/app/services/spree/carts/upsert_items.rb
index 63e124e24e..f7a3e96c4c 100644
--- a/spree/core/app/services/spree/carts/upsert_items.rb
+++ b/spree/core/app/services/spree/carts/upsert_items.rb
@@ -57,16 +57,16 @@ module Spree
private
- def resolve_variant(store, prefixed_id)
- return nil if prefixed_id.blank?
+ def resolve_variant(store, variant_id)
+ return nil if variant_id.blank?
- variant = store.variants.find_by_prefix_id(prefixed_id)
+ variant = store.variants.find_by_param(variant_id)
raise ActiveRecord::RecordNotFound.new(
- "Variant '#{prefixed_id}' not found in this store",
+ "Variant '#{variant_id}' not found in this store",
'Spree::Variant',
- 'prefix_id',
- prefixed_id
+ 'id',
+ variant_id
) unless variant
variant
diff --git a/spree/core/app/services/spree/products/create.rb b/spree/core/app/services/spree/products/create.rb
index cf9e440c83..317042e1fb 100644
--- a/spree/core/app/services/spree/products/create.rb
+++ b/spree/core/app/services/spree/products/create.rb
@@ -15,7 +15,7 @@ module Spree
def create_product(store:, params:)
params = params.to_h.with_indifferent_access
variants_params = params.delete(:variants)
- product_params = resolve_ids(params)
+ product_params = params
product_params[:store_ids] = [store.id] if product_params[:store_ids].blank?
# Map tags array to tag_list
@@ -26,8 +26,10 @@ module Spree
shipping_category_id = product_params.delete(:shipping_category_id)
product = Spree::Product.new(product_params)
- product.tax_category_id = tax_category_id if tax_category_id.present?
- product.shipping_category_id = shipping_category_id if shipping_category_id.present?
+ overrides = {}
+ overrides[:tax_category_id] = tax_category_id if tax_category_id.present?
+ overrides[:shipping_category_id] = shipping_category_id if shipping_category_id.present?
+ product.assign_attributes(overrides) if overrides.any?
if product.save
success(product: product, store: store, variants_params: variants_params)
@@ -62,7 +64,6 @@ module Spree
end
unless variant.save
- raise ActiveRecord::Rollback
return failure(variant, variant.errors)
end
@@ -106,29 +107,6 @@ module Spree
)
end
- def resolve_ids(params)
- params = params.to_h.with_indifferent_access
-
- if params[:tax_category_id].present?
- params[:tax_category_id] = resolve_prefix_id(Spree::TaxCategory, params[:tax_category_id])
- end
-
- if params[:shipping_category_id].present?
- params[:shipping_category_id] = resolve_prefix_id(Spree::ShippingCategory, params[:shipping_category_id])
- end
-
- if params[:taxon_ids].present?
- params[:taxon_ids] = params[:taxon_ids].map { |id| resolve_prefix_id(Spree::Taxon, id) }
- end
-
- params
- end
-
- def resolve_prefix_id(klass, id)
- return id unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- klass.find_by_prefix_id!(id).id
- end
end
end
end
diff --git a/spree/core/app/services/spree/products/update.rb b/spree/core/app/services/spree/products/update.rb
index 37121f93e9..d5d422d1da 100644
--- a/spree/core/app/services/spree/products/update.rb
+++ b/spree/core/app/services/spree/products/update.rb
@@ -15,7 +15,7 @@ module Spree
def update_product(product:, store:, params:)
params = params.to_h.with_indifferent_access
variants_params = params.delete(:variants)
- product_params = resolve_ids(params)
+ product_params = params
# Map tags array to tag_list
product_params[:tag_list] = product_params.delete(:tags) if product_params.key?(:tags)
@@ -69,7 +69,6 @@ module Spree
variant.assign_attributes(variant_data.except(:id))
unless variant.save
- raise ActiveRecord::Rollback
return failure(variant, variant.errors)
end
@@ -113,29 +112,6 @@ module Spree
)
end
- def resolve_ids(params)
- params = params.to_h.with_indifferent_access
-
- if params[:tax_category_id].present?
- params[:tax_category_id] = resolve_prefix_id(Spree::TaxCategory, params[:tax_category_id])
- end
-
- if params[:shipping_category_id].present?
- params[:shipping_category_id] = resolve_prefix_id(Spree::ShippingCategory, params[:shipping_category_id])
- end
-
- if params[:taxon_ids].present?
- params[:taxon_ids] = params[:taxon_ids].map { |id| resolve_prefix_id(Spree::Taxon, id) }
- end
-
- params
- end
-
- def resolve_prefix_id(klass, id)
- return id unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- klass.find_by_prefix_id!(id).id
- end
end
end
end
diff --git a/spree/core/app/services/spree/variants/create.rb b/spree/core/app/services/spree/variants/create.rb
index dc30d3b87f..674ef09119 100644
--- a/spree/core/app/services/spree/variants/create.rb
+++ b/spree/core/app/services/spree/variants/create.rb
@@ -21,8 +21,6 @@ module Spree
option_value_name = params.delete(:option_value)
total_on_hand = params.delete(:total_on_hand)
- resolve_ids!(params)
-
variant = product.variants.build(params)
if option_type_name.present? && option_value_name.present?
@@ -121,22 +119,8 @@ module Spree
variant.option_values = [option_value]
end
- def resolve_ids!(params)
- if params[:tax_category_id].present?
- params[:tax_category_id] = resolve_prefix_id(Spree::TaxCategory, params[:tax_category_id])
- end
- end
-
- def resolve_prefix_id(klass, id)
- return id unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- klass.find_by_prefix_id!(id).id
- end
-
def resolve_stock_location(id)
- return Spree::StockLocation.find(id) unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- Spree::StockLocation.find_by_prefix_id!(id)
+ Spree::StockLocation.find_by_param!(id)
end
end
end
diff --git a/spree/core/app/services/spree/variants/update.rb b/spree/core/app/services/spree/variants/update.rb
index 8e3cedef27..731d64d156 100644
--- a/spree/core/app/services/spree/variants/update.rb
+++ b/spree/core/app/services/spree/variants/update.rb
@@ -21,8 +21,6 @@ module Spree
option_value_name = params.delete(:option_value)
total_on_hand = params.delete(:total_on_hand)
- resolve_ids!(params)
-
if option_type_name.present? && option_value_name.present?
assign_option_value(variant.product, variant, option_type_name, option_value_name)
end
@@ -122,22 +120,8 @@ module Spree
variant.option_values = [option_value]
end
- def resolve_ids!(params)
- if params[:tax_category_id].present?
- params[:tax_category_id] = resolve_prefix_id(Spree::TaxCategory, params[:tax_category_id])
- end
- end
-
- def resolve_prefix_id(klass, id)
- return id unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- klass.find_by_prefix_id!(id).id
- end
-
def resolve_stock_location(id)
- return Spree::StockLocation.find(id) unless id.is_a?(String) && id.match?(/\A[a-z]+_/)
-
- Spree::StockLocation.find_by_prefix_id!(id)
+ Spree::StockLocation.find_by_param!(id)
end
end
end