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.

Fix: Store-scoped the tag vocabulary for every taggable and for bulk-added tags (#14959)

Damian Legawiec360f3e73fb05
diff --git a/docs/developer/upgrades/5.6-to-6.0.mdx b/docs/developer/upgrades/5.6-to-6.0.mdx
index 21d6cea3f9..06a38183c9 100644
--- a/docs/developer/upgrades/5.6-to-6.0.mdx
+++ b/docs/developer/upgrades/5.6-to-6.0.mdx
@@ -196,6 +196,18 @@ Rows written before the upgrade carry an id and an empty type; this task fills
the type in. Until it runs they still read as the staff member they always
were, with a deprecation warning. Safe to re-run at any time.
+### Stamp the store on tags added in bulk
+
+```bash
+bundle exec rake spree:upgrade:backfill_tagging_tenants
+```
+
+Product and order taggings carry the store they belong to, and the admin tag
+list filters by it. Tags added through the bulk endpoints were written without
+the store, so they were missing from that list while tags added one at a time
+were not. This stamps the record's store on every tagging without one. Safe to
+re-run at any time.
+
### Move users onto the Customer and AdminUser models
```bash
diff --git a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
index 66d6c0bb1e..46b47fad31 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/tags_controller.rb
@@ -63,22 +63,24 @@ module Spree
false
end
- # Tagging filter. Store-owned taggables (`Spree::Product`,
- # `Spree::Order`) tenant their taggings by `store_id` via
- # `acts_as_taggable_tenant`, so their vocabulary is bounded to the
- # current store with a single indexed `tenant` filter. Global
- # taggables like the customer/user type carry no store, so their tags
- # are not store-scoped here.
+ # Tagging filter. A taggable declaring `acts_as_taggable_tenant :store_id`
+ # stores the owning store on each tagging, so its vocabulary is bounded
+ # to the current store with a single indexed `tenant` filter. Taggables
+ # without a store tenant are not store-scoped here.
def taggings_conditions
conditions = { taggable_type: taggable_type, context: 'tags' }
conditions[:tenant] = current_store.id.to_s if store_tenanted_taggable?
conditions
end
- # True for taggables whose taggings carry a store `tenant`
- # (`acts_as_taggable_tenant :store_id`), i.e. store-owned records.
def store_tenanted_taggable?
- %w[Spree::Product Spree::Order].include?(taggable_type)
+ taggable_class.try(:tenant_column).to_s == 'store_id'
+ end
+
+ # `taggable_type` is already matched against `Spree.taggable_types`,
+ # so only a registered class name is ever constantized.
+ def taggable_class
+ taggable_type.constantize
end
# Per-type scope check for API-key principals: a key listing product
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb
index 5da1bcb28b..84ede0af5e 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/tags_controller_spec.rb
@@ -64,6 +64,36 @@ RSpec.describe Spree::Api::V3::Admin::TagsController, type: :controller do
expect(names).to include('bestseller')
expect(names).not_to include('clearance')
end
+
+ it 'includes tags added in bulk' do
+ Spree::Tags::BulkAdd.call(tag_names: ['bulk-added'], records: Spree::Product.where(id: own_product.id))
+
+ get :index, params: { taggable_type: 'product' }, as: :json
+
+ expect(json_response['data'].map { |t| t['name'] }).to include('bulk-added')
+ end
+ end
+
+ context 'a taggable that declares a store tenant' do
+ let(:headers) { bearer_headers }
+
+ let(:other_store) { create(:store) }
+ let!(:own_user) { create(:user, tag_list: ['newsletter']) }
+ let!(:foreign_user) { create(:user, tag_list: ['wholesale']) }
+
+ before do
+ own_user.taggings.update_all(tenant: store.id.to_s)
+ foreign_user.taggings.update_all(tenant: other_store.id.to_s)
+ allow(Spree.customer_class).to receive(:tenant_column).and_return(:store_id)
+ end
+
+ it 'bounds its vocabulary to the current store' do
+ get :index, params: { taggable_type: Spree.customer_class.api_type }, as: :json
+
+ names = json_response['data'].map { |t| t['name'] }
+ expect(names).to include('newsletter')
+ expect(names).not_to include('wholesale')
+ end
end
describe 'API-key scope enforcement' do
diff --git a/spree/core/app/services/spree/tags/bulk_add.rb b/spree/core/app/services/spree/tags/bulk_add.rb
index c38c1b7807..6212437429 100644
--- a/spree/core/app/services/spree/tags/bulk_add.rb
+++ b/spree/core/app/services/spree/tags/bulk_add.rb
@@ -27,17 +27,20 @@ module Spree
private
+ # Mirrors the gem's own tagging save, which stores the taggable's tenant
+ # (its `store_id` for store-owned records) as a string on each tagging.
def taggings_attributes(tags, records, context:, record_class:)
- records.pluck(:id).map do |record_id|
+ records.flat_map do |record|
tags.map do |tag|
{
- taggable_id: record_id,
+ taggable_id: record.id,
taggable_type: record_class.to_s,
context: context,
- tag_id: tag.id
+ tag_id: tag.id,
+ tenant: record.taggable_tenant&.to_s
}
end
- end.flatten.compact
+ end
end
end
end
diff --git a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
index 0e1c2f15c7..0eb71c543e 100644
--- a/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
+++ b/spree/core/lib/spree/upgrades/5_6_to_6_0/manifest.yml
@@ -819,3 +819,13 @@ steps:
Run AFTER the steps that create or move attachments (media, categories,
returns), so their files are covered. Idempotent — files that already
have a store are skipped.
+
+ - id: backfill_tagging_tenants
+ name: "Stamp the store on taggings written without one"
+ task: "spree:upgrade:backfill_tagging_tenants"
+ notes: |
+ Tags added through the bulk endpoints carried no store on their
+ taggings, and the admin tag list filters by it, so those tags were
+ missing from the list. This stamps the store of the record on every
+ tagging without one, for each taggable that keeps its taggings per
+ store. Idempotent — taggings that already carry a store are skipped.
diff --git a/spree/core/lib/tasks/backfill_tagging_tenants.rake b/spree/core/lib/tasks/backfill_tagging_tenants.rake
new file mode 100644
index 0000000000..5341ce7491
--- /dev/null
+++ b/spree/core/lib/tasks/backfill_tagging_tenants.rake
@@ -0,0 +1,35 @@
+namespace :spree do
+ namespace :upgrade do
+ desc <<~DESC
+ Stamps the store on taggings written without one, for every taggable
+ that keeps its taggings per store. Tags added through the bulk endpoints
+ before Spree 6.0 carried no store, and the admin tag list filters by it,
+ so those tags were missing from the list. Idempotent — taggings that
+ already carry a store are skipped.
+ DESC
+ task backfill_tagging_tenants: :environment do
+ total = 0
+
+ Spree.taggable_types.map(&:constantize).each do |taggable|
+ column = taggable.try(:tenant_column)
+ next if column.blank?
+
+ count = 0
+ ActsAsTaggableOn::Tagging.where(taggable_type: taggable.polymorphic_name, tenant: nil).in_batches do |taggings|
+ tenants = taggable.unscoped.where(id: taggings.select(:taggable_id)).pluck(:id, column).to_h
+
+ tenants.compact.group_by { |_id, tenant| tenant }.each do |tenant, pairs|
+ count += taggings.where(taggable_id: pairs.map(&:first)).update_all(tenant: tenant.to_s)
+ end
+ end
+
+ next if count.zero?
+
+ total += count
+ puts " #{taggable.name}: #{count} tagging(s) stamped with the #{column}."
+ end
+
+ puts total.zero? ? ' Nothing to backfill.' : " Backfilled #{total} tagging(s)."
+ end
+ end
+end
diff --git a/spree/core/spec/lib/tasks/backfill_tagging_tenants_spec.rb b/spree/core/spec/lib/tasks/backfill_tagging_tenants_spec.rb
new file mode 100644
index 0000000000..a06bbf58d7
--- /dev/null
+++ b/spree/core/spec/lib/tasks/backfill_tagging_tenants_spec.rb
@@ -0,0 +1,45 @@
+require 'spec_helper'
+require 'rake'
+
+describe 'spree:upgrade:backfill_tagging_tenants' do
+ subject { Rake::Task[task_name] }
+
+ let(:task_name) { 'spree:upgrade:backfill_tagging_tenants' }
+
+ before(:all) do
+ Rake::Task.define_task(:environment)
+ load Spree::Core::Engine.root.join('lib', 'tasks', 'backfill_tagging_tenants.rake')
+ end
+
+ before { subject.reenable }
+
+ let(:product) { create(:product, store: create(:store), tag_list: ['summer']) }
+ let(:user) { create(:user, tag_list: ['newsletter']) }
+
+ # Simulates a bulk-added row from before the store was stamped on it.
+ def unstamp!(record)
+ record.taggings.update_all(tenant: nil)
+ end
+
+ it 'stamps a tagging with the store of its record' do
+ unstamp!(product)
+
+ subject.invoke
+
+ expect(product.taggings.pluck(:tenant)).to eq([product.store_id.to_s])
+ end
+
+ it 'leaves a tagging of a record without a store tenant alone' do
+ unstamp!(user)
+
+ subject.invoke
+
+ expect(user.taggings.pluck(:tenant)).to eq([nil])
+ end
+
+ it 'skips taggings that already carry a store' do
+ product
+
+ expect { subject.invoke }.to output(/Nothing to backfill/).to_stdout
+ end
+end
diff --git a/spree/core/spec/services/spree/tags/bulk_add_spec.rb b/spree/core/spec/services/spree/tags/bulk_add_spec.rb
index 970e020339..378ef19b86 100644
--- a/spree/core/spec/services/spree/tags/bulk_add_spec.rb
+++ b/spree/core/spec/services/spree/tags/bulk_add_spec.rb
@@ -26,6 +26,7 @@ module Spree
expect(products.pluck(:id)).to include(tagging.taggable_id)
expect(tag_names).to include(tagging.tag.name)
expect(tagging.context).to eq(context)
+ expect(tagging.tenant).to eq(tagging.taggable.store_id.to_s)
end
end
@@ -33,6 +34,16 @@ module Spree
expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }
end
+ context 'when the records declare no tenant' do
+ let(:products) { create_list(:user, 2) }
+
+ it 'leaves the tagging tenant empty' do
+ subject
+
+ expect(ActsAsTaggableOn::Tagging.where(taggable: products).pluck(:tenant)).to all(be_nil)
+ end
+ end
+
context 'when tag names are duplicated or have extra spaces' do
let(:tag_names) { ['tag1', ' tag2 ', 'tag1', 'tag3'] }