Skip to content

Commit

Permalink
Stock::Quantifier must take a variant, not an id
Browse files Browse the repository at this point in the history
@brchristian pointed out that the previous resolve_variant_id logic was
broken when a variant had been deleted.

This removes the resolve_variant_id private method from Quantifier, and
reuqires that a variant object be passed in. Finding associated variants
should be the resposibility of the caller, and the code to load a
variant unscoped need not be here.

Fixes #4660
  • Loading branch information
jhawthorn authored and radar committed May 13, 2014
1 parent 3ea8a75 commit c76ac47
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion core/app/models/spree/line_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def adjust_quantity
end

def sufficient_stock?
Stock::Quantifier.new(variant_id).can_supply? quantity
Stock::Quantifier.new(variant).can_supply? quantity
end

def insufficient_stock?
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/stock/availability_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def validate(line_item)
quantity = line_item.quantity
end

quantifier = Stock::Quantifier.new(line_item.variant_id)
quantifier = Stock::Quantifier.new(line_item.variant)

unless quantifier.can_supply? quantity
variant = line_item.variant
Expand Down
10 changes: 1 addition & 9 deletions core/app/models/spree/stock/quantifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Quantifier
attr_reader :stock_items

def initialize(variant)
@variant = resolve_variant_id(variant)
@variant = variant
@stock_items = Spree::StockItem.joins(:stock_location).where(:variant_id => @variant, Spree::StockLocation.table_name =>{ :active => true})
end

Expand All @@ -24,14 +24,6 @@ def can_supply?(required)
total_on_hand >= required || backorderable?
end

private

# return variant when passed either variant object or variant id
def resolve_variant_id(variant)
variant = Spree::Variant.find_by_id(variant) unless variant.respond_to?(:should_track_inventory?)
variant
end

end
end
end

0 comments on commit c76ac47

Please sign in to comment.