Class: SubmissionTemplate

Inherits:
ApplicationRecord show all
Includes:
Uuid::Uuidable
Defined in:
app/models/submission_template.rb

Overview

It associates a name to a pre-filled submission (subclass) and a serialized set of attributes We could have use a Prototype Factory , and so just associate a name to existing submission but that doesn't work because the submission prototype doesn't pass the validation stage. Anyway that's basically a prototype factory

Constant Summary collapse

LATEST_VERSION =
-1
SUPERCEDED_BY_UNKNOWN_TEMPLATE =
-2

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Class Method Details

.grouped_by_product_linesObject


30
31
32
# File 'app/models/submission_template.rb', line 30

def self.grouped_by_product_lines
  visible.include_product_line.group_by { |t| t.product_line.try(:name) || 'General' }
end

Instance Method Details

#create_order!(attributes) ⇒ Object


54
55
56
57
58
59
# File 'app/models/submission_template.rb', line 54

def create_order!(attributes)
  new_order(attributes).tap do |order|
    yield(order) if block_given?
    order.save!
  end
end

#create_with_submission!(attributes) ⇒ Object


61
62
63
64
65
# File 'app/models/submission_template.rb', line 61

def create_with_submission!(attributes)
  create_order!(attributes) do |order|
    order.create_submission(user_id: order.user_id)
  end
end

#input_asset_typeObject


91
92
93
# File 'app/models/submission_template.rb', line 91

def input_asset_type
  sorted_request_types.first.asset_type
end

#input_field_infosObject


83
84
85
# File 'app/models/submission_template.rb', line 83

def input_field_infos
  FieldInfo.for_request_types(request_types)
end

#input_plate_purposesObject


95
96
97
# File 'app/models/submission_template.rb', line 95

def input_plate_purposes
  sorted_request_types.first.acceptable_plate_purposes
end

#new_order(params = {}) ⇒ Object

create a new submission of the good subclass and with pre-set attributes


68
69
70
71
72
73
74
75
76
77
# File 'app/models/submission_template.rb', line 68

def new_order(params = {})
  duped_params = safely_duplicate(params)
  # NOTE: Stringifying request_option keys here is NOT a good idea as it affects multipliers
  attributes = submission_attributes.with_indifferent_access.deep_merge(duped_params)

  submission_class.new(attributes).tap do |order|
    order.template_name = name
    order.product = product_for(attributes)
  end
end

#sequencing?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'app/models/submission_template.rb', line 87

def sequencing?
  request_types.any?(&:sequencing)
end

#submission_classObject


79
80
81
# File 'app/models/submission_template.rb', line 79

def submission_class
  submission_class_name.constantize
end

#supercedeObject


42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/submission_template.rb', line 42

def supercede
  ActiveRecord::Base.transaction do
    dup.tap do |cloned|
      yield(cloned) if block_given?
      name, cloned.name = cloned.name, "Superceding #{cloned.name}"
      cloned.save!
      update!(superceded_by_id: cloned.id, superceded_at: Time.now)
      cloned.update!(name: name)
    end
  end
end

#superceded_by_unknown!Object


38
39
40
# File 'app/models/submission_template.rb', line 38

def superceded_by_unknown!
  self.superceded_by_id = SUPERCEDED_BY_UNKNOWN_TEMPLATE
end

#visibleObject


34
35
36
# File 'app/models/submission_template.rb', line 34

def visible
  superceded_by_id == LATEST_VERSION
end