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 `Tags::BulkAdd` and `Tags::BulkRemove` services
Damian Legawiec97d6f47c6ddc
diff --git a/core/app/services/spree/tags/bulk_add.rb b/core/app/services/spree/tags/bulk_add.rbnew file mode 100644index 0000000000..6cd266fa59--- /dev/null+++ b/core/app/services/spree/tags/bulk_add.rb@@ -0,0 +1,38 @@+module Spree+ module Tags+ class BulkAdd+ prepend Spree::ServiceModule::Base++ # Adds the given tags to the given records.+ #+ # @param tag_names [Array<String>] eg. ['tag1', 'tag2']+ # @param records [Array<Spree::Taggable>] eg. Spree::Product, Spree::User+ # @param context [String] default: 'tags'+ # @return [Spree::ServiceModule::Base::Result]+ def call(tag_names: [], records: [], context: 'tags')+ tags = tag_names.map do |tag_name|+ ActsAsTaggableOn::Tag.find_or_create_by(name: tag_name.strip)+ end++ record_class = records.first.class++ taggings_to_upsert = records.pluck(:id).map do |record_id|+ tags.map do |tag|+ {+ taggable_id: record_id,+ taggable_type: record_class.to_s,+ context: context,+ tag_id: tag.id+ }+ end+ end.flatten.compact++ return if taggings_to_upsert.empty?++ ActsAsTaggableOn::Tagging.insert_all(taggings_to_upsert)++ record_class.where(id: records.pluck(:id)).touch_all+ end+ end+ end+enddiff --git a/core/app/services/spree/tags/bulk_remove.rb b/core/app/services/spree/tags/bulk_remove.rbnew file mode 100644index 0000000000..f29c59e693--- /dev/null+++ b/core/app/services/spree/tags/bulk_remove.rb@@ -0,0 +1,24 @@+module Spree+ module Tags+ class BulkRemove+ prepend Spree::ServiceModule::Base++ def call(tag_names: [], records: [], context: 'tags')+ tag_ids = ActsAsTaggableOn::Tag.where(name: tag_names.map(&:strip)).pluck(:id)++ return if tag_ids.empty?++ record_class = records.first.class++ ActsAsTaggableOn::Tagging.where(+ taggable_id: records.pluck(:id),+ taggable_type: record_class.to_s,+ context: context,+ tag_id: tag_ids+ ).delete_all++ record_class.where(id: records.pluck(:id)).touch_all+ end+ end+ end+enddiff --git a/core/spec/services/spree/tags/bulk_add_spec.rb b/core/spec/services/spree/tags/bulk_add_spec.rbnew file mode 100644index 0000000000..970e020339--- /dev/null+++ b/core/spec/services/spree/tags/bulk_add_spec.rb@@ -0,0 +1,63 @@+require 'spec_helper'++module Spree+ describe Tags::BulkAdd do+ let(:tag_names) { ['tag1', 'tag2', 'tag3'] }+ let(:products) { create_list(:product, 3) }+ let(:context) { 'tags' }++ describe '#call' do+ subject { described_class.call(tag_names: tag_names, records: products, context: context) }++ it 'creates tags for each tag name' do+ expect { subject }.to change { ActsAsTaggableOn::Tag.count }.by(tag_names.size)+ end++ it 'creates taggings for each product-tag pair' do+ expect { subject }.to change { ActsAsTaggableOn::Tagging.count }.by(products.size * tag_names.size)+ end++ it 'assigns correct attributes to taggings' do+ subject+ taggings = ActsAsTaggableOn::Tagging.last(products.size * tag_names.size)++ taggings.each do |tagging|+ expect(tagging.taggable_type).to eq('Spree::Product')+ expect(products.pluck(:id)).to include(tagging.taggable_id)+ expect(tag_names).to include(tagging.tag.name)+ expect(tagging.context).to eq(context)+ end+ end++ it 'touches all products' do+ expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }+ end++ context 'when tag names are duplicated or have extra spaces' do+ let(:tag_names) { ['tag1', ' tag2 ', 'tag1', 'tag3'] }++ it 'creates unique tags without extra spaces' do+ expect { subject }.to change { ActsAsTaggableOn::Tag.count }.by(3)+ expect(ActsAsTaggableOn::Tag.pluck(:name)).to match_array(['tag1', 'tag2', 'tag3'])+ end+ end++ context 'when no records are provided' do+ let(:products) { [] }++ it 'does not create any taggings' do+ expect { subject }.not_to change { ActsAsTaggableOn::Tagging.count }+ end+ end++ context 'when no tag names are provided' do+ let(:tag_names) { [] }++ it 'does not create any tags or taggings' do+ expect { subject }.not_to change { ActsAsTaggableOn::Tag.count }+ expect { subject }.not_to change { ActsAsTaggableOn::Tagging.count }+ end+ end+ end+ end+enddiff --git a/core/spec/services/spree/tags/bulk_remove_spec.rb b/core/spec/services/spree/tags/bulk_remove_spec.rbnew file mode 100644index 0000000000..3db11812fa--- /dev/null+++ b/core/spec/services/spree/tags/bulk_remove_spec.rb@@ -0,0 +1,37 @@+require 'spec_helper'++module Spree+ describe Tags::BulkRemove do+ let(:tag_names) { ['tag1', 'tag2', 'tag3'] }+ let(:products) { create_list(:product, 3) }+ let(:context) { 'tags' }++ describe '#call' do+ subject { described_class.call(tag_names: tag_names, records: products, context: context) }++ before do+ # Add tags to products initially+ Spree::Tags::BulkAdd.call(tag_names: tag_names, records: products, context: context)+ end++ it 'removes taggings for each product-tag pair' do+ expect { subject }.to change { ActsAsTaggableOn::Tagging.count }.by(-(products.size * tag_names.size))+ end++ it 'does not remove tags' do+ expect { subject }.not_to change { ActsAsTaggableOn::Tag.count }+ end++ it 'removes correct taggings' do+ subject+ products.each do |product|+ expect(product.tag_list_on(context)).to be_empty+ end+ end++ it 'touches all products' do+ expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }+ end+ end+ end+end