~singpolyma/sgx-jmp

ref: 4be555de103e992ee7e03feb48b45c1eca917c45 sgx-jmp/lib/buy_account_credit_form.rb -rw-r--r-- 652 bytes
4be555deStephen Paul Weber Split logic out into testable objects 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

require_relative "./xep0122_field"

class BuyAccountCreditForm
	def initialize(customer)
		@customer = customer
	end

	AMOUNT_FIELD =
		XEP0122Field.new(
			"xs:decimal",
			range: (0..1000),
			var: "amount",
			label: "Amount of credit to buy",
			required: true
		).field

	def balance
		{
			type: "fixed",
			value: "Current balance: $#{'%.2f' % @customer.balance}"
		}
	end

	def add_to_form(form)
		@customer.payment_methods.then do |payment_methods|
			form.type = :form
			form.title = "Buy Account Credit"
			form.fields = [
				balance,
				payment_methods.to_list_single,
				AMOUNT_FIELD
			]
		end
	end
end