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.

[SD-1339] Add store to taxonomies (#11161)

Szymon Iwacz8055e6a04ed6
diff --git a/api/app/controllers/spree/api/v1/taxonomies_controller.rb b/api/app/controllers/spree/api/v1/taxonomies_controller.rb
index f5c643d6e5..770bf9055b 100644
--- a/api/app/controllers/spree/api/v1/taxonomies_controller.rb
+++ b/api/app/controllers/spree/api/v1/taxonomies_controller.rb
@@ -19,7 +19,7 @@ module Spree
def create
authorize! :create, Taxonomy
- @taxonomy = Taxonomy.new(taxonomy_params)
+ @taxonomy = current_store.taxonomies.new(taxonomy_params)
if @taxonomy.save
respond_with(@taxonomy, status: 201, default_template: :show)
else
diff --git a/backend/app/controllers/spree/admin/resource_controller.rb b/backend/app/controllers/spree/admin/resource_controller.rb
index 8c7d5b1c5d..da519831f5 100644
--- a/backend/app/controllers/spree/admin/resource_controller.rb
+++ b/backend/app/controllers/spree/admin/resource_controller.rb
@@ -189,11 +189,13 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController
def collection
return parent.send(controller_name) if parent_data.present?
+ base_scope = model_class.try(:for_store, current_store) || model_class
+
if model_class.respond_to?(:accessible_by) &&
!current_ability.has_block?(params[:action], model_class)
- model_class.accessible_by(current_ability, action)
+ base_scope.accessible_by(current_ability, action)
else
- model_class.where(nil)
+ base_scope.where(nil)
end
end
diff --git a/backend/app/controllers/spree/admin/taxons_controller.rb b/backend/app/controllers/spree/admin/taxons_controller.rb
index c7c9c94f02..845f6b3c54 100644
--- a/backend/app/controllers/spree/admin/taxons_controller.rb
+++ b/backend/app/controllers/spree/admin/taxons_controller.rb
@@ -90,9 +90,9 @@ module Spree
def load_taxonomy
@taxonomy = if defined?(SpreeGlobalize)
- Taxonomy.includes(:translations, taxons: [:translations]).find(params[:taxonomy_id])
+ scope.includes(:translations, taxons: [:translations]).find(params[:taxonomy_id])
else
- Taxonomy.find(params[:taxonomy_id])
+ scope.find(params[:taxonomy_id])
end
end
@@ -102,7 +102,7 @@ module Spree
end
def set_parent(parent_id)
- @taxon.parent = Taxon.find(parent_id.to_i) if parent_id
+ @taxon.parent = current_store.taxons.find(parent_id.to_i) if parent_id
end
def set_permalink_params
@@ -127,6 +127,10 @@ module Spree
taxon.set_permalink
taxon.save!
end
+
+ def scope
+ current_store.taxonomies
+ end
end
end
end
diff --git a/backend/spec/controllers/spree/admin/taxonomies_controller_spec.rb b/backend/spec/controllers/spree/admin/taxonomies_controller_spec.rb
new file mode 100644
index 0000000000..18f28f87f0
--- /dev/null
+++ b/backend/spec/controllers/spree/admin/taxonomies_controller_spec.rb
@@ -0,0 +1,91 @@
+require 'spec_helper'
+
+describe Spree::Admin::TaxonomiesController, type: :controller do
+ stub_authorization!
+
+ let(:store) { create(:store) }
+ let(:new_store) { create(:store) }
+ let!(:taxonomy_1) { create(:taxonomy, store: store) }
+ let!(:taxonomy_2) { create(:taxonomy, store: store) }
+ let!(:taxonomy_3) { create(:taxonomy) }
+
+ before do
+ allow_any_instance_of(described_class).to receive(:current_store).and_return(store)
+ end
+
+ describe '#index' do
+ it 'should assign only the store credits for user and current store' do
+ get :index
+ expect(assigns(:collection)).to include taxonomy_1
+ expect(assigns(:collection)).to include taxonomy_2
+ expect(assigns(:collection)).not_to include taxonomy_3
+ end
+ end
+
+ context '#new' do
+ it 'should create taxonomy' do
+ get :new
+ expect(response.status).to eq(200)
+ end
+ end
+
+ describe '#create' do
+ let(:request) { post :create, params: { taxonomy: { name: 'Shirts' } } }
+
+ it 'should create taxonomy for current store' do
+ expect{ request }.to change { store.taxonomies.count }.by(1)
+ expect(response).to be_redirect
+ end
+
+ context 'different store' do
+ subject(:create_request) { post(:create, params: {taxonomy: {name: 'Bags'}}) }
+
+ before do
+ allow_any_instance_of(described_class).to receive(:current_store).and_return(new_store)
+ end
+
+ it 'should not create taxonomy for store' do
+ expect{ subject }.not_to change { store.taxonomies.reload.count }
+ expect(response).to be_redirect
+ end
+
+ it 'should create taxonomy for new store' do
+ expect{ subject }.to change { new_store.taxonomies.reload.count }.by(1)
+ expect(response).to be_redirect
+ end
+ end
+ end
+
+ describe '#update' do
+ it 'should allow to update current store taxonomy' do
+ expect{ put(:update, params: { id: taxonomy_1.id, taxonomy: { name: 'Beverages' } }) }.to change{taxonomy_1.reload.name}.to('Beverages')
+ end
+
+ it 'should not allow to update not current store taxonomy' do
+ expect{ put(:update, params: { id: taxonomy_3.id, taxonomy: { name: 'Shoes' } }) }.not_to change{taxonomy_3.reload.name}
+ end
+ end
+
+ describe '#destroy' do
+ before { delete :destroy, params: { id: taxonomy.id } }
+
+ context 'when current store taxonomy' do
+ let(:taxonomy) { taxonomy_1 }
+
+ it 'should be able to destroy taxonomy' do
+ expect(assigns(:object)).to eq(taxonomy)
+ expect(response).to have_http_status(:found)
+ expect(flash[:success]).to eq("Taxonomy \"#{taxonomy.name}\" has been successfully removed!")
+ end
+ end
+
+ context 'when not current store taxonomy' do
+ let(:taxonomy) { taxonomy_3 }
+
+ it 'should be able to destroy taxonomy' do
+ expect(assigns(:object)).to be_nil
+ expect(flash[:error]).to eq("Taxonomy is not found")
+ end
+ end
+ end
+end
diff --git a/backend/spec/controllers/spree/admin/taxons_controller_spec.rb b/backend/spec/controllers/spree/admin/taxons_controller_spec.rb
index b3882b39a3..1306b9fa0d 100644
--- a/backend/spec/controllers/spree/admin/taxons_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/taxons_controller_spec.rb
@@ -4,18 +4,20 @@ describe Spree::Admin::TaxonsController, type: :controller do
stub_authorization!
describe '#remove_icon' do
- let!(:taxonomy) { create(:taxonomy) }
+ let(:store) { Spree::Store.default }
+ let!(:taxonomy) { create(:taxonomy, store: store) }
let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
let!(:taxon_image) { Spree::TaxonImage.new }
let!(:image_file) { File.open(Spree::Core::Engine.root + 'spec/fixtures' + 'thinking-cat.jpg') }
before do
+ allow_any_instance_of(described_class).to receive(:current_store).and_return(store)
taxon_image.attachment.attach(io: image_file, filename: 'thinking-cat.jpg', content_type: 'image/jpeg')
taxon.icon = taxon_image
taxon.save!
end
- it 'removes taxon icon' do
+ it 'should remove current store taxon icon' do
expect(taxon.icon).to eq(taxon_image)
allow(taxon).to receive :remove_icon
@@ -24,5 +26,18 @@ describe Spree::Admin::TaxonsController, type: :controller do
expect(taxon.icon).to eq(nil)
end
+
+ context 'when different current store' do
+ let!(:second_store) { create(:store) }
+ let(:request) { post :remove_icon, params: { taxonomy_id: taxonomy.id, id: taxon.id } }
+
+ before do
+ allow_any_instance_of(described_class).to receive(:current_store).and_return(second_store)
+ end
+
+ it 'should not find taxonomy' do
+ expect { request }.to raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 5291ba48d8..08aced0d0d 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -26,6 +26,9 @@ module Spree
has_many :store_credits, class_name: 'Spree::StoreCredit'
has_many :store_credit_events, through: :store_credits, class_name: 'Spree::StoreCreditEvent'
+ has_many :taxonomies, class_name: 'Spree::Taxonomy'
+ has_many :taxons, through: :taxonomies, class_name: 'Spree::Taxon'
+
belongs_to :default_country, class_name: 'Spree::Country'
belongs_to :checkout_zone, class_name: 'Spree::Zone'
diff --git a/core/app/models/spree/taxon.rb b/core/app/models/spree/taxon.rb
index 9898ed2c25..6f89c14d91 100644
--- a/core/app/models/spree/taxon.rb
+++ b/core/app/models/spree/taxon.rb
@@ -22,7 +22,7 @@ module Spree
has_many :promotion_rules, through: :promotion_rule_taxons, class_name: 'Spree::PromotionRule'
validates :name, presence: true, uniqueness: { scope: [:parent_id, :taxonomy_id], allow_blank: true, case_sensitive: false }
- validates :permalink, uniqueness: { case_sensitive: false }
+ validates :permalink, uniqueness: { case_sensitive: false, scope: [:parent_id, :taxonomy_id] }
validates :hide_from_nav, inclusion: { in: [true, false] }
validates_associated :icon
validate :check_for_root, on: :create
diff --git a/core/app/models/spree/taxonomy.rb b/core/app/models/spree/taxonomy.rb
index 83a3c23c50..f69b7fb0b7 100644
--- a/core/app/models/spree/taxonomy.rb
+++ b/core/app/models/spree/taxonomy.rb
@@ -2,10 +2,12 @@ module Spree
class Taxonomy < Spree::Base
acts_as_list
- validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true }
+ validates :name, presence: true, uniqueness: { case_sensitive: false, allow_blank: true, scope: :store_id }
+ validates :store, presence: true
has_many :taxons, inverse_of: :taxonomy
has_one :root, -> { where parent_id: nil }, class_name: 'Spree::Taxon', dependent: :destroy
+ belongs_to :store, class_name: 'Spree::Store'
after_create :set_root
after_save :set_root_taxon_name
diff --git a/core/db/migrate/20210716093151_add_store_id_to_spree_taxonomies.rb b/core/db/migrate/20210716093151_add_store_id_to_spree_taxonomies.rb
new file mode 100644
index 0000000000..7726e8d634
--- /dev/null
+++ b/core/db/migrate/20210716093151_add_store_id_to_spree_taxonomies.rb
@@ -0,0 +1,11 @@
+class AddStoreIdToSpreeTaxonomies < ActiveRecord::Migration[5.2]
+ def change
+ unless column_exists?(:spree_taxonomies, :store_id)
+ add_column :spree_taxonomies, :store_id, :bigint
+ add_index :spree_taxonomies, :store_id
+ add_index :spree_taxonomies, [:name, :store_id], unique: true
+ Spree::Taxonomy.reset_column_information
+ Spree::Taxonomy.update_all(store_id: Spree::Store.default.id)
+ end
+ end
+end
diff --git a/core/db/migrate/20210716104141_add_index_on_name_parent_id_and_taxonomy_id_on_spree_taxons.rb b/core/db/migrate/20210716104141_add_index_on_name_parent_id_and_taxonomy_id_on_spree_taxons.rb
new file mode 100644
index 0000000000..6e1b46399f
--- /dev/null
+++ b/core/db/migrate/20210716104141_add_index_on_name_parent_id_and_taxonomy_id_on_spree_taxons.rb
@@ -0,0 +1,31 @@
+class AddIndexOnNameParentIdAndTaxonomyIdOnSpreeTaxons < ActiveRecord::Migration[5.2]
+ def up
+ klass = Spree::Taxon
+ columns = %w[name parent_id taxonomy_id]
+
+ unless index_exists?(klass.table_name, columns)
+ remove_index klass.table_name, columns if index_exists?(klass.table_name, columns)
+
+ say "Find duplicate #{klass} records"
+ duplicates = klass.
+ select((columns + %w[COUNT(*)]).join(',')).
+ group("#{columns.join(',')}").
+ having('COUNT(*) > 1').
+ map { |row| row.attributes.slice(*columns) }
+
+ say "Delete all but the oldest duplicate #{klass} record"
+ duplicates.each do |conditions|
+ klass.where(conditions).order(:created_at).drop(1).each(&:destroy)
+ end
+
+ duplicates.each do |conditions| puts klass.where(conditions).order(:created_at).drop(1) end
+
+ say "Add unique index to #{klass.table_name} for #{columns.inspect}"
+ add_index klass.table_name, columns, unique: true
+ end
+ end
+
+ def down
+ remove_index :spree_taxons, [:name, :parent_id, :taxonomy_id]
+ end
+end
diff --git a/core/db/migrate/20210721120857_add_index_on_permalink_parent_id_and_taxonomy_id_on_spree_taxons.rb b/core/db/migrate/20210721120857_add_index_on_permalink_parent_id_and_taxonomy_id_on_spree_taxons.rb
new file mode 100644
index 0000000000..0ba0efc0bf
--- /dev/null
+++ b/core/db/migrate/20210721120857_add_index_on_permalink_parent_id_and_taxonomy_id_on_spree_taxons.rb
@@ -0,0 +1,31 @@
+class AddIndexOnPermalinkParentIdAndTaxonomyIdOnSpreeTaxons < ActiveRecord::Migration[5.2]
+ def up
+ klass = Spree::Taxon
+ columns = %w[permalink parent_id taxonomy_id]
+
+ unless index_exists?(klass.table_name, columns)
+ remove_index klass.table_name, columns if index_exists?(klass.table_name, columns)
+
+ say "Find duplicate #{klass} records"
+ duplicates = klass.
+ select((columns + %w[COUNT(*)]).join(',')).
+ group("#{columns.join(',')}").
+ having('COUNT(*) > 1').
+ map { |row| row.attributes.slice(*columns) }
+
+ say "Delete all but the oldest duplicate #{klass} record"
+ duplicates.each do |conditions|
+ klass.where(conditions).order(:created_at).drop(1).each(&:destroy)
+ end
+
+ duplicates.each do |conditions| puts klass.where(conditions).order(:created_at).drop(1) end
+
+ say "Add unique index to #{klass.table_name} for #{columns.inspect}"
+ add_index klass.table_name, columns, unique: true
+ end
+ end
+
+ def down
+ remove_index :spree_taxons, [:permalink, :parent_id, :taxonomy_id]
+ end
+end
diff --git a/core/lib/spree/testing_support/factories/taxonomy_factory.rb b/core/lib/spree/testing_support/factories/taxonomy_factory.rb
index 26f528e317..09e304b334 100644
--- a/core/lib/spree/testing_support/factories/taxonomy_factory.rb
+++ b/core/lib/spree/testing_support/factories/taxonomy_factory.rb
@@ -1,5 +1,6 @@
FactoryBot.define do
factory :taxonomy, class: Spree::Taxonomy do
sequence(:name) { |n| "taxonomy_#{n}" }
+ store { Spree::Store.default }
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 5eed39c41f..88ffd71aa7 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -116,6 +116,22 @@ describe Spree::Store, type: :model do
it { expect(subject.menu_items).to eq([menu_item]) }
end
end
+
+ describe '#taxonomies' do
+ let!(:taxonomy) { create(:taxonomy, store: subject) }
+ let!(:taxonomy_2) { create(:taxonomy, store: create(:store)) }
+
+ it { expect(subject.taxonomies).to eq([taxonomy]) }
+
+ describe '#taxons' do
+ let!(:taxon) { create(:taxon, taxonomy: taxonomy) }
+ let!(:taxon_2) { create(:taxon, taxonomy: taxonomy_2) }
+
+ it { expect(taxon).not_to be_nil }
+ it { expect(taxon_2).not_to be_nil }
+ it { expect(subject.taxons).to match_array([taxonomy.root, taxon]) }
+ end
+ end
end
describe 'validations' do
diff --git a/frontend/app/controllers/spree/products_controller.rb b/frontend/app/controllers/spree/products_controller.rb
index 869fb70f9e..3ed7340914 100644
--- a/frontend/app/controllers/spree/products_controller.rb
+++ b/frontend/app/controllers/spree/products_controller.rb
@@ -21,7 +21,7 @@ module Spree
def show
redirect_if_legacy_path
- @taxon = params[:taxon_id].present? ? Spree::Taxon.find_by(id: params[:taxon_id]) : nil
+ @taxon = params[:taxon_id].present? ? taxons_scope.find_by(id: params[:taxon_id]) : nil
@taxon = @product.taxons.first unless @taxon.present?
if !http_cache_enabled? || stale?(etag: etag_show, last_modified: last_modified_show, public: true)
@@ -56,7 +56,7 @@ module Spree
end
def load_taxon
- @taxon = Spree::Taxon.find(params[:taxon]) if params[:taxon].present?
+ @taxon = taxons_scope.find(params[:taxon]) if params[:taxon].present?
end
def can_show_product?
@@ -119,5 +119,9 @@ module Spree
[product_last_modified, current_store_last_modified].compact.max
end
+
+ def taxons_scope
+ current_store.taxons
+ end
end
end
diff --git a/frontend/app/controllers/spree/taxons_controller.rb b/frontend/app/controllers/spree/taxons_controller.rb
index d6f239b27f..de78dd13c0 100644
--- a/frontend/app/controllers/spree/taxons_controller.rb
+++ b/frontend/app/controllers/spree/taxons_controller.rb
@@ -30,7 +30,7 @@ module Spree
end
def load_taxon
- @taxon = Spree::Taxon.friendly.find(params[:id])
+ @taxon = current_store.taxons.friendly.find(params[:id])
end
def load_products
diff --git a/frontend/spec/features/taxons_spec.rb b/frontend/spec/features/taxons_spec.rb
index d066df6d97..77b0e1e9fc 100644
--- a/frontend/spec/features/taxons_spec.rb
+++ b/frontend/spec/features/taxons_spec.rb
@@ -1,17 +1,19 @@
require 'spec_helper'
describe 'viewing products', type: :feature, inaccessible: true do
- let!(:taxonomy) { create(:taxonomy, name: 'Category') }
- let!(:super_clothing) { taxonomy.root.children.create(name: 'Super Clothing') }
- let!(:t_shirts) { super_clothing.children.create(name: 'T-Shirts') }
+ let(:store) { Spree::Store.default }
+ let!(:taxonomy) { create(:taxonomy, name: 'Category', store: store) }
+ let!(:super_clothing) { taxonomy.root.children.create(name: 'Super Clothing', taxonomy_id: taxonomy.id) }
+ let!(:t_shirts) { super_clothing.children.create(name: 'T-Shirts', taxonomy_id: taxonomy.id) }
let(:metas) { { meta_description: 'Brand new Ruby on Rails TShirts', meta_title: 'Ruby On Rails TShirt', meta_keywords: 'ror, tshirt, ruby' } }
- let(:store_name) { ((first_store = Spree::Store.first) && first_store.name).to_s }
+ let(:store_name) { store.name.to_s }
before do
t_shirts.children.create(name: 'XXL') # xxl
product = create(:product, name: 'Superman T-Shirt')
product.taxons << t_shirts
+ allow_any_instance_of(Spree::StoreController).to receive(:current_store).and_return(store)
end
# Regression test for #1796
diff --git a/sample/db/samples/taxonomies.rb b/sample/db/samples/taxonomies.rb
index 8e3fc570bc..2f0013ee2f 100644
--- a/sample/db/samples/taxonomies.rb
+++ b/sample/db/samples/taxonomies.rb
@@ -1,5 +1,8 @@
taxonomies = [
- { name: I18n.t('spree.taxonomy_categories_name') }
+ {
+ name: I18n.t('spree.taxonomy_categories_name'),
+ store: Spree::Store.default
+ }
]
taxonomies.each do |taxonomy_attrs|