Skip to content
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
3 changes: 2 additions & 1 deletion core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LineItem < Spree::Base
has_many :inventory_units, inverse_of: :line_item

before_validation :normalize_quantity
before_validation :set_required_attributes
after_initialize :set_required_attributes

validates :variant, presence: true
validates :quantity, numericality: {
Expand Down Expand Up @@ -159,6 +159,7 @@ def normalize_quantity
# Sets tax category, price-related attributes from
# its variant if they are nil and a variant is present.
def set_required_attributes
return if persisted?
return unless variant
self.tax_category ||= variant.tax_category
set_pricing_attributes
Expand Down
30 changes: 12 additions & 18 deletions core/spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,25 @@
end
end

describe 'line item creation' do
describe '.new' do
let(:variant) { create :variant }

subject(:line_item) { Spree::LineItem.new(variant:, order:) }

# Tests for https://github.com/spree/spree/issues/3391
context 'before validation' do
before { line_item.valid? }

it 'copies the variants price' do
expect(line_item.price).to eq(variant.price)
end
it 'copies the variants price' do
expect(line_item.price).to eq(variant.price)
end

it 'copies the variants cost_price' do
expect(line_item.cost_price).to eq(variant.cost_price)
end
it 'copies the variants cost_price' do
expect(line_item.cost_price).to eq(variant.cost_price)
end

it "copies the order's currency" do
expect(line_item.currency).to eq(order.currency)
end
it "copies the order's currency" do
expect(line_item.currency).to eq(order.currency)
end

# Test for https://github.com/spree/spree/issues/3481
it 'copies the variants tax category' do
expect(line_item.tax_category).to eq(line_item.variant.tax_category)
end
it 'copies the variants tax category' do
expect(line_item.tax_category).to eq(line_item.variant.tax_category)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,11 @@
rule.actionable?(line_item)
end

let(:rule_line_item) { Spree::LineItem.new(product: rule_product) }
let(:other_line_item) { Spree::LineItem.new(product: other_product) }
let(:rule_line_item) { build :line_item, product: rule_product }
let(:other_line_item) { build :line_item }

let(:rule_options) { super().merge(products: [rule_product]) }
let(:rule_product) { mock_model(Spree::Product) }
let(:other_product) { mock_model(Spree::Product) }
let(:rule_product) { build :product }

context "with 'any' match policy" do
let(:rule_options) { super().merge(preferred_match_policy: 'any') }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
describe "#eligible?(line_item)" do
subject { condition.eligible?(line_item, {}) }

let(:condition_line_item) { Spree::LineItem.new(product: condition_product) }
let(:other_line_item) { Spree::LineItem.new(product: other_product) }
let(:condition_line_item) { build(:line_item, product: condition_product) }
let(:other_line_item) { build(:line_item) }

let(:condition_options) { super().merge(products: [condition_product]) }
let(:condition_product) { mock_model(Spree::Product) }
let(:other_product) { mock_model(Spree::Product) }
let(:condition_product) { build(:product) }

it "is eligible if there are no products" do
expect(condition).to be_eligible(condition_line_item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,11 @@
describe "#eligible?(line_item)" do
subject { condition.eligible?(line_item) }

let(:condition_line_item) { Spree::LineItem.new(product: condition_product) }
let(:other_line_item) { Spree::LineItem.new(product: other_product) }
let(:condition_line_item) { build(:line_item, product: condition_product) }
let(:other_line_item) { build(:line_item) }

let(:condition_options) { super().merge(products: [condition_product]) }
let(:condition_product) { mock_model(Spree::Product) }
let(:other_product) { mock_model(Spree::Product) }
let(:condition_product) { build(:product) }

it "is eligible if there are no products" do
expect(condition).to be_eligible(condition_line_item)
Expand Down
Loading