class ListPayment
  include SimplyStored::Couch
  include ActiveModel::SerializerSupport
  property :amount
  property :source # bitcoin, apple_pay, etc...
  belongs_to :list
  belongs_to :user

  after_save :close_list_when_is_paid
  validates :list_id, presence: true

  def close_list_when_is_paid
    amount_paid = list.list_payments.inject(0.0){|sum, payment| sum + payment.amount}
    list.close! if amount_paid >= list.price
  end
end
