Skip to content

Commit

Permalink
Merge pull request #4073 from nebulab/waiting-for-dev/move_currently_…
Browse files Browse the repository at this point in the history
…valid_to_a_price

Move currently_valid_prices to a method
  • Loading branch information
kennyadsl authored May 28, 2021
2 parents 05a14cd + 515fbe5 commit 9d13705
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 11 deletions.
7 changes: 7 additions & 0 deletions core/app/models/concerns/spree/default_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ module DefaultPrice
autosave: true
end

# Returns `#prices` prioritized for being considered as default price
#
# @return [ActiveRecord::Relation<Spree::Price>]
def currently_valid_prices
prices.currently_valid
end

def find_or_build_default_price
default_price || build_default_price(Spree::Config.default_pricing_options.desired_attributes)
end
Expand Down
8 changes: 0 additions & 8 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class Variant < Spree::Base
stock_items.discard_all
images.destroy_all
prices.discard_all
currently_valid_prices.discard_all
end

attr_writer :rebuild_vat_prices
Expand Down Expand Up @@ -58,13 +57,6 @@ class Variant < Spree::Base
inverse_of: :variant,
autosave: true

has_many :currently_valid_prices,
-> { currently_valid },
class_name: 'Spree::Price',
dependent: :destroy,
inverse_of: :variant,
autosave: true

before_validation :set_cost_currency
before_validation :set_price, if: -> { product && product.master }
before_validation :build_vat_prices, if: -> { rebuild_vat_prices? || new_record? && product }
Expand Down
2 changes: 1 addition & 1 deletion core/lib/spree/core/search/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def add_eagerload_scopes(scope)
# separate queries most of the time but opt for a join as soon as any
# `where` constraints affecting joined tables are added to the search;
# which is the case as soon as a taxon is added to the base scope.
scope = scope.preload(master: :currently_valid_prices)
scope = scope.preload(master: :prices)
scope = scope.preload(master: :images) if @properties[:include_images]
scope
end
Expand Down
27 changes: 27 additions & 0 deletions core/spec/models/spree/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,33 @@
expect(price.country).to eq(country)
end
end

describe '.currently_valid' do
it 'prioritizes first those associated to a country' do
price_1 = create(:price, country: create(:country))
price_2 = create(:price, country: nil) { |price| price.touch }

result = described_class.currently_valid

expect(
result.index(price_1) < result.index(price_2)
).to be(true)
end

context 'when country data is the same' do
it 'prioritizes first those recently updated' do
price_1 = create(:price, country: nil)
price_2 = create(:price, country: nil)
price_1.touch

result = described_class.currently_valid

expect(
result.index(price_1) < result.index(price_2)
).to be(true)
end
end
end
end

describe "#currency" do
Expand Down
2 changes: 0 additions & 2 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,12 @@

expect(variant.stock_items).not_to be_empty
expect(variant.prices).not_to be_empty
expect(variant.currently_valid_prices).not_to be_empty

variant.discard

expect(variant.images).to be_empty
expect(variant.stock_items.reload).to be_empty
expect(variant.prices).to be_empty
expect(variant.currently_valid_prices).to be_empty
end

describe 'default_price' do
Expand Down
14 changes: 14 additions & 0 deletions core/spec/support/concerns/default_price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@
it { is_expected.to be_falsey }
end
end

describe '#currently_valid_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

expect(
result.index(price_1) < result.index(price_2)
).to be(true)
end
end
end

0 comments on commit 9d13705

Please sign in to comment.