Module: Controllers::HeatLoss

Extended by:
ActiveSupport::Concern
Includes:
HeatLossPathsHelper
Included in:
MyRoomsController
Defined in:
app/concerns/controllers/heat_loss.rb

Constant Summary

Constants included from HeatLossPathsHelper

HeatLossPathsHelper::HEAT_LOSS_ACTIONS

Instance Method Summary collapse

Methods included from HeatLossPathsHelper

#crm_request?, #enter_heat_loss_info_path_for_room, #format_errors_as_list, #heat_loss_path, #hl_btn_submit_class, #hl_card_class, #hl_card_col_class, #hl_card_content_class, #hl_card_header_class, #hl_card_title_class, #hl_field_class, #hl_field_error, #hl_form_control_class, #hl_form_group_class, #hl_form_select_class, #room_path_for_room, #show_heat_loss_path_for_room

Instance Method Details

#ceiling_heat_loss_detailsObject



185
186
187
188
189
190
191
# File 'app/concerns/controllers/heat_loss.rb', line 185

def ceiling_heat_loss_details
  set_ceiling_variables
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#complete_ceiling_heat_loss_detailsObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/concerns/controllers/heat_loss.rb', line 193

def complete_ceiling_heat_loss_details
  if @room.update(params[:room])
    respond_to do |format|
      format.turbo_stream { render action: :heat_loss_details_summary }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_ceiling_variables
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :ceiling_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_doors_heat_loss_detailsObject



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'app/concerns/controllers/heat_loss.rb', line 393

def complete_doors_heat_loss_details
  if @room.update(params[:room])
    respond_to do |format|
      format.turbo_stream { render action: :heat_loss_details_summary }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_doors_variables
    @exterior_wall = @room.exterior_walls.to_a[@exterior_wall_child_index.to_i]
    if @door.nil?
      @door = @exterior_wall.doors.to_a.to_a[@child_index.to_i]
      @child_index = nil
    end
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :doors_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_exterior_walls_heat_loss_detailsObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'app/concerns/controllers/heat_loss.rb', line 278

def complete_exterior_walls_heat_loss_details
  if @room.update(params[:room])
    respond_to do |format|
      format.turbo_stream { render action: :heat_loss_details_summary }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_exterior_walls_variables
    if @exterior_wall.nil?
      @exterior_wall = @room.exterior_walls.to_a[@child_index.to_i]
      @child_index = nil
    end
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :exterior_walls_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_heat_loss_infoObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/concerns/controllers/heat_loss.rb', line 59

def complete_heat_loss_info
  if @room.floor_type && @room.floor_type.is_indoors?
    res = @room.can_calculate_heat_loss?
    if res[:status] == :ok
      @heat_loss = @room.calculate_heat_loss
      redirect_to(show_heat_loss_path_for(@room, request.subdomain), flash: { notice: "Room's heat loss was calculated." })
    else
      flash[:error] = "Can't calculate heat loss yet: #{res[:errors].join(', ')}."
      @room.enter_heat_loss_info = true
      render action: :enter_heat_loss_info
    end
  else
    flash[:error] = 'Heat Loss Calculation is only for indoor rooms.'
    render action: :show
  end
end

#complete_room_heat_loss_detailsObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/concerns/controllers/heat_loss.rb', line 130

