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.

V-3065 FIX sort order caused by default_scope (#12649)

brozeke86d9391e259
diff --git a/admin/app/views/spree/admin/products/form/_properties.html.erb b/admin/app/views/spree/admin/products/form/_properties.html.erb
index e38aefe3b4..46245a1696 100644
--- a/admin/app/views/spree/admin/products/form/_properties.html.erb
+++ b/admin/app/views/spree/admin/products/form/_properties.html.erb
@@ -15,7 +15,7 @@
</tr>
</thead>
<tbody>
- <%= f.fields_for :product_properties do |product_property_form| %>
+ <%= f.fields_for :product_properties, f.object.product_properties.sort_by_property_position do |product_property_form| %>
<% property = product_property_form.object.property %>
<%= product_property_form.hidden_field :id %>
<%= product_property_form.hidden_field :property_id %>
diff --git a/core/app/models/spree/product_property.rb b/core/app/models/spree/product_property.rb
index 4fc689e991..20e2bca9c2 100644
--- a/core/app/models/spree/product_property.rb
+++ b/core/app/models/spree/product_property.rb
@@ -33,7 +33,7 @@ module Spree
scope :filterable, -> { joins(:property).where(Property.table_name => { filterable: true }) }
scope :for_products, ->(products) { joins(:product).merge(products) }
scope :sort_by_property_position, -> {
- joins(:property).order("spree_properties.position ASC")
+ unscope(:order).joins(:property).order("spree_properties.position ASC")
}
self.whitelisted_ransackable_attributes = ['value', 'filter_param']
diff --git a/core/app/models/spree/property.rb b/core/app/models/spree/property.rb
index 766cf39cf7..e9d6424b07 100644
--- a/core/app/models/spree/property.rb
+++ b/core/app/models/spree/property.rb
@@ -39,8 +39,10 @@ module Spree
KIND_OPTIONS = { short_text: 0, long_text: 1, number: 2, rich_text: 3 }.freeze
enum :kind, KIND_OPTIONS
+ DEPENDENCY_UPDATE_FIELDS = [:presentation, :name, :kind, :filterable, :display_on, :position].freeze
+
after_touch :touch_all_products
- after_update :touch_all_products, if: -> { saved_changes.key?(:presentation) }
+ after_update :touch_all_products, if: -> { DEPENDENCY_UPDATE_FIELDS.any? { |field| saved_changes.key?(field) } }
after_save :ensure_product_properties_have_filter_params
self.whitelisted_ransackable_attributes = ['presentation', 'filterable']
diff --git a/core/spec/models/spree/property_spec.rb b/core/spec/models/spree/property_spec.rb
index 31d3655111..e940ce6ad7 100644
--- a/core/spec/models/spree/property_spec.rb
+++ b/core/spec/models/spree/property_spec.rb
@@ -152,4 +152,28 @@ describe Spree::Property, type: :model do
it { expect { property.update(name: 'test') }.not_to change { product_property.reload.filter_param }.from(nil) }
end
end
+
+ describe '#after_touch callback' do
+ let!(:product_property) { create(:product_property) }
+
+ it 'touches the product' do
+ expect { product_property.property.touch }.to change { product_property.product.reload.updated_at }
+ end
+ end
+
+ describe '#after_update callback' do
+ let!(:product_property) { create(:product_property) }
+
+ context 'with DEPENDENCY_UPDATE_FIELDS' do
+ it 'touches the product' do
+ expect { product_property.property.update(name: 'test') }.to change { product_property.product.reload.updated_at }
+ end
+ end
+
+ context 'without DEPENDENCY_UPDATE_FIELDS' do
+ it 'does not touch the product' do
+ expect { product_property.property.update(updated_at: Time.now) }.not_to change { product_property.product.reload.updated_at }
+ end
+ end
+ end
end