Skip to content

Commit

Permalink
Gracefully handle empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
dacook committed Jul 18, 2024
1 parent b30172d commit 08db0d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ def scale_for_unit_value

def system
return "custom" unless scales = scales_for_variant_unit(ignore_available_units: true)
return "custom" unless product_scale = @variant.product.variant_unit_scale

scales[product_scale.to_f]['system']
product_scale = @variant.product.variant_unit_scale&.to_f
return "custom" unless product_scale.present? && product_scale.positive?

scales[product_scale]['system']
end

# @returns enumerable with label and value for select
Expand Down
2 changes: 1 addition & 1 deletion spec/services/weights_and_measures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
expect(subject.system).to eq("custom")
end

pending "when unit is valid, but scale is empty string" do
it "when unit is valid, but scale is empty string" do
allow(product).to receive(:variant_unit) { "weight" }
allow(product).to receive(:variant_unit_scale) { "" }
expect(subject.system).to eq("custom")
Expand Down

0 comments on commit 08db0d6

Please sign in to comment.