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.

Added yard docs and more specs to Pricing classes

Damian Legawiece11dc2e3486c
diff --git a/core/app/models/spree/price_list.rb b/core/app/models/spree/price_list.rb
index 61259d62ea..c46d2c128b 100644
--- a/core/app/models/spree/price_list.rb
+++ b/core/app/models/spree/price_list.rb
@@ -1,5 +1,5 @@
module Spree
- class PriceList < Spree::Base
+ class PriceList < Spree.base_class
acts_as_paranoid
acts_as_list scope: :store
@@ -18,12 +18,17 @@ module Spree
# Override default nested attributes to use bulk_update_prices for performance
attr_reader :prices_attributes
+ # Sets the prices attributes for bulk update
+ # @param attributes [Array<Hash>] array of price attributes with :id, :amount, :compare_at_amount
+ # @return [Array<Hash>] array of price attributes with :id, :amount, :compare_at_amount
def prices_attributes=(attributes)
@prices_attributes = attributes.values
end
after_update :process_bulk_prices_update
+ # Processes the bulk prices update
+ # @return [void]
def process_bulk_prices_update
return if @prices_attributes.blank?
@@ -73,6 +78,9 @@ module Spree
MATCH_POLICIES.map { |key| [Spree.t(key), key] }
end
+ # Returns true if the price list is applicable to the context
+ # @param context [Spree::Pricing::Context]
+ # @return [Boolean]
def applicable?(context)
return false unless active_or_scheduled?
return false unless within_date_range?(context.date || Time.current)
@@ -80,6 +88,9 @@ module Spree
rules_applicable?(context)
end
+ # Returns true if the price list rules are applicable to the context
+ # @param context [Spree::Pricing::Context]
+ # @return [Boolean]
def rules_applicable?(context)
return true if price_rules.none?
@@ -93,6 +104,8 @@ module Spree
end
end
+ # Returns true if the price list is active or scheduled
+ # @return [Boolean]
def active_or_scheduled?
active? || scheduled?
end
@@ -103,6 +116,9 @@ module Spree
active_or_scheduled? && within_date_range?(Time.current)
end
+ # Adds products to the price list
+ # @param product_ids [Array<String>] of product ids
+ # @return [void]
def add_products(product_ids)
return if product_ids.blank?
@@ -134,6 +150,9 @@ module Spree
end
end
+ # Removes products from the price list
+ # @param product_ids [Array<String>] of product ids
+ # @return [void]
def remove_products(product_ids)
return if product_ids.blank?
@@ -197,6 +216,9 @@ module Spree
private
+ # Touches the variants in a background job
+ # @param variant_ids [Array<String>] array of variant ids
+ # @return [void]
def touch_variants(variant_ids)
return if variant_ids.blank?
@@ -211,6 +233,9 @@ module Spree
end
end
+ # Returns true if the date is within the price list date range
+ # @param date [Time] the date to check
+ # @return [Boolean]
def within_date_range?(date)
timezone = store&.preferred_timezone || Rails.application.config.time_zone
date_in_tz = date.in_time_zone(timezone)
diff --git a/core/lib/spree/core/pricing/context.rb b/core/lib/spree/core/pricing/context.rb
index a88fe2644b..6c23184fbe 100644
--- a/core/lib/spree/core/pricing/context.rb
+++ b/core/lib/spree/core/pricing/context.rb
@@ -3,6 +3,15 @@ module Spree
class Context
attr_reader :variant, :currency, :store, :zone, :user, :quantity, :date, :order
+ # Initializes the context
+ # @param variant [Spree::Variant]
+ # @param currency [String]
+ # @param store [Spree::Store]
+ # @param zone [Spree::Zone]
+ # @param user [Spree::User]
+ # @param quantity [Integer]
+ # @param date [Time]
+ # @param order [Spree::Order]
def initialize(variant:, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)
@variant = variant
@currency = currency
@@ -14,6 +23,10 @@ module Spree
@order = order
end
+ # Returns a new context from a variant and currency
+ # @param variant [Spree::Variant]
+ # @param currency [String]
+ # @return [Spree::Pricing::Context]
def self.from_currency(variant, currency)
new(variant: variant, currency: currency)
end
@@ -30,6 +43,8 @@ module Spree
)
end
+ # Returns the cache key for the context
+ # @return [String]
def cache_key
[
'spree',
diff --git a/core/lib/spree/core/pricing/resolver.rb b/core/lib/spree/core/pricing/resolver.rb
index d36467471c..e1497f1019 100644
--- a/core/lib/spree/core/pricing/resolver.rb
+++ b/core/lib/spree/core/pricing/resolver.rb
@@ -3,10 +3,14 @@ module Spree
class Resolver
attr_reader :context
+ # Initializes the resolver
+ # @param context [Spree::Pricing::Context]
def initialize(context)
@context = context
end
+ # Returns the best price for the variant
+ # @return [Spree::Price]
def resolve
Rails.cache.fetch(cache_key, expires_in: 15.minutes) do
find_best_price
@@ -15,6 +19,8 @@ module Spree
private
+ # Returns the best price for the variant
+ # @return [Spree::Price]
def find_best_price
# Try to find a price from applicable price lists first
price_from_list = find_price_from_lists
@@ -24,6 +30,8 @@ module Spree
find_base_price
end
+ # Returns the price from applicable price lists
+ # @return [Spree::Price]
def find_price_from_lists
applicable_price_lists.each do |price_list|
price = find_price_for_list(price_list)
@@ -33,6 +41,8 @@ module Spree
nil
end
+ # Returns the price lists that are applicable to the context
+ # @return [Array<Spree::PriceList>]
def applicable_price_lists
@applicable_price_lists ||= begin
lists = Spree::PriceList.includes(:price_rules)
@@ -43,6 +53,9 @@ module Spree
end
end
+ # Returns the price for a given price list
+ # @param price_list [Spree::PriceList]
+ # @return [Spree::Price]
def find_price_for_list(price_list)
currency = context.currency&.upcase
@@ -62,6 +75,8 @@ module Spree
end
end
+ # Returns the base price for the variant in the current currency
+ # @return [Spree::Price]
def find_base_price
currency = context.currency&.upcase
@@ -82,10 +97,14 @@ module Spree
price || build_empty_price
end
+ # Returns the prices for the variant
+ # @return [ActiveRecord::Relation<Spree::Price>]
def prices
context.variant.prices
end
+ # Builds an empty price for the variant
+ # @return [Spree::Price]
def build_empty_price
context.variant.prices.build(
variant: context.variant,
@@ -95,6 +114,8 @@ module Spree
)
end
+ # Returns the cache key for the resolver
+ # @return [String]
def cache_key
"#{context.cache_key}/resolved_price"
end
diff --git a/core/spec/lib/spree/core/pricing/context_spec.rb b/core/spec/lib/spree/core/pricing/context_spec.rb
new file mode 100644
index 0000000000..3f83602088
--- /dev/null
+++ b/core/spec/lib/spree/core/pricing/context_spec.rb
@@ -0,0 +1,245 @@
+require 'spec_helper'
+
+module Spree
+ module Pricing
+ RSpec.describe Context do
+ let(:variant) { create(:variant) }
+ let(:currency) { 'USD' }
+ let(:store) { create(:store) }
+ let(:zone) { create(:zone) }
+ let(:user) { create(:user) }
+ let(:quantity) { 5 }
+ let(:date) { Time.zone.parse('2024-01-15 12:00:00') }
+
+ describe '#initialize' do
+ context 'with all parameters' do
+ subject do
+ described_class.new(
+ variant: variant,
+ currency: currency,
+ store: store,
+ zone: zone,
+ user: user,
+ quantity: quantity,
+ date: date
+ )
+ end
+
+ it 'sets all attributes' do
+ expect(subject.variant).to eq(variant)
+ expect(subject.currency).to eq(currency)
+ expect(subject.store).to eq(store)
+ expect(subject.zone).to eq(zone)
+ expect(subject.user).to eq(user)
+ expect(subject.quantity).to eq(quantity)
+ expect(subject.date).to eq(date)
+ end
+ end
+
+ context 'with minimal parameters' do
+ subject do
+ described_class.new(variant: variant, currency: currency)
+ end
+
+ it 'sets required attributes' do
+ expect(subject.variant).to eq(variant)
+ expect(subject.currency).to eq(currency)
+ end
+
+ it 'defaults store to default store' do
+ expect(subject.store).to eq(Spree::Store.default)
+ end
+
+ it 'defaults zone to nil' do
+ expect(subject.zone).to be_nil
+ end
+
+ it 'defaults user to nil' do
+ expect(subject.user).to be_nil
+ end
+
+ it 'defaults quantity to nil' do
+ expect(subject.quantity).to be_nil
+ end
+
+ it 'defaults date to current time' do
+ Timecop.freeze do
+ expect(subject.date).to be_within(1.second).of(Time.current)
+ end
+ end
+ end
+ end
+
+ describe '.from_currency' do
+ subject { described_class.from_currency(variant, currency) }
+
+ it 'creates a context with variant and currency' do
+ expect(subject.variant).to eq(variant)
+ expect(subject.currency).to eq(currency)
+ end
+
+ it 'uses default store' do
+ expect(subject.store).to eq(Spree::Store.default)
+ end
+ end
+
+ describe '.from_order' do
+ let(:order) { create(:order_with_line_items, line_items_count: 1) }
+ let(:order_variant) { order.line_items.first.variant }
+ let(:order_quantity) { order.line_items.first.quantity }
+
+ subject { described_class.from_order(order_variant, order) }
+
+ it 'sets variant from parameter' do
+ expect(subject.variant).to eq(order_variant)
+ end
+
+ it 'sets currency from order' do
+ expect(subject.currency).to eq(order.currency)
+ end
+
+ it 'sets store from order' do
+ expect(subject.store).to eq(order.store)
+ end
+
+ it 'sets user from order' do
+ expect(subject.user).to eq(order.user)
+ end
+
+ it 'sets order reference' do
+ expect(subject.order).to eq(order)
+ end
+
+ it 'sets quantity from line item' do
+ expect(subject.quantity).to eq(order_quantity)
+ end
+
+ context 'with zone from order tax_zone' do
+ let(:tax_zone) { create(:zone) }
+
+ before do
+ allow(order).to receive(:tax_zone).and_return(tax_zone)
+ end
+
+ it 'sets zone from order tax_zone' do
+ expect(subject.zone).to eq(tax_zone)
+ end
+ end
+
+ context 'with zone from store checkout_zone' do
+ let(:checkout_zone) { create(:zone) }
+
+ before do
+ allow(order).to receive(:tax_zone).and_return(nil)
+ order.store.update!(checkout_zone: checkout_zone)
+ end
+
+ it 'sets zone from store checkout_zone' do
+ expect(subject.zone).to eq(checkout_zone)
+ end
+ end
+
+ context 'with explicit quantity parameter' do
+ subject { described_class.from_order(order_variant, order, quantity: 10) }
+
+ it 'uses the provided quantity' do
+ expect(subject.quantity).to eq(10)
+ end
+ end
+
+ context 'when variant is not in order' do
+ let(:other_variant) { create(:variant) }
+
+ subject { described_class.from_order(other_variant, order) }
+
+ it 'sets quantity to nil' do
+ expect(subject.quantity).to be_nil
+ end
+ end
+ end
+
+ describe '#cache_key' do
+ context 'with all attributes' do
+ let(:specific_date) { Time.zone.parse('2024-01-15 12:00:00') }
+
+ subject do
+ described_class.new(
+ variant: variant,
+ currency: currency,
+ store: store,
+ zone: zone,
+ user: user,
+ quantity: quantity,
+ date: specific_date
+ )
+ end
+
+ it 'generates a cache key with all components' do
+ expected_key = [
+ 'spree',
+ 'pricing',
+ variant.id,
+ currency,
+ store.id,
+ zone.id,
+ user.id,
+ quantity,
+ specific_date.to_i
+ ].join('/')
+
+ expect(subject.cache_key).to eq(expected_key)
+ end
+ end
+
+ context 'with minimal attributes' do
+ subject do
+ described_class.new(variant: variant, currency: currency)
+ end
+
+ it 'generates a cache key excluding nil values' do
+ Timecop.freeze do
+ expected_key = [
+ 'spree',
+ 'pricing',
+ variant.id,
+ currency,
+ Spree::Store.default.id,
+ Time.current.to_i
+ ].join('/')
+
+ expect(subject.cache_key).to eq(expected_key)
+ end
+ end
+ end
+
+ context 'with some optional attributes' do
+ subject do
+ described_class.new(
+ variant: variant,
+ currency: currency,
+ user: user,
+ quantity: quantity
+ )
+ end
+
+ it 'includes present optional attributes in correct order' do
+ Timecop.freeze do
+ expected_key = [
+ 'spree',
+ 'pricing',
+ variant.id,
+ currency,
+ Spree::Store.default.id,
+ user.id,
+ quantity,
+ Time.current.to_i
+ ].join('/')
+
+ expect(subject.cache_key).to eq(expected_key)
+ end
+ end
+ end
+ end
+ end
+ end
+end