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

Add #total_on_hand method to Variant model #3427

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def in_stock?(quantity=1)
Spree::Stock::Quantifier.new(self).can_supply?(quantity)
end

def total_on_hand
Spree::Stock::Quantifier.new(self).total_on_hand
end

# Product may be created with deleted_at already set,
# which would make AR's default finder return nil.
# This is a stopgap for that little problem.
Expand Down
12 changes: 12 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,16 @@
end
end
end

describe '#total_on_hand' do
it 'should be infinite if track_inventory_levels is false' do
Spree::Config[:track_inventory_levels] = false
build(:variant).total_on_hand.should eql(Float::INFINITY)
end

it 'should match quantifier total_on_hand' do
variant = build(:variant)
expect(variant.total_on_hand).to eq(Spree::Stock::Quantifier.new(variant).total_on_hand)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I feel about this calling to Spree::Stock::Quantifier again. I think just a number would be more suitable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know, I had the same impression. I didn't want to test more specifics cause there's a bunch of tests for it in the quantifier_spec, so I wasn't sure about the right approach. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you just need to make sure that the Variant total_on_hand sends the proper message to Quantifier. So I guess one single spec here should be enough.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alrighty, makes sense. Thank you @rubenrails + @huoxito.

end
end
end