Skip to content

Commit

Permalink
Merge pull request #11863 from abdellani/11862-fix-product-shoud-not-…
Browse files Browse the repository at this point in the history
…be-priceless

product should not be priceless
  • Loading branch information
filipefurtad0 authored Dec 1, 2023
2 parents 7c9d4f1 + da90032 commit f94d2c3
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 4 deletions.
5 changes: 2 additions & 3 deletions app/controllers/spree/admin/variants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ def update
flash[:success] = flash_message_for(@object, :successfully_updated)
redirect_to spree.admin_product_variants_url(params[:product_id], @url_filters)
else
redirect_to spree.edit_admin_product_variant_url(params[:product_id],
@object,
@url_filters)
load_data
render :edit
end
end

Expand Down
1 change: 1 addition & 0 deletions app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Product < ApplicationRecord
validates :variant_unit_name,
presence: { if: ->(p) { p.variant_unit == 'items' } }
validate :validate_image
validates :price, numericality: { greater_than_or_equal_to: 0, if: ->{ new_record? } }

accepts_nested_attributes_for :variants, allow_destroy: true
accepts_nested_attributes_for :image
Expand Down
1 change: 1 addition & 0 deletions app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Variant < ApplicationRecord
}

validates :unit_value, numericality: { greater_than: 0 }
validates :price, numericality: { greater_than_or_equal_to: 0 }

validates :unit_description, presence: true, if: ->(variant) {
variant.product&.variant_unit.present? && variant.unit_value.nil?
Expand Down
1 change: 1 addition & 0 deletions lib/spree/core/product_duplicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def duplicate_product
new_product.created_at = nil
new_product.deleted_at = nil
new_product.updated_at = nil
new_product.price = 0
new_product.unit_value = %w(weight volume).include?(product.variant_unit) ? 1.0 : nil
new_product.product_properties = reset_properties
new_product.image = duplicate_image(product.image) if product.image
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/api/v0/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@
expect(response.status).to eq(422)
expect(json_response["error"]).to eq("Invalid resource. Please fix errors and try again.")
errors = json_response["errors"]
expect(errors.keys).to match_array(["name", "primary_taxon", "supplier", "variant_unit"])
expect(errors.keys).to match_array(["name", "primary_taxon", "supplier", "variant_unit",
"price"])
end

it "can update a product" do
Expand Down
1 change: 1 addition & 0 deletions spec/lib/spree/core/product_duplicator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
expect(new_product).to receive(:sku=).with("")
expect(new_product).to receive(:product_properties=).with([new_property])
expect(new_product).to receive(:created_at=).with(nil)
expect(new_product).to receive(:price=).with(0)
expect(new_product).to receive(:unit_value=).with(nil)
expect(new_product).to receive(:updated_at=).with(nil)
expect(new_product).to receive(:deleted_at=).with(nil)
Expand Down

0 comments on commit f94d2c3

Please sign in to comment.