Skip to content

Commit

Permalink
S with before, improve variant related validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rioug committed Aug 19, 2024
1 parent df6b80b commit e1c9831
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/controllers/api/v0/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create
authorize! :create, Spree::Product
@product = Spree::Product.new(product_params)

if @product.save
if @product.save(context: :create_and_create_standard_variant)
render json: @product, serializer: Api::Admin::ProductSerializer, status: :created
else
invalid_resource!(@product)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/spree/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def edit
def create
delete_stock_params_and_set_after do
@object.attributes = permitted_resource_params
if @object.save
if @object.save(context: :create_and_create_standard_variant)
flash[:success] = flash_message_for(@object, :successfully_created)
redirect_after_save
else
Expand Down
34 changes: 18 additions & 16 deletions app/models/spree/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ class Product < ApplicationRecord

# These validators are used to make sure the standard variant created via
# `ensure_standard_variant` will be valid. The are only used when creating a new product
validates :supplier_id, presence: true, on: :create
validates :primary_taxon_id, presence: true, on: :create
validates :variant_unit, presence: true, on: :create
validates :unit_value, presence: true, if: ->(product) {
%w(weight volume).include?(product.variant_unit)
}, on: :create
validates :unit_value, numericality: { greater_than: 0 }, allow_blank: true, on: :create
validates :unit_description, presence: true, if: ->(product) {
product.variant_unit.present? && product.unit_value.nil?
}, on: :create
validates :variant_unit_scale, presence: true, if: ->(product) {
%w(weight volume).include?(product.variant_unit)
}, on: :create
validates :variant_unit_name, presence: true, if: ->(product) {
product.variant_unit == 'items'
}, on: :create
with_options on: :create_and_create_standard_variant do
validates :supplier_id, presence: true
validates :primary_taxon_id, presence: true
validates :variant_unit, presence: true
validates :unit_value, presence: true, if: ->(product) {
%w(weight volume).include?(product.variant_unit)
}
validates :unit_value, numericality: { greater_than: 0 }, allow_blank: true
validates :unit_description, presence: true, if: ->(product) {
product.variant_unit.present? && product.unit_value.nil?
}
validates :variant_unit_scale, presence: true, if: ->(product) {
%w(weight volume).include?(product.variant_unit)
}
validates :variant_unit_name, presence: true, if: ->(product) {
product.variant_unit == 'items'
}
end

accepts_nested_attributes_for :image
accepts_nested_attributes_for :product_properties,
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/api/v0/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
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", "price",
"primary_taxon_id", "supplier_id"
"name", "price", "primary_taxon_id",
"supplier_id", "variant_unit"
])
end

Expand Down
60 changes: 47 additions & 13 deletions spec/models/spree/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ module Spree
product.price = 4.27
product.shipping_category_id = shipping_category.id
product.supplier_id = supplier.id
product.save!
product.save(context: :create_and_create_standard_variant)

expect(product.variants.reload.length).to eq 1
standard_variant = product.variants.reload.first
Expand All @@ -162,60 +162,94 @@ module Spree
end

context "with variant attributes" do
it { is_expected.to validate_presence_of(:variant_unit).on(:create) }
it { is_expected.to validate_presence_of(:supplier_id).on(:create) }
it { is_expected.to validate_presence_of(:primary_taxon_id).on(:create) }
it {
is_expected.to validate_presence_of(:variant_unit)
.on(:create_and_create_standard_variant)
}
it {
is_expected.to validate_presence_of(:supplier_id)
.on(:create_and_create_standard_variant)
}
it {
is_expected.to validate_presence_of(:primary_taxon_id)
.on(:create_and_create_standard_variant)
}

describe "unit_value" do
subject { build(:simple_product, variant_unit: "items") }

it {
is_expected.to validate_numericality_of(:unit_value).is_greater_than(0).on(:create)
is_expected.to validate_numericality_of(:unit_value).is_greater_than(0)
.on(:create_and_create_standard_variant)
}
it {
is_expected.not_to validate_presence_of(:unit_value)
.on(:create_and_create_standard_variant)
}
it { is_expected.not_to validate_presence_of(:unit_value).on(:create) }

["weight", "volume"].each do |variant_unit|
context "when variant_unit is #{variant_unit}" do
subject { build(:simple_product, variant_unit:) }

it { is_expected.to validate_presence_of(:unit_value).on(:create) }
it {
is_expected.to validate_presence_of(:unit_value)
.on(:create_and_create_standard_variant)
}
end
end

describe "unit_description" do
it { is_expected.not_to validate_presence_of(:unit_description).on(:create) }
it {
is_expected.not_to validate_presence_of(:unit_description)
.on(:create_and_create_standard_variant)
}

context "when variant_unit is et and unit_value is nil" do
subject {
build(:simple_product, variant_unit: "items", unit_value: nil,
unit_description: "box")
}

it { is_expected.to validate_presence_of(:unit_description).on(:create) }
it {
is_expected.to validate_presence_of(:unit_description)
.on(:create_and_create_standard_variant)
}
end
end

describe "variant_unit_scale" do
it { is_expected.not_to validate_presence_of(:variant_unit_scale).on(:create) }
it {
is_expected.not_to validate_presence_of(:variant_unit_scale)
.on(:create_and_create_standard_variant)
}

["weight", "volume"].each do |variant_unit|
context "when variant_unit is #{variant_unit}" do
subject { build(:simple_product, variant_unit:) }

it { is_expected.to validate_presence_of(:variant_unit_scale).on(:create) }
it {
is_expected.to validate_presence_of(:variant_unit_scale)
.on(:create_and_create_standard_variant)
}
end
end
end

describe "variant_unit_name" do
subject { build(:simple_product, variant_unit: "volume") }

it { is_expected.not_to validate_presence_of(:variant_unit_name).on(:create) }
it {
is_expected.not_to validate_presence_of(:variant_unit_name)
.on(:create_and_create_standard_variant)
}

context "when variant_unit is items" do
subject { build(:simple_product, variant_unit: "items") }

it { is_expected.to validate_presence_of(:variant_unit_name).on(:create) }
it {
is_expected.to validate_presence_of(:variant_unit_name)
.on(:create_and_create_standard_variant)
}
end
end
end
Expand Down

0 comments on commit e1c9831

Please sign in to comment.