mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
A previous pull request added support for flexible decimal characters when editing money amounts. https://github.com/openfoodfoundation/openfoodnetwork/pull/1831 This pull request applies the same principle to the weight calculator which was missed in the previous pull request.
21 lines
522 B
Ruby
21 lines
522 B
Ruby
require 'spree/localized_number'
|
|
|
|
module Calculator
|
|
class Weight < Spree::Calculator
|
|
extend Spree::LocalizedNumber
|
|
preference :per_kg, :decimal, default: 0.0
|
|
attr_accessible :preferred_per_kg
|
|
localize_number :preferred_per_kg
|
|
|
|
def self.description
|
|
I18n.t('spree.weight')
|
|
end
|
|
|
|
def compute(object)
|
|
line_items = line_items_for object
|
|
total_weight = line_items.sum { |li| ((li.variant.andand.weight || 0) * li.quantity) }
|
|
total_weight * preferred_per_kg
|
|
end
|
|
end
|
|
end
|