Skip to content

Commit

Permalink
Add #total_on_hand method to Variant model
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenrails authored and anthonycreates committed Jul 31, 2013
1 parent 8ec17d1 commit 597d399
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ See #3133 for more information.

* Removed `variants_including_master_and_deleted`, in favour of using the Paranoia gem. This scope would now be achieved using `variants_including_master.with_deleted`.

*Ryan Bigg*
*Ryan Bigg*

* You can now find the total amount on hand of a variant by calling `Variant#total_on_hand`. #3427

*Ruben Ascencio*
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 597d399

Please sign in to comment.