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
Remove integer casting for IDs (#11426)
Damian Legawiecb9edad8096ee
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex aa06ac2cad..d9e5a8417b 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -154,7 +154,7 @@ module Spreedef by_options(products)return products unless options?- products_ids = options.map { |key, value| products.with_option_value(key, value).ids }+ products_ids = options.map { |key, value| products.with_option_value(key, value)&.ids }.compact.uniqproducts.where(id: products_ids.reduce(&:intersection))enddiff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex 711b0b30ee..a1531af78f 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -34,9 +34,14 @@ module Spreedef self.property_conditions(property)properties = Property.table_namecase property- when String then { "#{properties}.name" => property }when Property then { "#{properties}.id" => property.id }- else { "#{properties}.id" => property.to_i }+ when Integer then { "#{properties}.id" => property }+ else+ if Property.column_for_attribute('id').type == :uuid+ ["#{properties.name} = ? OR #{properties.id} = ?", property, property]+ else+ { "#{properties}.name" => property }+ endendend@@ -139,26 +144,34 @@ module Spreeendadd_search_scope :with_option do |option|- option_types = OptionType.table_name- conditions = case option- when String then { "#{option_types}.name" => option }- when OptionType then { "#{option_types}.id" => option.id }- else { "#{option_types}.id" => option.to_i }- end-- joins(:option_types).where(conditions)+ if option.is_a?(OptionType)+ joins(:option_types).where(spree_option_types: { id: option.id })+ elsif option.is_a?(Integer)+ joins(:option_types).where(spree_option_types: { id: option })+ elsif OptionType.column_for_attribute('id').type == :uuid+ joins(:option_types).where(spree_option_types: { name: option }).or(Product.joins(:option_types).where(spree_option_types: { id: option }))+ else+ joins(:option_types).where(spree_option_types: { name: option })+ endendadd_search_scope :with_option_value do |option, value|- option_values = OptionValue.table_nameoption_type_id = case option- when String then OptionType.find_by(name: option) || option.to_iwhen OptionType then option.id- else option.to_i+ when Integer then option+ else+ if OptionType.column_for_attribute('id').type == :uuid+ OptionType.where(id: option).or(OptionType.where(name: option))&.first&.id+ else+ OptionType.where(name: option)&.first&.id+ endend- conditions = "#{option_values}.name = ? AND #{option_values}.option_type_id = ?", value, option_type_id- group('spree_products.id').joins(variants_including_master: :option_values).where(conditions)+ return Product.group("#{Spree::Product.table_name}.id").none if option_type_id.blank?++ group("#{Spree::Product.table_name}.id").+ joins(variants_including_master: :option_values).+ where(Spree::OptionValue.table_name => { name: value, option_type_id: option_type_id })end# Finds all products which have either:@@ -320,14 +333,11 @@ module Spreeprivate_class_method :prepare_wordsdef self.get_taxons(*ids_or_records_or_names)- taxons = Taxon.table_nameids_or_records_or_names.flatten.map do |t|case t- when Integer then Taxon.find_by(id: t)when ApplicationRecord then t- when String- Taxon.find_by(name: t) ||- Taxon.where("#{taxons}.permalink LIKE ? OR #{taxons}.permalink = ?", "%/#{t}/", "#{t}/").first+ else+ Taxon.where(name: t).or(Taxon.where(id: t)).or(Taxon.where("permalink LIKE ? OR permalink = ?", "%/#{t}/", "#{t}/")).firstendend.compact.flatten.uniqenddiff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rbindex 1edf7d01fb..3f5051f708 100644--- a/core/app/models/spree/order.rb+++ b/core/app/models/spree/order.rb@@ -634,10 +634,10 @@ module SpreeActiveSupport::Deprecation.warn('`Order#validate_payments_attributes` is deprecated and will be removed in Spree 5')# Ensure the payment methods specified are allowed for this user- payment_method_ids = available_payment_methods.map(&:id)+ payment_method_ids = available_payment_methods.map(&:id).map(&:to_s)attributes.each do |payment_attributes|- payment_method_id = payment_attributes[:payment_method_id].to_i+ payment_method_id = payment_attributes[:payment_method_id].to_sraise ActiveRecord::RecordNotFound unless payment_method_ids.include?(payment_method_id)enddiff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rbindex 1e589c9985..bc19e75a81 100644--- a/core/app/models/spree/product.rb+++ b/core/app/models/spree/product.rb@@ -194,10 +194,7 @@ module Spreeend# Adding properties and option types on creation based on a chosen prototype- attr_reader :prototype_id- def prototype_id=(value)- @prototype_id = value.to_i- end+ attr_accessor :prototype_id# Ensures option_types and product_option_types exist for keys in option_values_hashdef ensure_option_types_exist_for_values_hashdiff --git a/core/app/services/spree/cart/update.rb b/core/app/services/spree/cart/update.rbindex 5b3e7476ac..3e1d21800c 100644--- a/core/app/services/spree/cart/update.rb+++ b/core/app/services/spree/cart/update.rb@@ -25,10 +25,10 @@ module Spreedef filter_order_items(order, params)return params if params[:line_items_attributes].nil? || params[:line_items_attributes][:id]- line_item_ids = order.line_items.pluck(:id)+ line_item_ids = order.line_item_ids.map(&:to_s)params[:line_items_attributes].each_pair do |id, value|- params[:line_items_attributes].delete(id) unless line_item_ids.include?(value[:id].to_i) || value[:variant_id].present?+ params[:line_items_attributes].delete(id) unless line_item_ids.include?(value[:id].to_s) || value[:variant_id].present?endparamsenddiff --git a/core/lib/spree/core/importer/product.rb b/core/lib/spree/core/importer/product.rbindex 62ccd9c705..cbc8669612 100644--- a/core/lib/spree/core/importer/product.rb+++ b/core/lib/spree/core/importer/product.rb@@ -32,7 +32,7 @@ module Spreevariants_attrs.each do |variant_attribute|# update the variant if the id is present in the payloadif variant_attribute['id'].present?- product.variants.find(variant_attribute['id'].to_i).update(variant_attribute)+ product.variants.find(variant_attribute['id']).update(variant_attribute)else# make sure the product is assigned before the options=product.variants.create({ product: product }.merge(variant_attribute))diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rbindex cc9c835a1e..713229508b 100644--- a/core/spec/models/spree/product/scopes_spec.rb+++ b/core/spec/models/spree/product/scopes_spec.rb@@ -325,4 +325,63 @@ describe 'Product scopes', type: :model doexpect { Spree::Product.in_stock.in_stock_or_backorderable.count }.not_to raise_errorendend++ context 'options scopes' do+ let(:option_type) { create(:option_type) }+ let(:option_value) { create(:option_value, option_type: option_type) }+ let!(:product) { create(:product, option_types: [option_type]) }+ let!(:variant) { create(:variant, product: product, option_values: [option_value]) }++ describe '.with_option' do+ subject(:with_option) { Spree::Product.method(:with_option) }++ it "finds by a option type's name" do+ expect(with_option.call(option_type.name).count).to eq(1)+ end++ it "doesn't find any option types with an unknown name" do+ expect(with_option.call('fake').count).to eq(0)+ end++ it 'finds by a option type' do+ expect(with_option.call(option_type).count).to eq(1)+ end++ it 'finds by an id' do+ expect(with_option.call(option_type.id).count).to eq(1)+ end++ it 'cannot find an option type with an unknown id' do+ expect(with_option.call(0).count).to eq(0)+ end+ end++ describe '.with_option_value' do+ subject(:with_option) { Spree::Product.method(:with_option_value) }++ it "finds by a option type's name" do+ expect(with_option.call(option_type.name, option_value.name).count).to eq({ product.id => 1 })+ end++ it "doesn't find any option types with an unknown name" do+ expect(with_option.call('fake', 'fake').count).to eq({})+ end++ it 'finds by a option type' do+ expect(with_option.call(option_type, option_value.name).count).to eq({ product.id => 1 })+ end++ it 'finds by an id' do+ expect(with_option.call(option_type.id, option_value.name).count).to eq({ product.id => 1 })+ end++ it 'cannot find an option type with an unknown id' do+ expect(with_option.call(0, 'fake').count).to eq({})+ end++ it 'can return product ids' do+ expect(with_option.call(option_type, option_value.name).ids).to match_array([product.id])+ end+ end+ endend