Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move currently_valid_prices to a method #4073

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
kennyadsl marked this conversation as resolved.
Show resolved Hide resolved
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
kennyadsl marked this conversation as resolved.
Show resolved Hide resolved
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