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.

Open source product methods/validations/callbacks

Łukasz Adamczyk183f6f9dc9d8
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index 9d5f8498a7..a94fa62207 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -141,14 +141,20 @@ module Spree
has_many :page_links, as: :linkable, class_name: 'Spree::PageLink', dependent: :destroy
+ after_initialize :ensure_master
+ after_initialize :assign_default_tax_category
+
+ before_validation :downcase_slug
+ before_validation :normalize_slug, on: :update
+ before_validation :validate_master
+ before_validation :ensure_default_shipping_category
+
after_create :add_associations_from_prototype
after_create :build_variants_from_option_values_hash, if: :option_values_hash
after_destroy :punch_slug
after_restore :update_slug_history
- after_initialize :ensure_master
-
after_save :save_master
after_save :run_touch_callbacks, if: :anything_changed?
after_save :reset_nested_changes
@@ -156,10 +162,6 @@ module Spree
after_commit :auto_match_taxons, if: :eligible_for_taxon_matching?
- before_validation :downcase_slug
- before_validation :normalize_slug, on: :update
- before_validation :validate_master
-
with_options length: { maximum: 255 }, allow_blank: true do
validates :meta_keywords
validates :meta_title
@@ -257,15 +259,20 @@ module Spree
multi_search in_stock_items out_of_stock_items]
[
- :sku, :barcode, :price, :currency, :weight, :height, :width, :depth, :is_master,
- :cost_currency, :price_in, :amount_in, :cost_price, :compare_at_price, :compare_at_amount_in,
- :dimensions_unit, :weight_unit, :track_inventory
+ :sku, :barcode, :weight, :height, :width, :depth, :is_master, :dimensions_unit, :weight_unit
].each do |method_name|
delegate method_name, :"#{method_name}=", to: :find_or_build_master
end
+ [
+ :price, :price_in, :amount_in, :compare_at_price, :compare_at_amount_in,
+ :currency, :cost_currency, :cost_price, :track_inventory
+ ].each do |method_name|
+ delegate method_name, :"#{method_name}=", to: :default_variant
+ end
+
delegate :display_amount, :display_price, :has_default_price?,
- :display_compare_at_price, :images, to: :find_or_build_master
+ :display_compare_at_price, :images, to: :default_variant
delegate :name, to: :brand, prefix: true, allow_nil: true
@@ -391,6 +398,38 @@ module Spree
# Adding properties and option types on creation based on a chosen prototype
attr_accessor :prototype_id
+ def first_or_default_variant(currency)
+ @first_or_default_variant ||= if !has_variants?
+ default_variant
+ elsif first_available_variant(currency).present?
+ first_available_variant(currency)
+ else
+ variants.first
+ end
+ end
+
+ def first_available_variant(currency)
+ @first_available_variant ||= variants.find { |v| v.purchasable? && v.price_in(currency).amount.present? }
+ end
+
+ def price_varies?(currency)
+ @price_varies ||= prices_including_master.find_all { |p| p.currency == currency && p.amount.present? }.map(&:amount).uniq.count > 1
+ end
+
+ def any_variant_available?(currency)
+ @any_variant_available ||= if has_variants?
+ first_available_variant(currency).present?
+ else
+ master.purchasable? && master.price_in(currency).amount.present?
+ end
+ end
+
+ # returns the lowest price for the product in the given currency
+ # prices_including_master are usually already loaded, so this should not trigger an extra query
+ def lowest_price(currency)
+ prices_including_master.find_all { |p| p.currency == currency }.min_by(&:amount)
+ end
+
# Ensures option_types and product_option_types exist for keys in option_values_hash
def ensure_option_types_exist_for_values_hash
return if option_values_hash.nil?
@@ -556,7 +595,7 @@ module Spree
end
def any_variant_in_stock_or_backorderable?
- if variants.any?
+ if has_variants?
variants_including_master.in_stock_or_backorderable.exists?
else
master.in_stock_or_backorderable?
@@ -653,6 +692,10 @@ module Spree
self.master ||= build_master
end
+ def assign_default_tax_category
+ self.tax_category = Spree::TaxCategory.default if new_record?
+ end
+
def normalize_slug
self.slug = normalize_friendly_id(slug)
end
@@ -718,6 +761,15 @@ module Spree
end
end
+ def ensure_default_shipping_category
+ return if shipping_category.present?
+
+ if new_record?
+ name = I18n.t('spree.seed.shipping.categories.default')
+ self.shipping_category = Spree::ShippingCategory.find_or_create_by!(name: name)
+ end
+ end
+
# Try building a slug based on the following fields in increasing order of specificity.
def slug_candidates
[
diff --git a/core/lib/spree/testing_support/common_rake.rb b/core/lib/spree/testing_support/common_rake.rb
index 01c14e7da0..c2b6a18686 100644
--- a/core/lib/spree/testing_support/common_rake.rb
+++ b/core/lib/spree/testing_support/common_rake.rb
@@ -46,7 +46,7 @@ namespace :common do
system('bin/rails db:environment:set RAILS_ENV=test > /dev/null 2>&1')
system('bundle exec rake db:drop db:create > /dev/null 2>&1')
Spree::DummyModelGenerator.start
- system('bundle exec rake db:migrate')
+ system('bundle exec rake db:migrate > /dev/null 2>&1')
begin
require "generators/#{ENV['LIB_NAME']}/install/install_generator"
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index 23e8fc0eb5..ac7a9c3bbd 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -12,6 +12,63 @@ describe Spree::Product, type: :model do
let!(:store) { Spree::Store.default }
+ describe 'after_initialize :assign_default_tax_category' do
+ let!(:tax_category_1) { create(:tax_category, is_default: false) }
+ let!(:tax_category_2) { create(:tax_category, is_default: true) }
+
+ context 'when product is new' do
+ let(:product) { described_class.new(stores: [store]) }
+
+ it 'assigns default tax category' do
+ expect(product.tax_category.id).to eq(tax_category_2.id)
+ end
+ end
+
+ context 'when product is persisted' do
+ let(:product) { create(:product, tax_category: nil, stores: [store]) }
+
+ it 'does not assign default tax category' do
+ expect(product.tax_category_id).to be(nil)
+ end
+ end
+ end
+
+ describe 'before_validation :ensure_default_shipping_category' do
+ subject { product.valid? }
+
+ let(:product) { build(:product, shipping_category: nil, stores: [store]) }
+
+ let!(:shipping_category_1) { create(:shipping_category, name: I18n.t('spree.seed.shipping.categories.digital')) }
+ let!(:shipping_category_2) { create(:shipping_category, name: I18n.t('spree.seed.shipping.categories.default')) }
+
+ it 'assigns the default shipping category' do
+ subject
+ expect(product.shipping_category).to eq(shipping_category_2)
+ end
+
+ context 'when product has a shipping category' do
+ let(:product) { build(:product, shipping_category: shipping_category_1, stores: [store]) }
+
+ it 'keeps the assigned shipping category' do
+ subject
+ expect(product.shipping_category).to eq(shipping_category_1)
+ end
+ end
+
+ context 'when product is persisted' do
+ let(:product) { create(:product, stores: [store]) }
+
+ before do
+ end
+
+ it 'does not assign the default shipping category' do
+ product.update(shipping_category: nil)
+ expect(subject).to be(false)
+ expect(product.shipping_category).to be(nil)
+ end
+ end
+ end
+
context 'product instance' do
let(:product) { create(:product, stores: [store]) }
let(:variant) { create(:variant, product: product) }
@@ -26,14 +83,14 @@ describe Spree::Product, type: :model do
product.master.stock_items.where(variant: product.master).update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
product.master.stock_items.where(variant: product.master).update_all(count_on_hand: 10) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq false
+ expect(product.reload.send("#{method_name}?")).to eq false
end
it "returns true if variant is #{method_name.humanize.downcase}" do
variant.stock_items.update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
variant.stock_items.update_all(count_on_hand: 10) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq true
+ expect(product.reload.send("#{method_name}?")).to eq true
end
end
@@ -42,14 +99,14 @@ describe Spree::Product, type: :model do
product.master.stock_items.update_all(backorderable: false) if %w[purchasable backorderable].include?(method_name)
product.master.stock_items.update_all(count_on_hand: 0) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq false
+ expect(product.reload.send("#{method_name}?")).to eq false
end
it "returns true if master is #{method_name.humanize.downcase}" do
product.master.stock_items.update_all(backorderable: true) if %w[purchasable backorderable].include?(method_name)
product.master.stock_items.update_all(count_on_hand: 10) if method_name == 'in_stock'
- expect(product.send("#{method_name}?")).to eq true
+ expect(product.reload.send("#{method_name}?")).to eq true
end
end
end
@@ -774,7 +831,7 @@ describe Spree::Product, type: :model do
before { variant_2.stock_items.first.adjust_count_on_hand(1) }
it 'returns first non-master in stock variant' do
- expect(product.default_variant).to eq(variant_2)
+ expect(product.reload.default_variant).to eq(variant_2)
end
end
@@ -782,13 +839,13 @@ describe Spree::Product, type: :model do
before { variant_2.stock_items.first.update(backorderable: true) }
it 'returns first non-master backorderable variant' do
- expect(product.default_variant).to eq(variant_2)
+ expect(product.reload.default_variant).to eq(variant_2)
end
end
context 'product without variants in stock or backorerable' do
it 'returns first non-master variant' do
- expect(product.default_variant).to eq(variant_1)
+ expect(product.reload.default_variant).to eq(variant_1)
end
end
end
@@ -806,13 +863,13 @@ describe Spree::Product, type: :model do
after { Spree::Config[:track_inventory_levels] = true }
it 'returns first non-master variant' do
- expect(product.default_variant).to eq(variant_1)
+ expect(product.reload.default_variant).to eq(variant_1)
end
end
context 'product without variants' do
it 'returns master variant' do
- expect(product.default_variant).to eq(product.master)
+ expect(product.reload.default_variant).to eq(product.master)
end
end
end
@@ -825,13 +882,13 @@ describe Spree::Product, type: :model do
let!(:variant) { create(:variant, product: product) }
it 'returns first non-master variant ID' do
- expect(product.default_variant_id).to eq(variant.id)
+ expect(product.reload.default_variant_id).to eq(variant.id)
end
end
context 'product without variants' do
it 'returns master variant ID' do
- expect(product.default_variant_id).to eq(product.master.id)
+ expect(product.reload.default_variant_id).to eq(product.master.id)
end
end
end
@@ -920,7 +977,10 @@ describe Spree::Product, type: :model do
# makes the stock items available for the before hook
let!(:variant) { create(:variant, product: product) }
- before { Spree::StockItem.update_all(backorderable: false) }
+ before do
+ Spree::StockItem.update_all(backorderable: false)
+ product.reload
+ end
context 'with at least one non-master variant stock items count_on_hand > 0' do
before do
@@ -1098,7 +1158,7 @@ describe Spree::Product, type: :model do
let!(:variant2) { create(:variant, product: product) }
it 'returns an array with CSV data for each variant' do
- csv_lines = product.to_csv(store)
+ csv_lines = product.reload.to_csv(store)
expect(csv_lines.size).to eq(2)
expect(csv_lines.first).to include(product.name)
@@ -1145,6 +1205,179 @@ describe Spree::Product, type: :model do
end
end
+ describe '#first_or_default_variant' do
+ subject { product.first_or_default_variant('USD') }
+
+ let!(:product) { create(:product, stores: [store]) }
+
+ context 'without variants' do
+ it 'returns the default variant' do
+ expect(subject).to eq(product.master)
+ end
+ end
+
+ context 'with a variant in the given currency' do
+ let!(:variant_1) { create(:variant, product: product) }
+ let!(:variant_2) { create(:variant, product: product) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+
+ create(:price, variant: variant_1, currency: 'PLN', amount: 10)
+ create(:price, variant: variant_2, currency: 'USD', amount: 10)
+ end
+
+ it 'returns the available variant in the given currency' do
+ expect(subject).to eq(variant_2)
+ end
+ end
+
+ context 'with all variants in different currencies' do
+ let!(:variant_1) { create(:variant, product: product) }
+ let!(:variant_2) { create(:variant, product: product) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+
+ create(:price, variant: variant_1, currency: 'PLN', amount: 10)
+ create(:price, variant: variant_2, currency: 'GBP', amount: 10)
+ end
+
+ it 'returns the first variant' do
+ expect(subject).to eq(variant_1)
+ end
+ end
+ end
+
+ describe '#first_available_variant' do
+ subject { product.first_available_variant('USD') }
+
+ let!(:product) { create(:product, stores: [store]) }
+
+ let!(:variant_1) { create(:variant, product: product, create_stock: false) }
+ let!(:variant_2) { create(:variant, product: product) }
+ let!(:variant_3) { create(:variant, product: product) }
+ let!(:variant_4) { create(:variant, product: product) }
+ let!(:variant_5) { create(:variant, product: product) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+
+ create(:price, variant: variant_2, currency: 'PLN', amount: 10)
+ create(:price, variant: variant_4, currency: 'USD', amount: 20)
+ create(:price, variant: variant_5, currency: 'USD', amount: 10)
+ end
+
+ it 'returns the first available variant' do
+ expect(subject).to eq(variant_4)
+ end
+ end
+
+ describe '#price_varies?' do
+ subject { product.price_varies?('USD') }
+
+ let!(:product) { create(:product, stores: [store]) }
+
+ let!(:variant_1) { create(:variant, product: product) }
+ let!(:variant_2) { create(:variant, product: product) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+ end
+
+ context 'when all variants have the same price in the given currency' do
+ before do
+ create(:price, variant: variant_1, currency: 'USD', amount: 10)
+ create(:price, variant: variant_1, currency: 'PLN', amount: 15)
+
+ create(:price, variant: variant_2, currency: 'USD', amount: 10)
+ create(:price, variant: variant_2, currency: 'PL', amount: 20)
+ end
+
+ it { is_expected.to be(false) }
+ end
+
+ context 'when variants have different prices in the given currency' do
+ before do
+ create(:price, variant: variant_1, currency: 'USD', amount: 10)
+ create(:price, variant: variant_2, currency: 'USD', amount: 20)
+ end
+
+ it { is_expected.to be(true) }
+ end
+ end
+
+ describe '#any_variant_available?' do
+ subject { product.any_variant_available?('USD') }
+
+ let!(:product) { create(:product, stores: [store]) }
+
+ context 'without variants' do
+ before do
+ product.master.prices.where(currency: 'USD').delete_all
+ product.master.prices.create(currency: currency, amount: 10)
+ end
+
+ context 'when master variant is available' do
+ let(:currency) { 'USD' }
+ it { is_expected.to be(true) }
+ end
+
+ context 'when master variant is not available' do
+ let(:currency) { 'PLN' }
+ it { is_expected.to be(false) }
+ end
+ end
+
+ context 'with variants' do
+ let!(:variant_1) { create(:variant, product: product) }
+ let!(:variant_2) { create(:variant, product: product) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+
+ create(:price, variant: variant_1, currency: 'PLN', amount: 10)
+ create(:price, variant: variant_2, currency: currency, amount: 10)
+ end
+
+ context 'when all variants are available' do
+ let(:currency) { 'USD' }
+ it { is_expected.to be(true) }
+ end
+
+ context 'when no variants are available' do
+ let(:currency) { 'PLN' }
+ it { is_expected.to be(false) }
+ end
+ end
+ end
+
+ describe '#lowest_price' do
+ subject { product.lowest_price('USD') }
+
+ let!(:product) { create(:product, stores: [store]) }
+
+ let!(:variant_1) { create(:variant, product: product) }
+ let!(:variant_2) { create(:variant, product: product) }
+ let!(:variant_3) { create(:variant, product: product) }
+
+ let(:price_1) { create(:price, variant: variant_1, currency: 'PLN', amount: 10) }
+ let(:price_2) { create(:price, variant: variant_2, currency: 'USD', amount: 20) }
+ let(:price_3) { create(:price, variant: variant_3, currency: 'USD', amount: 18) }
+
+ before do
+ product.reload.prices.where(currency: 'USD').delete_all
+
+ price_1
+ price_2
+ price_3
+ end
+
+ it 'returns the lowest price' do
+ expect(subject).to eq(price_3)
+ end
+ end
+
describe 'scopes' do
describe '.not_discontinued' do
let(:product) { create(:product, stores: [store]) }
@@ -1168,71 +1401,76 @@ describe Spree::Product, type: :model do
end
describe '.available' do
- let!(:discontinued_product) { create(:product, discontinue_on: 1.day.ago) }
- let!(:future_product) { create(:product, available_on: 1.day.from_now, status: 'active') }
- let!(:active_product) { create(:product, available_on: 1.day.ago, status: 'active') }
-
- context 'when available_on is specified' do
- let(:current_time) { Time.current }
-
- it 'returns products available before or on the specified date' do
- available_products = described_class.available(current_time)
-
- expect(available_products).to include(active_product)
- expect(available_products).not_to include(future_product)
- expect(available_products).not_to include(discontinued_product)
- end
- end
-
- context 'when available_on is not specified' do
- it 'returns active, not discontinued products' do
- available_products = described_class.available
-
- expect(available_products).to include(active_product)
- expect(available_products).to include(future_product)
- expect(available_products).not_to include(discontinued_product)
- end
- end
-
- context 'when show_products_without_price is false' do
- before do
- Spree::Config.show_products_without_price = false
- end
-
- let!(:product_without_price) { create(:product, status: 'active') }
- let!(:product_with_price) { create(:product, status: 'active') }
-
- before do
- product_without_price.master.prices.delete_all
- end
-
- it 'only returns products with prices in the specified currency' do
- available_products = described_class.available(nil, 'USD')
-
- expect(available_products).to include(product_with_price)
- expect(available_products).not_to include(product_without_price)
- end
- end
-
- context 'when show_products_without_price is true' do
- before do
- Spree::Config.show_products_without_price = true
- end
-
- let!(:product_without_price) { create(:product, status: 'active') }
- let!(:product_with_price) { create(:product, status: 'active') }
-
- before do
- product_without_price.master.prices.delete_all
- end
-
- it 'returns products regardless of price' do
- available_products = described_class.available(nil, 'USD')
-
- expect(available_products).to include(product_with_price)
- expect(available_products).to include(product_without_price)
- end
- end
+ let!(:discontinued_product) { create(:product, discontinue_on: 1.day.ago, stores: [store]) }
+ let!(:future_product) { create(:product, available_on: 1.day.from_now, status: 'active', stores: [store]) }
+ let!(:active_product) { create(:product, available_on: 1.day.ago, status: 'active', stores: [store]) }
+
+ let!(:prices) do
+ [
+ create(:price, variant: active_product.default_variant, currency: 'USD', amount: 10),
+ create(:price, variant: future_product.default_variant, currency: 'USD', amount: 10),
+ create(:price, variant: discontinued_product.default_variant, currency: 'USD', amount: 10)
+ ]
+ end
+
+ context 'when available_on is specified' do
+ subject(:available_products) { described_class.available(Time.current) }
+
+ it 'returns products available before or on the specified date' do
+ expect(available_products).to contain_exactly(active_product)
+ end
+ end
+
+ context 'when available_on is not specified' do
+ subject(:available_products) { described_class.available }
+
+ it 'returns active, not discontinued products' do
+ expect(available_products).to contain_exactly(active_product, future_product)
+ end
+ end
+
+ context 'when show_products_without_price is false' do
+ subject(:available_products) { described_class.available(nil, 'USD') }
+
+ before do
+ Spree::Config.show_products_without_price = false
+ end
+
+ let(:active_product_2) { create(:product, status: 'active', stores: [store]) }
+ let(:active_product_3) { create(:product, status: 'active', stores: [store]) }
+ let(:active_product_4) { create(:product, status: 'active', stores: [store]) }
+
+ before do
+ active_product_2.default_variant.prices.create(currency: 'USD', amount: 10)
+ active_product_3.default_variant.prices.create(currency: 'PLN', amount: 10)
+ end
+
+ it 'only returns products with prices in the specified currency' do
+ expect(available_products).to contain_exactly(future_product, active_product, active_product_2)
+ end
+ end
+
+ context 'when show_products_without_price is true' do
+ subject(:available_products) { described_class.available(nil, 'USD') }
+ before do
+ Spree::Config.show_products_without_price = true
+ end
+
+ let(:active_product_2) { create(:product, status: 'active', stores: [store]) }
+ let(:active_product_3) { create(:product, status: 'active', stores: [store]) }
+ let(:active_product_4) { create(:product, status: 'active', stores: [store]) }
+
+ before do
+ active_product_2.default_variant.prices.create(currency: 'USD', amount: 10)
+ active_product_3.default_variant.prices.create(currency: 'PLN', amount: 10)
+ end
+
+ it 'returns products regardless of price' do
+ expect(available_products).to contain_exactly(
+ future_product, active_product, active_product_2, active_product_3, active_product_4
+ )
+ end
+ end
end
end
end