Skip to content

Commit

Permalink
Merge pull request #2831 from kristinalim/fix-product_import_date_error
Browse files Browse the repository at this point in the history
Fix product import date error when some but not all variants have import date
  • Loading branch information
sauloperez authored Oct 9, 2018
2 parents 8596215 + da904c9 commit 227dd3e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
5 changes: 1 addition & 4 deletions app/models/spree/product_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ def variants_distributed_by(order_cycle, distributor)

# Get the most recent import_date of a product's variants
def import_date
variants.map do |variant|
next if variant.import_date.blank?
variant.import_date
end.sort.last
variants.map(&:import_date).compact.max
end

# Build a product distribution for each distributor
Expand Down
41 changes: 41 additions & 0 deletions spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -714,4 +714,45 @@ module Spree
end
end
end

describe "product import" do
describe "finding the most recent import date of the variants" do
let!(:product) { create(:product) }

let(:reference_time) { Time.zone.now.beginning_of_day }

before do
product.reload
end

context "when the variants do not have an import date" do
let!(:variant_a) { create(:variant, product: product, import_date: nil) }
let!(:variant_b) { create(:variant, product: product, import_date: nil) }

it "returns nil" do
expect(product.import_date).to be_nil
end
end

context "when some variants have import date and some do not" do
let!(:variant_a) { create(:variant, product: product, import_date: nil) }
let!(:variant_b) { create(:variant, product: product, import_date: reference_time - 1.hour) }
let!(:variant_c) { create(:variant, product: product, import_date: reference_time - 2.hour) }

it "returns the most recent import date" do
expect(product.import_date).to eq(variant_b.import_date)
end
end

context "when all variants have import date" do
let!(:variant_a) { create(:variant, product: product, import_date: reference_time - 2.hour) }
let!(:variant_b) { create(:variant, product: product, import_date: reference_time - 1.hour) }
let!(:variant_c) { create(:variant, product: product, import_date: reference_time - 3.hour) }

it "returns the most recent import date" do
expect(product.import_date).to eq(variant_b.import_date)
end
end
end
end
end

0 comments on commit 227dd3e

Please sign in to comment.