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
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.rbindex 05459dbcdf..44ff82aa14 100644--- a/core/app/models/spree/property.rb+++ b/core/app/models/spree/property.rb@@ -16,6 +16,7 @@ module Spreescope :filterable, -> { where(filterable: true) }after_touch :touch_all_products+ after_save :ensure_product_properties_have_filter_paramsself.whitelisted_ransackable_attributes = ['presentation']@@ -39,5 +40,11 @@ module Spreeuniq_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)+ endendenddiff --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.rbindex 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, :stringadd_index :spree_product_properties, :filter_param-- Spree::ProductProperty.reset_column_information- Spree::ProductProperty.all.each(&:save)endendenddiff --git a/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rb b/core/db/migrate/20210505114659_add_filter_param_to_spree_properties.rbindex 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, :stringadd_index :spree_properties, :filter_param-- Spree::Property.reset_column_information- Spree::Property.all.each(&:save)endendenddiff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rbindex 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 doendendend++ 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+ endend