2022-04-20 14:12:22 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Read and write the amount attribute, after converting to/from cents.
|
2016-07-05 12:30:52 +02:00
|
|
|
module AmountConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2022-12-01 15:43:16 +01:00
|
|
|
include ApplicationHelper
|
|
|
|
validates :amount, numericality: { greater_than_or_equal_to: 0 }
|
2016-07-05 12:30:52 +02:00
|
|
|
|
|
|
|
def amount=(amount)
|
|
|
|
if amount.nil?
|
|
|
|
write_attribute(:amount, amount)
|
|
|
|
else
|
2022-12-01 15:43:16 +01:00
|
|
|
write_attribute(:amount, to_centimes(amount))
|
2016-07-05 12:30:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def amount
|
|
|
|
if read_attribute(:amount).blank?
|
|
|
|
read_attribute(:amount)
|
|
|
|
else
|
|
|
|
read_attribute(:amount) / 100.0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|