11module SubmissionProcessing
22 extend ActiveSupport ::Concern
33
4- # Common method to process a submission for either form or code
54 def process_submission
6- # Initialize a new submission, handling the case where no fields are enabled
7- @submission = @form . submissions . new
8- @submission . device = @device
5+ build_submission
96
10- # Only attempt to update with submission params if they exist in the request
11- # This handles forms that have no enabled fields
12- @submission . assign_attributes ( submission_params ) if params [ :submission ] . present?
7+ return handle_already_claimed_code if code_already_claimed?
8+
9+ if @submission . save
10+ handle_successful_submission
11+ else
12+ render "public_forms/show" , status : :unprocessable_entity
13+ end
14+ end
1315
14- # Associate the submission with the current user if logged in
16+ private
17+
18+ def build_submission
19+ @submission = @form . submissions . new ( device : @device )
20+ @submission . assign_attributes ( submission_params ) if params [ :submission ] . present?
1521 @submission . user = current_user if current_user
22+ end
1623
17- # Check if this is a code submission and if the code has already been claimed
18- if defined? ( @code ) && @code && @code . claimed?
19- @submission . errors . add ( :base , "This code has already been used" )
20- return render "public_forms/show" , status : :unprocessable_entity
21- end
24+ def code?
25+ defined? ( @code ) && @code
26+ end
2227
23- if @submission . save
24- # Mark the code as claimed if this is a code submission
25- @code . claim! if defined? ( @code ) && @code
26-
27- # Send the submission email only if target_email_address is present
28- send_submission_email
29-
30- # Redirect to thanks page instead of rendering
31- # This prevents form resubmission on refresh
32- if defined? ( @code ) && @code
33- redirect_to code_thanks_path ( @code )
34- else
35- redirect_to form_thanks_path ( @form . code , @device )
36- end
28+ def code_already_claimed?
29+ code? && @code . claimed?
30+ end
31+
32+ def handle_already_claimed_code
33+ @submission . errors . add ( :base , "This code has already been used" )
34+ render "public_forms/show" , status : :unprocessable_entity
35+ end
36+
37+ def handle_successful_submission
38+ claim_code_if_present
39+ send_submission_email
40+ redirect_to_thanks_page
41+ end
42+
43+ def claim_code_if_present
44+ @code . claim! if code?
45+ end
46+
47+ def redirect_to_thanks_page
48+ if code?
49+ redirect_to code_thanks_path ( @code )
3750 else
38- render "public_forms/show" , status : :unprocessable_entity
51+ redirect_to form_thanks_path ( @form . code , @device )
3952 end
4053 end
4154
4255 def send_submission_email
43- if @form . target_email_address . present?
44- begin
45- SubmissionMailer . new_submission ( @submission ) . deliver_later
46- # Email will be sent asynchronously, so we mark it as pending
47- # It will be updated when the job completes
48- rescue => e
49- # Log the error but continue with the form submission
50- Rails . logger . error "Failed to queue submission email: #{ e . message } "
51- @submission . mark_as_failed! if @submission . respond_to? ( :mark_as_failed! )
52- end
56+ return unless @form . target_email_address . present?
57+
58+ begin
59+ SubmissionMailer . new_submission ( @submission ) . deliver_later
60+ rescue => e
61+ log_email_error ( e )
5362 end
5463 end
5564
65+ def log_email_error ( error )
66+ Rails . logger . error "Failed to queue submission email: #{ error . message } "
67+ @submission . mark_as_failed! if @submission . respond_to? ( :mark_as_failed! )
68+ end
69+
5670 def check_form_access
57- # Redirect to login if the form requires login and user is not logged in
58- if @form . require_login && !logged_in?
59- store_location # Store the current URL to redirect back after login
60- redirect_to login_path , alert : "You need to log in to access this form"
61- end
71+ return unless @form . require_login && !logged_in?
72+
73+ store_location
74+ redirect_to login_path , alert : "You need to log in to access this form"
6275 end
6376
6477 def submission_params
@@ -71,7 +84,6 @@ def submission_params
7184 :postcode
7285 )
7386 else
74- # Return an empty hash if there are no submission parameters
7587 { }
7688 end
7789 end
0 commit comments