mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-27 21:06:49 +00:00
We're now handling these values on the frontend, so can keep this simple. fixes up: Add numericality validation for * Add translation for Active Record error message
25 lines
553 B
Ruby
25 lines
553 B
Ruby
# frozen_string_literal: false
|
|
|
|
module Calculator
|
|
class PerItem < Spree::Calculator
|
|
preference :amount, :decimal, default: 0
|
|
|
|
validates :preferred_amount,
|
|
numericality: true
|
|
|
|
def self.description
|
|
I18n.t(:flat_rate_per_item)
|
|
end
|
|
|
|
def compute(object = nil)
|
|
return 0 if object.nil?
|
|
|
|
number_of_line_items = line_items_for(object).reduce(0) do |sum, line_item|
|
|
value_to_add = line_item.quantity
|
|
sum + value_to_add
|
|
end
|
|
preferred_amount * number_of_line_items
|
|
end
|
|
end
|
|
end
|