Skip to content

Commit

Permalink
Add #total_on_hand method to Variant model
Browse files Browse the repository at this point in the history
Fixes #3427
  • Loading branch information
rubenrails authored and radar committed Jul 26, 2013
1 parent d140553 commit 6b44bc7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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)
end
end
end

0 comments on commit 6b44bc7

Please sign in to comment.