def complete_room_heat_loss_details
  @room.enter_heat_loss_info = true
  if @room.update(params[:room])
    set_under_floor_variables
    respond_to do |format|
      format.turbo_stream { render action: :under_floor_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :room_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_skylights_heat_loss_detailsObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/concerns/controllers/heat_loss.rb', line 223

def complete_skylights_heat_loss_details
  if @room.update(params[:room])
    respond_to do |format|
      format.turbo_stream { render action: :heat_loss_details_summary }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_skylights_variables
    if @skylight.nil?
      @skylight = @room.ceiling.skylights.to_a[@child_index.to_i]
      @child_index = nil
    end
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :skylights_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_under_floor_heat_loss_detailsObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/concerns/controllers/heat_loss.rb', line 161

def complete_under_floor_heat_loss_details
  if @room.update(params[:room])
    set_ceiling_variables
    respond_to do |format|
      format.turbo_stream { render action: :ceiling_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_under_floor_variables
    set_validation_errors_flash(@room)
    flash.now[:error] ||= 'Could not save...'
    respond_to do |format|
      format.turbo_stream { render action: :under_floor_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#complete_windows_heat_loss_detailsObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'app/concerns/controllers/heat_loss.rb', line 335

def complete_windows_heat_loss_details
  if @room.update(params[:room])
    respond_to do |format|
      format.turbo_stream { render action: :heat_loss_details_summary }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  else
    set_windows_variables
    @exterior_wall = @room.exterior_walls.to_a[@exterior_wall_child_index.to_i]
    if @window.nil?
      @window = @exterior_wall.windows.to_a[@child_index.to_i]
      @child_index = nil
    end
    set_validation_errors_flash(@room)
    respond_to do |format|
      format.turbo_stream { render action: :windows_heat_loss_details }
      format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
    end
  end
end

#doors_heat_loss_detailsObject



384
385
386
387
388
389
390
391
# File 'app/concerns/controllers/heat_loss.rb', line 384

def doors_heat_loss_details
  set_doors_variables
  @door ||= @exterior_wall.doors.new
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#dupe_door_heat_loss_detailsObject



414
415
416
417
418
419
420
421
422
423
424
# File 'app/concerns/controllers/heat_loss.rb', line 414

def dupe_door_heat_loss_details
  set_doors_variables
  new_door = @door.dup
  unless new_door.save
    flash[:error] = "Door could not be duplicated: #{new_door.errors_to_s}"
  end
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#dupe_exterior_wall_heat_loss_detailsObject



298
299
300
301
302
303
304
305
306
307
308
# File 'app/concerns/controllers/heat_loss.rb', line 298

def dupe_exterior_wall_heat_loss_details
  set_exterior_walls_variables
  new_exterior_wall = @exterior_wall.dup
  unless new_exterior_wall.save
    flash[:error] = "Ext. Wall could not be duplicated: #{new_exterior_wall.errors_to_s}"
  end
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#dupe_skylight_heat_loss_detailsObject



243
244
245
246
247
248
249
250
251
252
253
# File 'app/concerns/controllers/heat_loss.rb', line 243

def dupe_skylight_heat_loss_details
  set_skylights_variables
  new_skylight = @skylight.dup
  unless new_skylight.save
    flash[:error] = "Skylight could not be duplicated: #{new_skylight.errors_to_s}"
  end
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#dupe_window_heat_loss_detailsObject



356
357
358
359
360
361
362
363
364
365
366
# File 'app/concerns/controllers/heat_loss.rb', line 356

def dupe_window_heat_loss_details
  set_windows_variables
  new_window = @window.dup
  unless new_window.save
    flash[:error] = "Window could not be duplicated: #{new_window.errors_to_s}"
  end
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#enter_heat_loss_infoObject



46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/concerns/controllers/heat_loss.rb', line 46

def enter_heat_loss_info
  if @room.present?
    if @room.floor_type && @room.floor_type.is_indoors?
      @room.enter_heat_loss_info = true
    else
      flash[:error] = 'Heat Loss Calculation is only for indoor rooms.'
      render action: :show
    end
  else
    redirect_to(projects_path_for(@room, request.subdomain), flash: { error: 'Sorry we cannot find this room' })
  end
end

#exterior_walls_heat_loss_detailsObject



269
270
271
272
273
274
275
276
# File 'app/concerns/controllers/heat_loss.rb', line 269

def exterior_walls_heat_loss_details
  set_exterior_walls_variables
  @exterior_wall ||= @room.exterior_walls.new
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#heat_loss_details_summaryObject



115
116
117
118
119
120
# File 'app/concerns/controllers/heat_loss.rb', line 115

def heat_loss_details_summary
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#projects_path_for(room, subdomain) ⇒ Object



519
520
521
522
523
524
525
526
# File 'app/concerns/controllers/heat_loss.rb', line 519

def projects_path_for(room, subdomain)
  case subdomain
  when 'crm'
    customer_opportunities_path(room.customer)
  else
    my_projects_path
  end
end

#remove_door_heat_loss_detailsObject



426
427
428
429
430
431
432
433
# File 'app/concerns/controllers/heat_loss.rb', line 426

def remove_door_heat_loss_details
  set_doors_variables
  @door.destroy
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#remove_exterior_wall_heat_loss_detailsObject



310
311
312
313
314
315
316
317
# File 'app/concerns/controllers/heat_loss.rb', line 310

def remove_exterior_wall_heat_loss_details
  set_exterior_walls_variables
  @exterior_wall.destroy
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#remove_skylight_heat_loss_detailsObject



255
256
257
258
259
260
261
262
# File 'app/concerns/controllers/heat_loss.rb', line 255

def remove_skylight_heat_loss_details
  set_skylights_variables
  @skylight.destroy
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#remove_window_heat_loss_detailsObject



368
369
370
371
372
373
374
375
# File 'app/concerns/controllers/heat_loss.rb', line 368

def remove_window_heat_loss_details
  set_windows_variables
  @window.destroy
  respond_to do |format|
    format.turbo_stream { render action: :heat_loss_details_summary }
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#room_heat_loss_detailsObject



122
123
124
125
126
127
128
# File 'app/concerns/controllers/heat_loss.rb', line 122

def room_heat_loss_details
  @room.insulation_surface ||= @room.area
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#set_ceiling_variablesObject



179
180
181
182
183
# File 'app/concerns/controllers/heat_loss.rb', line 179

def set_ceiling_variables
  @room.build_ceiling(area: @room.area, height: 9.0) unless @room.ceiling.present?
  @ceiling_insulation_type_options = CeilingInsulationType.options_for_select(@room.ceiling.ceiling_type_id, @room)
  @ceiling_insulation_type_id = @room.ceiling.ceiling_insulation_type_id if @room.ceiling.present?
end

#set_doors_variablesObject



377
378
379
380
381
382
# File 'app/concerns/controllers/heat_loss.rb', line 377

def set_doors_variables
  @exterior_wall = @room.exterior_walls.find(params[:exterior_wall_id])
  @exterior_wall_child_index = params[:exterior_wall_child_index]
  @door = @exterior_wall.doors.find(params[:door_id]) if params[:door_id].present?
  @child_index = params[:child_index]
end

#set_exterior_walls_variablesObject



264
265
266
267
# File 'app/concerns/controllers/heat_loss.rb', line 264

def set_exterior_walls_variables
  @exterior_wall = @room.exterior_walls.find_by(id: params[:exterior_wall_id]) if params[:exterior_wall_id].present?
  @child_index = params[:child_index]
end

#set_skylights_variablesObject



209
210
211
212
# File 'app/concerns/controllers/heat_loss.rb', line 209

def set_skylights_variables
  @skylight = @room.ceiling.skylights.find(params[:skylight_id]) if params[:skylight_id].present?
  @child_index = params[:child_index]
end

#set_under_floor_variablesObject



147
148
149
150
151
# File 'app/concerns/controllers/heat_loss.rb', line 147

def set_under_floor_variables
  @room.build_under_floor(area: @room.area, under_floor_type_id: @room.guess_under_floor_type_id) unless @room.under_floor.present?
  @under_floor_insulation_type_options = UnderFloorInsulationType.options_for_select(@room.under_floor.under_floor_type_id, @room)
  @under_floor_insulation_type_id = @room.under_floor.under_floor_insulation_type_id if @room.under_floor.present?
end

#set_windows_variablesObject



319
320
321
322
323
324
# File 'app/concerns/controllers/heat_loss.rb', line 319

def set_windows_variables
  @exterior_wall = @room.exterior_walls.find(params[:exterior_wall_id])
  @exterior_wall_child_index = params[:exterior_wall_child_index]
  @window = @exterior_wall.windows.find(params[:window_id]) if params[:window_id].present?
  @child_index = params[:child_index]
end

#show_heat_lossObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/concerns/controllers/heat_loss.rb', line 76

def show_heat_loss
  if @room.present?
    if @room.floor_type && @room.floor_type.is_indoors?
      res = @room.can_calculate_heat_loss?
      if res[:status] == :ok
        @heat_loss = @room.last_heat_loss.present? ? @room.last_heat_loss : @room.calculate_heat_loss
      end
      if @heat_loss && @heat_loss['hlc_by_object']&.any?
        respond_to do |format|
          format.html # show.html.erb
          format.pdf do
            result = Pdf::Document::HeatLossReport.call(@room)
            if params[:save_to_room].to_b
              @room.uploads.where(category: 'heat_loss_report').each(&:destroy) # Clean up before new ones are uploaded
              Upload.uploadify_from_data(file_name: result.file_name, data: result.pdf, category: 'heat_loss_report', resource: @room)
              flash[:info] = 'The Heat Loss report was saved and can be found in the attachments for this room'
              redirect_to room_configuration_path(@room, anchor: "attachments")
            else
              send_data result.pdf,
                filename: result.file_name,
                type: 'application/pdf',
                disposition: 'inline'
            end
          end
        end
      else
        flash[:error] = "Can't calculate heat loss yet: #{res[:errors].join(', ')}."
        @room.enter_heat_loss_info = true
        redirect_to enter_heat_loss_info_path_for_room(@room)
      end
    else
      flash[:error] = 'Heat Loss Calculation is only for indoor rooms.'
      render action: :show
    end
  else
    redirect_to(projects_path_for(@room, request.subdomain), flash: { error: 'Sorry we cannot find this room' })
  end
end

#show_heat_loss_path_for(room, subdomain) ⇒ Object



510
511
512
513
514
515
516
517
# File 'app/concerns/controllers/heat_loss.rb', line 510

def show_heat_loss_path_for(room, subdomain)
  case subdomain
  when 'crm'
    show_heat_loss_room_configuration_path(room)
  else
    show_heat_loss_my_room_path(room)
  end
end

#skylights_heat_loss_detailsObject



214
215
216
217
218
219
220
221
# File 'app/concerns/controllers/heat_loss.rb', line 214

def skylights_heat_loss_details
  set_skylights_variables
  @skylight ||= @room.ceiling.skylights.new
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#under_floor_heat_loss_detailsObject



153
154
155
156
157
158
159
# File 'app/concerns/controllers/heat_loss.rb', line 153

def under_floor_heat_loss_details
  set_under_floor_variables
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#update_ceiling_typeObject



445
446
447
448
449
450
451
452
453
# File 'app/concerns/controllers/heat_loss.rb', line 445

def update_ceiling_type
  ceiling_type_id = params[:room].try(:[], :ceiling_attributes).try(:[], :ceiling_type_id).try(:to_i)
  @ceiling_insulation_type_options = CeilingInsulationType.options_for_select(ceiling_type_id, @room)
  @ceiling_insulation_type_id = @room.ceiling.try(:ceiling_insulation_type_id)
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#update_door_typeObject



490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'app/concerns/controllers/heat_loss.rb', line 490

def update_door_type
  @door_child_index = params[:child_index].to_i unless params[:child_index].nil?
  @exterior_wall_child_index = params[:exterior_wall_child_index].to_i

  if door_attributes = params[:room].try(:[], :exterior_walls_attributes).try(:[], @exterior_wall_child_index.to_s).try(:[], :doors_attributes).try(:[], @door_child_index.to_s)
    @door_type_id = door_attributes[:door_type_id].to_i
    @door_core_type_id = door_attributes[:door_core_type_id].to_i
    @door_frame_type_id = door_attributes[:door_frame_type_id].to_i
  end
  if door = @room.exterior_walls.try(:[], @exterior_wall_child_index).try(:doors).try(:[], @door_child_index)
    @door_type_id ||= door.door_type_id
    @door_core_type_id ||= door.door_core_type_id
    @door_frame_type_id ||= door.door_frame_type_id
  end
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#update_exterior_wall_typeObject



455
456
457
458
459
460
461
462
463
464
465
466
# File 'app/concerns/controllers/heat_loss.rb', line 455

def update_exterior_wall_type
  @exterior_wall_child_index = params[:child_index].try(:to_i)
  @exterior_wall_type_id = params[:room].try(:[], :exterior_walls_attributes).try(:[], @exterior_wall_child_index.to_s).try(:[], :exterior_wall_type_id).try(:to_i)
  @exterior_wall_type_id ||= @room.try(:exterior_walls).try(:[], @exterior_wall_child_index).try(:exterior_wall_type_id)
  @exterior_wall_insulation_type_options = ExteriorWallInsulationType.options_for_select(@exterior_wall_type_id, @room)
  @exterior_wall_insulation_type_id = @room.exterior_walls.try(:[], @exterior_wall_child_index).try(:exterior_wall_insulation_type_id)

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#update_under_floor_typeObject



435
436
437
438
439
440
441
442
443
# File 'app/concerns/controllers/heat_loss.rb', line 435

def update_under_floor_type
  under_floor_type_id = params.dig(:room, :under_floor_attributes, :under_floor_type_id)&.to_i
  @under_floor_insulation_type_options = UnderFloorInsulationType.options_for_select(under_floor_type_id, @room)
  @under_floor_insulation_type_id = @room.try(:under_floor).try(:under_floor_insulation_type_id)
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#update_window_typeObject



468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'app/concerns/controllers/heat_loss.rb', line 468

def update_window_type
  @window_child_index = params[:child_index].try(:to_i)
  @exterior_wall_child_index = params[:exterior_wall_child_index].to_i

  if window_attributes = params[:room].try(:[], :exterior_walls_attributes).try(:[], @exterior_wall_child_index.to_s).try(:[], :windows_attributes).try(:[], @window_child_index.to_s)
    @window_type_id = window_attributes[:window_type_id].to_i
    @window_glass_type_id = window_attributes[:window_glass_type_id].to_i
    @window_frame_type_id = window_attributes[:window_frame_type_id].to_i
  end

  if window = @room.exterior_walls.try(:[], @exterior_wall_child_index).try(:windows).try(:[], @window_child_index)
    @window_type_id ||= window.window_type_id
    @window_glass_type_id ||= window.window_glass_type_id
    @window_frame_type_id ||= window.window_frame_type_id
  end

  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end

#windows_heat_loss_detailsObject



326
327
328
329
330
331
332
333
# File 'app/concerns/controllers/heat_loss.rb', line 326

def windows_heat_loss_details
  set_windows_variables
  @window ||= @exterior_wall.windows.new
  respond_to do |format|
    format.turbo_stream
    format.html { redirect_to enter_heat_loss_info_path_for_room(@room) }
  end
end