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.

Use parameterized property name during creation, added `Product#remov… (#12170)

Damian Legawiecf2be12398f98
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index bdeeabf010..edc2229de4 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -363,10 +363,15 @@ module Spree
end
def property(property_name)
- product_properties.joins(:property).find_by(Property.table_name => { name: property_name }).try(:value)
+ if product_properties.loaded?
+ product_properties.detect { |property| property.property.name == property_name }.try(:value)
+ else
+ product_properties.joins(:property).find_by(spree_properties: { name: property_name }).try(:value)
+ end
end
def set_property(property_name, property_value, property_presentation = property_name)
+ property_name = property_name.to_s.parameterize
ApplicationRecord.transaction do
# Manual first_or_create to work around Mobility bug
property = if Property.where(name: property_name).exists?
@@ -381,7 +386,7 @@ module Spree
product_property = if ProductProperty.where(product: self, property: property).exists?
ProductProperty.where(product: self, property: property).first
else
- ProductProperty.create(product: self, property: property)
+ ProductProperty.new(product: self, property: property)
end
product_property.value = property_value
@@ -389,6 +394,10 @@ module Spree
end
end
+ def remove_property(property_name)
+ product_properties.joins(:property).find_by(spree_properties: { name: property_name.parameterize })&.destroy
+ end
+
def total_on_hand
@total_on_hand ||= if any_variants_not_track_inventory?
BigDecimal::INFINITY