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.

More performant way of generating filter params in DB migrations (#11058)

Damian Legawiecd45e7fb7232a
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 05459dbcdf..44ff82aa14 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -16,6 +16,7 @@ module Spree
scope :filterable, -> { where(filterable: true) }
after_touch :touch_all_products
+ after_save :ensure_product_properties_have_filter_params
self.whitelisted_ransackable_attributes = ['presentation']
@@ -39,5 +40,11 @@ module Spree
uniq_values_cache_key = ['property-uniq-values', cache_key_with_version]
Rails.cache.fetch(uniq_values_cache_key) { block.call }
end
+
+ def ensure_product_properties_have_filter_params
+ return unless filterable?
+
+ product_properties.where(filter_param: [nil, '']).where.not(value: [nil, '']).find_each(&:save)
+ end
end
end
diff --git a/core/db/migrate/20210504163720_add_filter_param_to_spree_product_properties.rb b/core/db/migrate/20210504163720_add_filter_param_to_spree_product_properties.rb
index 333499a01e..a4928335ee 100644
--- a/core/db/migrate/20210504163720_add_filter_param_to_spree_product_properties.rb
+++ b/core/db/migrate/20210504163720_add_filter_param_to_spree_product_properties.rb
@@ -3,9 +3,6 @@ class AddFilterParamToSpreeProductProperties < ActiveRecord::Migration[6.0]
unless column_exists?(:spree_product_properties, :filter_param)
add_column :spree_product_properties, :filter_param, :string
add_index :spree_product_properties, :filter_param
-
- Spree::ProductProperty.reset_column_information
- Spree::ProductProperty.all.each(&:save)
end
end
end
diff --git a/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb b/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb
index 6b727b8495..a11f7d98c7 100644
--- a/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb
+++ b/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb
@@ -3,9 +3,6 @@ class AddFilterParamToSpreeProperties < ActiveRecord::Migration[6.0]
unless column_exists?(:spree_properties, :filter_param)
add_column :spree_properties, :filter_param, :string
add_index :spree_properties, :filter_param
-
- Spree::Property.reset_column_information
- Spree::Property.all.each(&:save)
end
end
end
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index b113fae3f2..93988c3115 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -70,4 +70,24 @@ describe Spree::Property, type: :model do
end
end
end
+
+ describe '#ensure_product_properties_have_filter_params' do
+ let(:property) { create(:property) }
+ let(:product) { create(:product) }
+ let(:product_2) { create(:product) }
+
+ let(:product_property) { create(:product_property, property: property, product: product) }
+ let(:product_property_2) { create(:product_property, property: property, product: product_2, value: 'Test Test') }
+
+ before { product_property.update_column(:value, 'some value') }
+
+ context 'filterable property' do
+ it { expect { property.update(filterable: true) }.to change { product_property.reload.filter_param }.from(nil) }
+ it { expect { property.update(filterable: true) }.not_to change { product_property_2.reload.updated_at } }
+ end
+
+ context 'not-filterable property' do
+ it { expect { property.update(name: 'test') }.not_to change { product_property.reload.filter_param }.from(nil) }
+ end
+ end
end