Skip to content

Commit

Permalink
Defend against nils in variant serializer
Browse files Browse the repository at this point in the history
If unit_price.denominator ever returns nil, the serializer won't explode now.
  • Loading branch information
Matt-Yorkley committed Mar 30, 2021
1 parent 6a4c7a4 commit f7b3813
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/serializers/api/variant_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def thumb_url
end

def unit_price_price
price_with_fees / unit_price.denominator
price_with_fees / (unit_price.denominator || 1)
end

def unit_price_unit
Expand Down
18 changes: 18 additions & 0 deletions spec/serializers/api/variant_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@
:tag_list # Used to apply tag rules
)
end

describe "#unit_price_price" do
context "without fees" do
it "displays the price divided by the unit price denominator" do
allow(subject).to receive_message_chain(:unit_price, :denominator) { 1000 }

expect(subject.unit_price_price).to eq(variant.price / 1000)
end
end

context "when the denominator returns nil" do
it "returns the price" do
allow(subject).to receive_message_chain(:unit_price, :denominator) { nil }

expect(subject.unit_price_price).to eq(variant.price)
end
end
end
end

0 comments on commit f7b3813

Please sign in to comment.