Class: RecordLoader::SubmissionTemplateLoader

Inherits:
ApplicationRecordLoader show all
Defined in:
lib/record_loader/submission_template_loader.rb

Overview

Creates the specified submission templates if they are not present

Instance Method Summary collapse

Methods inherited from ApplicationRecordLoader

#wip_list

Instance Method Details

#create_or_update!(name, options) ⇒ Object



12
13
14
15
16
17
# File 'lib/record_loader/submission_template_loader.rb', line 12

def create_or_update!(name, options)
  derived_options = generate_derived_options(options['related_records'])
  final_options = options.except('related_records').merge(derived_options)

  SubmissionTemplate.create_with(final_options).find_or_create_by!(name:)
end

#find_project(name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/record_loader/submission_template_loader.rb', line 44

def find_project(name)
  if Rails.env.production?
    Project.find_by!(name:)
  else
    # In development mode or UAT we don't care so much
    Project.find_by(name:) || UatActions::StaticRecords.project
  end
end

#find_study(name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/record_loader/submission_template_loader.rb', line 53

def find_study(name)
  if Rails.env.production?
    Study.find_by!(name:)
  else
    # In development mode or UAT we don't care so much
    Study.find_by(name:) || UatActions::StaticRecords.study
  end
end

#generate_derived_options(related_records) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/record_loader/submission_template_loader.rb', line 19

def generate_derived_options(related_records)
  {
    product_line: ProductLine.find_or_create_by!(name: related_records['product_line_name']),
    product_catalogue: ProductCatalogue.find_by!(name: related_records['product_catalogue_name']),
    submission_parameters: submission_parameters(related_records)
  }
end

#sort_request_type_ids(request_type_keys) ⇒ Object



38
39
40
41
42
# File 'lib/record_loader/submission_template_loader.rb', line 38

def sort_request_type_ids(request_type_keys)
  request_types = RequestType.where(key: request_type_keys).sort_by { |record| request_type_keys.index(record.key) }

  request_types.map(&:id)
end

#submission_parameters(related_records) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/record_loader/submission_template_loader.rb', line 27

def submission_parameters(related_records)
  params = {}
  params[:request_type_ids_list] = sort_request_type_ids(related_records['request_type_keys']) if related_records[
    'request_type_keys'
  ]

  params[:project_id] = find_project(related_records['project_name']).id if related_records['project_name']
  params[:study_id] = find_study(related_records['study_name']).id if related_records['study_name']
  params
end