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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added Classifications::Reposition service
Damian Legawiecf4524b1c74e6
diff --git a/api/app/controllers/spree/api/v1/classifications_controller.rb b/api/app/controllers/spree/api/v1/classifications_controller.rbindex 4cdc52a315..5ad7b9a3f7 100644--- a/api/app/controllers/spree/api/v1/classifications_controller.rb+++ b/api/app/controllers/spree/api/v1/classifications_controller.rb@@ -9,9 +9,10 @@ module Spreeproduct_id: params[:product_id],taxon_id: params[:taxon_id])- # Because position we get back is 0-indexed.- # acts_as_list is 1-indexed.- classification.insert_at(params[:position].to_i + 1)+ Spree::Dependencies.classification_reposition_service.constantize.call(+ classification: classification,+ position: params[:position]+ )head :okendenddiff --git a/core/app/models/spree/app_dependencies.rb b/core/app/models/spree/app_dependencies.rbindex d0d9d8f28d..151c98ecd7 100644--- a/core/app/models/spree/app_dependencies.rb+++ b/core/app/models/spree/app_dependencies.rb@@ -12,7 +12,8 @@ module Spree:completed_order_finder, :order_sorter, :cart_compare_line_items_service, :collection_paginator, :products_sorter,:products_finder, :taxon_finder, :line_item_by_variant_finder, :cart_estimate_shipping_rates_service,:account_create_address_service, :account_update_address_service, :account_create_service, :account_update_service,- :address_finder, :collection_sorter, :error_handler, :current_store_finder, :cart_empty_service, :cart_destroy_service+ :address_finder, :collection_sorter, :error_handler, :current_store_finder, :cart_empty_service, :cart_destroy_service,+ :classification_reposition_service].freezeattr_accessor *INJECTION_POINTS@@ -70,6 +71,8 @@ module Spree@account_create_address_service = 'Spree::Account::Addresses::Create'@account_update_address_service = 'Spree::Account::Addresses::Update'+ @classification_reposition_service = 'Spree::Classifications::Reposition'+@error_handler = 'Spree::ErrorReporter'enddiff --git a/core/app/services/spree/classifications/reposition.rb b/core/app/services/spree/classifications/reposition.rbnew file mode 100644index 0000000000..7490679b27--- /dev/null+++ b/core/app/services/spree/classifications/reposition.rb@@ -0,0 +1,18 @@+module Spree+ module Classifications+ class Reposition+ prepend Spree::ServiceModule::Base++ def call(classification:, position:)+ if position.is_a?(String) && !position.match(/^\d+$/)+ return failure(nil, I18n.t('errors.messages.not_a_number'))+ end++ # Because position we get back is 0-indexed.+ # acts_as_list is 1-indexed.+ classification.insert_at(position.to_i + 1)+ success(classification)+ end+ end+ end+enddiff --git a/core/spec/services/spree/classifications/reposition_spec.rb b/core/spec/services/spree/classifications/reposition_spec.rbnew file mode 100644index 0000000000..98bbf06a93--- /dev/null+++ b/core/spec/services/spree/classifications/reposition_spec.rb@@ -0,0 +1,24 @@+require 'spec_helper'++module Spree+ describe Classifications::Reposition do+ subject { described_class }++ let(:classification) { create(:classification) }++ context 'success' do+ let(:call) { described_class.call(classification: classification, position: 3) }++ it { expect(call.success?).to be_truthy }+ it { expect(call.value).to eq(classification) }+ it { expect { call }.to change(classification.reload, :position).to(4) }+ end++ context 'failure' do+ let(:call) { described_class.call(classification: classification, position: 'invalid') }++ it { expect(call.success?).to be_falsey }+ it { expect(call.error.to_s).to eq('is not a number') }+ end+ end+end