Skip to content

Commit

Permalink
Rename currently_valid to prioritized_for_default
Browse files Browse the repository at this point in the history
It conveys better what it's doing, as the query only consists of `ORDER`
clauses and no record is filtered out.
  • Loading branch information
waiting-for-dev committed Mar 23, 2021
1 parent a921983 commit db31154
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/prices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions core/app/models/concerns/spree/default_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def self.default_pricing

# Returns `#prices` prioritized for being considered as default price
#
# @return [Array<Spree::Price>]
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}
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/variant/price_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions core/spec/models/spree/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit db31154

Please sign in to comment.