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

Fix variant data inconsistencies #6369

Merged
merged 4 commits into from
Jan 13, 2021
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 app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Variant < ActiveRecord::Base

before_validation :set_cost_currency
before_validation :update_weight_from_unit_value, if: ->(v) { v.product.present? }
before_validation :ensure_unit_value

after_save :save_default_price
after_save :update_units
Expand Down Expand Up @@ -297,5 +298,11 @@ def destruction
exchange_variants(:reload).destroy_all
yield
end

def ensure_unit_value
return unless product&.variant_unit == "items" && unit_value.nil?

self.unit_value = 1.0
andrewpbrett marked this conversation as resolved.
Show resolved Hide resolved
end
andrewpbrett marked this conversation as resolved.
Show resolved Hide resolved
end
end
8 changes: 4 additions & 4 deletions spec/controllers/spree/admin/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

before { controller_login_as_enterprise_user([producer]) }

it 'fails' do
it 'succeeds' do
spree_post :bulk_update,
"products" => [
{
Expand All @@ -50,7 +50,7 @@
}
]

expect(response).to have_http_status(400)
expect(response).to have_http_status(302)
end

it 'does not redirect to bulk_products' do
Expand All @@ -63,8 +63,8 @@
}
]

expect(response).not_to redirect_to(
'/api/products/bulk_products?page=1;per_page=500;'
expect(response).to redirect_to(
'/api/products/bulk_products'
)
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/models/spree/product_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
end

context 'when the product does exist' do
context 'when a different varian_unit is passed' do
context 'when a different variant_unit is passed' do
let!(:product) do
create(
:simple_product,
Expand All @@ -54,22 +54,22 @@
}
end

it 'does not update the product' do
it 'updates the product' do
product_set.save

expect(product.reload.attributes).to include(
'variant_unit' => 'items'
'variant_unit' => 'weight'
)
end

it 'adds an error' do
it 'does not add an error' do
product_set.save
expect(product_set.errors.get(:base))
.to include("Unit value can't be blank")
expect(product_set.errors)
.to be_empty
end

it 'returns false' do
expect(product_set.save).to eq(false)
it 'returns true' do
expect(product_set.save).to eq(true)
end
end

Expand Down
19 changes: 17 additions & 2 deletions spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ module Spree
before do
product.update_attribute :variant_unit, 'items'
product.reload
variant.reload
end

it "is valid with only unit value set" do
Expand All @@ -549,10 +550,11 @@ module Spree
expect(variant).to be_valid
end

it "is invalid when neither unit value nor unit description are set" do
it "sets unit_value to 1.0 before validation if it's nil" do
variant.unit_value = nil
variant.unit_description = nil
expect(variant).not_to be_valid
expect(variant).to be_valid
expect(variant.unit_value).to eq 1.0
end

it "has a valid master variant" do
Expand Down Expand Up @@ -783,4 +785,17 @@ module Spree
expect(e.reload.variant_ids).to be_empty
end
end

describe "#ensure_unit_value" do
let(:product) { create(:product, variant_unit: "weight") }
let(:variant) { create(:variant, product_id: product.id) }

context "when a product's variant_unit value is changed from weight to items" do
it "sets the variant's unit_value to 1" do
product.update(variant_unit: "items")

expect(variant.unit_value).to eq 1
end
end
end
end