From db31154b0f4b1f6059c9ee77b4a144561b58f68e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Busqu=C3=A9?= Date: Tue, 23 Mar 2021 07:12:48 +0100 Subject: [PATCH] Rename currently_valid to prioritized_for_default It conveys better what it's doing, as the query only consists of `ORDER` clauses and no record is filtered out. --- backend/app/controllers/spree/admin/prices_controller.rb | 4 ++-- core/app/models/concerns/spree/default_price.rb | 6 +++--- core/app/models/spree/price.rb | 2 +- core/app/models/spree/variant/price_selector.rb | 4 ++-- core/spec/models/spree/price_spec.rb | 6 +++--- core/spec/models/spree/variant_spec.rb | 4 ++-- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/backend/app/controllers/spree/admin/prices_controller.rb b/backend/app/controllers/spree/admin/prices_controller.rb index 254fbb00fda..96b58af2b65 100644 --- a/backend/app/controllers/spree/admin/prices_controller.rb +++ b/backend/app/controllers/spree/admin/prices_controller.rb @@ -10,12 +10,12 @@ def index @search = @product.prices.accessible_by(current_ability, :index).ransack(params[:q]) @master_prices = @search.result - .currently_valid + .prioritized_for_default .for_master .order(:variant_id, :country_iso, :currency) .page(params[:page]).per(Spree::Config.admin_variants_per_page) @variant_prices = @search.result - .currently_valid + .prioritized_for_default .for_variant .order(:variant_id, :country_iso, :currency) .page(params[:page]).per(Spree::Config.admin_variants_per_page) diff --git a/core/app/models/concerns/spree/default_price.rb b/core/app/models/concerns/spree/default_price.rb index 8ab06d50ae0..cab567ee9b2 100644 --- a/core/app/models/concerns/spree/default_price.rb +++ b/core/app/models/concerns/spree/default_price.rb @@ -16,9 +16,9 @@ def self.default_pricing # Returns `#prices` prioritized for being considered as default price # - # @return [Array] - def currently_valid_prices - prices.currently_valid + # @return [ActiveRecord::Relation] relation of {Spree::Price} + def prioritized_for_default_prices + prices.prioritized_for_default end # Returns {#default_price} or builds it from {Spree::Price.default_pricing} diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb index 3832172875d..f5d129f6c76 100644 --- a/core/app/models/spree/price.rb +++ b/core/app/models/spree/price.rb @@ -20,7 +20,7 @@ class Price < Spree::Base validates :currency, inclusion: { in: ::Money::Currency.all.map(&:iso_code), message: :invalid_code } validates :country, presence: true, unless: -> { for_any_country? } - scope :currently_valid, -> { order(Arel.sql("country_iso IS NULL")).order(created_at: :DESC, id: :DESC) } + scope :prioritized_for_default, -> { order(Arel.sql("country_iso IS NULL")).order(created_at: :DESC, id: :DESC) } scope :for_master, -> { joins(:variant).where(spree_variants: { is_master: true }) } scope :for_variant, -> { joins(:variant).where(spree_variants: { is_master: false }) } scope :for_any_country, -> { where(country: nil) } diff --git a/core/app/models/spree/variant/price_selector.rb b/core/app/models/spree/variant/price_selector.rb index c6c4f57fa1f..fbdac1b0419 100644 --- a/core/app/models/spree/variant/price_selector.rb +++ b/core/app/models/spree/variant/price_selector.rb @@ -26,11 +26,11 @@ def initialize(variant) # @param [Spree::Variant::PricingOptions] price_options Pricing Options to abide by # @return [Spree::Money, nil] The most specific price for this set of pricing options. def price_for(price_options) - variant.currently_valid_prices.detect do |price| + variant.prioritized_for_default_prices.detect do |price| ( price.country_iso == price_options.desired_attributes[:country_iso] || price.country_iso.nil? ) && price.currency == price_options.desired_attributes[:currency] - end.try!(:money) + end&.money end end end diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb index a0f14b11d85..abdc3d0216a 100644 --- a/core/spec/models/spree/price_spec.rb +++ b/core/spec/models/spree/price_spec.rb @@ -97,12 +97,12 @@ end end - describe '.currently_valid' do + describe '.prioritized_for_default' do it 'prioritizes first those associated to a country' do price_1 = create(:price, country: create(:country)) price_2 = create(:price, country: nil) - result = described_class.currently_valid + result = described_class.prioritized_for_default expect( result.index(price_1) < result.index(price_2) @@ -114,7 +114,7 @@ price_1 = create(:price, country: nil) price_2 = create(:price, country: nil) - result = described_class.currently_valid + result = described_class.prioritized_for_default expect( result.index(price_2) < result.index(price_1) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index f46695d528c..36a7d03120c 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -306,13 +306,13 @@ end end - describe '#currently_valid_prices' do + describe '#prioritized_for_default_prices' do it 'returns prioritized prices' do price_1 = create(:price, country: create(:country)) price_2 = create(:price, country: nil) variant = create(:variant, prices: [price_1, price_2]) - result = variant.currently_valid_prices + result = variant.prioritized_for_default_prices expect( result.index(price_1) < result.index(price_2)