@@ -102,80 +102,43 @@ def view_all_dates
102
102
def show
103
103
end
104
104
105
- def new # rubocop:disable MethodLength
105
+ def new
106
106
if cart . items . empty?
107
107
flash [ :error ] = 'You need to add items to your cart before making a ' \
108
108
'reservation.'
109
109
redirect_loc = ( request . env [ 'HTTP_REFERER' ] . present? ? :back : root_path )
110
110
redirect_to redirect_loc
111
- else
112
- # error handling
113
- @errors = cart . validate_all
114
- unless @errors . empty?
115
- if can? :override , :reservation_errors
116
- flash [ :error ] = 'Are you sure you want to continue? Please review ' \
117
- 'the errors below.'
118
- else
119
- flash [ :error ] = 'Please review the errors below. If uncorrected, ' \
120
- 'any reservations with errors will be filed as a request, and ' \
121
- 'subject to administrator approval.'
122
- end
123
- end
111
+ return
112
+ end
124
113
114
+ # error handling
115
+ @errors = cart . validate_all
116
+ if @errors . empty?
125
117
# this is used to initialize each reservation later
126
118
@reservation = Reservation . new ( start_date : cart . start_date ,
127
119
due_date : cart . due_date ,
128
120
reserver_id : cart . reserver_id )
121
+ else
122
+ handle_new_reservation_errors
129
123
end
130
124
end
131
125
132
- def create # rubocop:disable all
126
+ def create
127
+ # store information about the cart because it is purged if reservations
128
+ # are successfully created
133
129
@errors = cart . validate_all
134
- notes = params [ :reservation ] [ :notes ]
135
- requested = !@errors . empty? && ( cannot? :override , :reservation_errors )
136
-
137
- # check for missing notes and validation errors
138
- if !@errors . blank? && notes . blank?
139
- # there were errors but they didn't fill out the notes
140
- flash [ :error ] = 'Please give a short justification for this ' \
141
- "reservation #{ requested ? 'request' : 'override' } "
142
- @notes_required = true
143
- if AppConfig . get ( :request_text ) . empty?
144
- @request_text = 'Please give a short justification for this ' \
145
- 'equipment request.'
146
- else
147
- @request_text = AppConfig . get ( :request_text )
148
- end
149
- render ( :new ) && return
150
- end
151
-
152
- Reservation . transaction do
153
- begin
154
- start_date = cart . start_date
155
- reserver = cart . reserver_id
156
- notes = format_errors ( @errors ) + notes . to_s
157
- if requested
158
- flash [ :notice ] = cart . request_all ( current_user ,
159
- params [ :reservation ] [ :notes ] )
160
- else
161
- flash [ :notice ] = cart . reserve_all ( current_user ,
162
- params [ :reservation ] [ :notes ] )
163
- end
164
-
165
- if ( cannot? :manage , Reservation ) || ( requested == true )
166
- redirect_to ( catalog_path ) && return
167
- end
168
- if start_date == Time . zone . today
169
- flash [ :notice ] += ' Are you simultaneously checking out equipment ' \
170
- 'for someone? Note that only the reservation has been made. ' \
171
- 'Don\'t forget to continue to checkout.'
172
- end
173
- redirect_to ( manage_reservations_for_user_path ( reserver ) ) && return
174
- rescue ActiveRecord ::RecordNotSaved , ActiveRecord ::RecordInvalid => e
175
- redirect_to catalog_path , flash : { error : 'Oops, something went ' \
176
- "wrong with making your reservation.<br/> #{ e . message } " . html_safe }
177
- raise ActiveRecord ::Rollback
178
- end
130
+ start_date = cart . start_date
131
+ reserver_id = cart . reserver_id
132
+ creator =
133
+ ReservationCreator . new ( cart : cart , current_user : current_user ,
134
+ override : can? ( :override , :reservation_errors ) ,
135
+ notes : params [ :reservation ] [ :notes ] )
136
+ result = creator . create!
137
+ if result [ :error ]
138
+ handle_create_errors ( result [ :error ] )
139
+ else
140
+ handle_create_success ( result [ :result ] , start_date , reserver_id ,
141
+ creator . request? )
179
142
end
180
143
end
181
144
@@ -511,6 +474,58 @@ def archive # rubocop:disable all
511
474
512
475
private
513
476
477
+ def handle_new_reservation_errors
478
+ if can? :override , :reservation_errors
479
+ flash [ :error ] = 'Are you sure you want to continue? Please review ' \
480
+ 'the errors below.'
481
+ render :new
482
+ elsif AppConfig . check ( :disable_requests )
483
+ flash [ :error ] = 'Please review the errors below.'
484
+ render :requests_disabled
485
+ else
486
+ flash [ :error ] = 'Please review the errors below. If uncorrected, ' \
487
+ 'any reservations with errors will be filed as a request, and ' \
488
+ 'subject to administrator approval.'
489
+ render :new_request
490
+ end
491
+ end
492
+
493
+ def handle_create_errors ( errors )
494
+ case errors
495
+ when 'needs notes'
496
+ flash [ :error ] = 'Please give a short justification for this reservation'
497
+ @notes_required = true
498
+ @request_text = if AppConfig . get ( :request_text ) . empty?
499
+ 'Please give a short justification for this ' \
500
+ 'equipment request.'
501
+ else
502
+ AppConfig . get ( :request_text )
503
+ end
504
+ render :new_request
505
+ when 'requests disabled'
506
+ flash [ :error ] = 'Unable to create reservation'
507
+ render :requests_disabled
508
+ else
509
+ redirect_to catalog_path ,
510
+ flash : { error : 'Oops, something went wrong with making ' \
511
+ "your reservation.<br/> #{ errors } " . html_safe }
512
+ end
513
+ end
514
+
515
+ def handle_create_success ( messages , start_date , reserver_id , requested )
516
+ flash [ :notice ] = messages
517
+ if can? ( :manage , Reservation ) && !requested
518
+ if start_date == Time . zone . today
519
+ flash [ :notice ] += ' Are you simultaneously checking out equipment ' \
520
+ 'for someone? Note that only the reservation has been made. ' \
521
+ 'Don\'t forget to continue to checkout.'
522
+ end
523
+ redirect_to ( manage_reservations_for_user_path ( reserver_id ) )
524
+ else
525
+ redirect_to ( catalog_path )
526
+ end
527
+ end
528
+
514
529
def reservation_params
515
530
params . require ( :reservation )
516
531
. permit ( :checkout_handler_id , :checkin_handler_id ,
0 commit comments