mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-11 23:17:48 +00:00
This can happen when there's a validation error. The field with error will also be marked changed, but the error style will override it. I'd like to move this into a FormBuilder. Existing formbuilder gems don't seem to support it (though I didn't look very hard).
59 lines
1.4 KiB
Ruby
59 lines
1.4 KiB
Ruby
# frozen_string_literal: false
|
|
|
|
module Spree
|
|
class Price < ApplicationRecord
|
|
self.belongs_to_required_by_default = false
|
|
|
|
acts_as_paranoid without_default_scope: true
|
|
|
|
belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'
|
|
|
|
validate :check_price
|
|
validates :amount, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
|
|
|
|
def display_amount
|
|
money
|
|
end
|
|
alias :display_price :display_amount
|
|
|
|
def money
|
|
Spree::Money.new(amount || 0, currency:)
|
|
end
|
|
|
|
def price
|
|
amount
|
|
end
|
|
|
|
def price_changed?
|
|
amount_changed?
|
|
end
|
|
|
|
def price=(price)
|
|
self[:amount] = parse_price(price)
|
|
end
|
|
|
|
private
|
|
|
|
def check_price
|
|
return unless currency.nil?
|
|
|
|
self.currency = Spree::Config[:currency]
|
|
end
|
|
|
|
# strips all non-price-like characters from the price, taking into account locale settings
|
|
def parse_price(price)
|
|
return price unless price.is_a?(String)
|
|
|
|
separator, _delimiter = I18n.t([:'number.currency.format.separator',
|
|
:'number.currency.format.delimiter'])
|
|
non_price_characters = /[^0-9\-#{separator}]/
|
|
# Strip everything else first
|
|
price = price.gsub(non_price_characters, '')
|
|
# Then replace the locale-specific decimal separator with the standard separator if necessary
|
|
price.gsub!(separator, '.') unless separator == '.'
|
|
|
|
price.to_d
|
|
end
|
|
end
|
|
end
|