Use FormTemplate for activation form

Stephen Paul Weber created

Change summary

.rubocop.yml                    |  2 
forms/registration/activate.rb  | 36 ++++++++++++++
forms/registration/plan_name.rb | 16 ++++++
lib/registration.rb             | 86 +++++-----------------------------
test/test_registration.rb       |  4 
5 files changed, 69 insertions(+), 75 deletions(-)

Detailed changes

.rubocop.yml 🔗

@@ -122,7 +122,7 @@ Style/FormatStringToken:
 
 Style/FrozenStringLiteralComment:
   Exclude:
-    - forms/*
+    - forms/**/*.rb
 
 Naming/AccessorMethodName:
   Enabled: false

forms/registration/activate.rb 🔗

@@ -0,0 +1,36 @@
+form!
+title "Activate JMP"
+
+center = " (#{@rate_center})" if @rate_center
+instructions <<~I
+	You've selected #{@tel}#{center} as your JMP number.
+	To activate your account, you can either deposit $#{CONFIG[:activation_amount]} to your balance or enter your invite code if you have one.
+	(If you'd like to pay in a cryptocurrency other than Bitcoin, currently we recommend using a service like simpleswap.io, morphtoken.com, changenow.io, or godex.io. Manual payment via Bitcoin Cash is also available if you contact support.)
+I
+
+field(
+	var: "activation_method",
+	type: "list-single",
+	label: "Activate using",
+	required: true,
+	options: [
+		{
+			value: "credit_card",
+			label: "Credit Card"
+		},
+		{
+			value: "bitcoin",
+			label: "Bitcoin"
+		},
+		{
+			value: "code",
+			label: "Invite Code"
+		},
+		{
+			value: "mail",
+			label: "Mail or eTransfer"
+		}
+	]
+)
+
+instance_eval File.read("#{__dir__}/plan_name.rb")

forms/registration/plan_name.rb 🔗

@@ -0,0 +1,16 @@
+field(
+	var: "plan_name",
+	type: "list-single",
+	label: "What currency should your account balance be in?",
+	required: true,
+	options: [
+		{
+			value: "cad_beta_unlimited-v20210223",
+			label: "Canadian Dollars"
+		},
+		{
+			value: "usd_beta_unlimited-v20210223",
+			label: "United States Dollars"
+		}
+	]
+)

lib/registration.rb 🔗

@@ -48,85 +48,27 @@ class Registration
 
 		attr_reader :customer, :tel
 
-		FORM_FIELDS = [
-			{
-				var: "activation_method",
-				type: "list-single",
-				label: "Activate using",
-				required: true,
-				options: [
-					{
-						value: "credit_card",
-						label: "Credit Card"
-					},
-					{
-						value: "bitcoin",
-						label: "Bitcoin"
-					},
-					{
-						value: "code",
-						label: "Invite Code"
-					},
-					{
-						value: "mail",
-						label: "Mail or eTransfer"
-					}
-				]
-			},
-			{
-				var: "plan_name",
-				type: "list-single",
-				label: "What currency should your account balance be in?",
-				required: true,
-				options: [
-					{
-						value: "cad_beta_unlimited-v20210223",
-						label: "Canadian Dollars"
-					},
-					{
-						value: "usd_beta_unlimited-v20210223",
-						label: "United States Dollars"
-					}
-				]
-			}
-		].freeze
-
-		ACTIVATE_INSTRUCTION =
-			"To activate your account, you can either deposit " \
-			"$#{CONFIG[:activation_amount]} to your balance or enter " \
-			"your invite code if you have one."
-
-		CRYPTOCURRENCY_INSTRUCTION =
-			"(If you'd like to pay in a cryptocurrency other than " \
-			"Bitcoin, currently we recommend using a service like " \
-			"simpleswap.io, morphtoken.com, changenow.io, or godex.io. " \
-			"Manual payment via Bitcoin Cash is also available if you " \
-			"contact support.)"
-
-		def add_instructions(form, center)
-			center = " (#{center})" if center
-			[
-				"You've selected #{tel}#{center} as your JMP number",
-				ACTIVATE_INSTRUCTION,
-				CRYPTOCURRENCY_INSTRUCTION
-			].each do |txt|
-				form << Blather::XMPPNode.new(:instructions, form.document).tap { |i|
-					i << txt
-				}
-			end
+		def form(center)
+			FormTemplate.render(
+				"registration/activate",
+				tel: tel,
+				rate_center: center
+			)
 		end
 
 		def write
 			rate_center.then { |center|
 				Command.reply do |reply|
 					reply.allowed_actions = [:next]
-					form = reply.form
-					form.type = :form
-					form.title = "Activate JMP"
-					add_instructions(form, center)
-					form.fields = FORM_FIELDS
+					reply.command << form(center)
 				end
-			}.then { |iq| Payment.for(iq, customer, tel) }.then(&:write)
+			}.then(&method(:next_step))
+		end
+
+		def next_step(iq)
+			EMPromise.resolve(nil).then {
+				Payment.for(iq, customer, tel)
+			}.then(&:write)
 		end
 
 	protected

test/test_registration.rb 🔗

@@ -100,8 +100,8 @@ class RegistrationTest < Minitest::Test
 				[Matching.new do |iq|
 					assert_equal :form, iq.form.type
 					assert_equal(
-						"You've selected +15555550000 (FA, KE) as your JMP number",
-						iq.form.instructions
+						"You've selected +15555550000 (FA, KE) as your JMP number.",
+						iq.form.instructions.lines.first.chomp
 					)
 				end]
 			)