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
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.rbindex 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_classacts_as_paranoidacts_as_list scope: :store@@ -18,12 +18,17 @@ module Spree# Override default nested attributes to use bulk_update_prices for performanceattr_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_amountdef prices_attributes=(attributes)@prices_attributes = attributes.valuesendafter_update :process_bulk_prices_update+ # Processes the bulk prices update+ # @return [void]def process_bulk_prices_updatereturn if @prices_attributes.blank?@@ -73,6 +78,9 @@ module SpreeMATCH_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 Spreerules_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 Spreeendend+ # Returns true if the price list is active or scheduled+ # @return [Boolean]def active_or_scheduled?active? || scheduled?end@@ -103,6 +116,9 @@ module Spreeactive_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 Spreeendend+ # 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 Spreeprivate+ # 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 Spreeendend+ # 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_zonedate_in_tz = date.in_time_zone(timezone)diff --git a/core/lib/spree/core/pricing/context.rb b/core/lib/spree/core/pricing/context.rbindex a88fe2644b..6c23184fbe 100644--- a/core/lib/spree/core/pricing/context.rb+++ b/core/lib/spree/core/pricing/context.rb@@ -3,6 +3,15 @@ module Spreeclass Contextattr_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 = orderend+ # 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.rbindex d36467471c..e1497f1019 100644--- a/core/lib/spree/core/pricing/resolver.rb+++ b/core/lib/spree/core/pricing/resolver.rb@@ -3,10 +3,14 @@ module Spreeclass Resolverattr_reader :context+ # Initializes the resolver+ # @param context [Spree::Pricing::Context]def initialize(context)@context = contextend+ # Returns the best price for the variant+ # @return [Spree::Price]def resolveRails.cache.fetch(cache_key, expires_in: 15.minutes) dofind_best_price@@ -15,6 +19,8 @@ module Spreeprivate+ # Returns the best price for the variant+ # @return [Spree::Price]def find_best_price# Try to find a price from applicable price lists firstprice_from_list = find_price_from_lists@@ -24,6 +30,8 @@ module Spreefind_base_priceend+ # Returns the price from applicable price lists+ # @return [Spree::Price]def find_price_from_listsapplicable_price_lists.each do |price_list|price = find_price_for_list(price_list)@@ -33,6 +41,8 @@ module Spreenilend+ # Returns the price lists that are applicable to the context+ # @return [Array<Spree::PriceList>]def applicable_price_lists@applicable_price_lists ||= beginlists = Spree::PriceList.includes(:price_rules)@@ -43,6 +53,9 @@ module Spreeendend+ # 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 Spreeendend+ # Returns the base price for the variant in the current currency+ # @return [Spree::Price]def find_base_pricecurrency = context.currency&.upcase@@ -82,10 +97,14 @@ module Spreeprice || build_empty_priceend+ # Returns the prices for the variant+ # @return [ActiveRecord::Relation<Spree::Price>]def pricescontext.variant.pricesend+ # Builds an empty price for the variant+ # @return [Spree::Price]def build_empty_pricecontext.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"enddiff --git a/core/spec/lib/spree/core/pricing/context_spec.rb b/core/spec/lib/spree/core/pricing/context_spec.rbnew file mode 100644index 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