Class: AssetLink

Inherits:
ApplicationRecord show all
Includes:
Api::AssetLinkIo::Extensions, Uuid::Uuidable
Defined in:
app/models/asset_link.rb

Overview

AssetLink is powered by acts-as-dag Briefly, acts-as-dag attempts to implement a directed-acyclic-graph in a relational database. In order to optimize for retrieval it inserts an AssetLink record for EACH ancestor-descendant link. As a result, it is possible to retrieve ALL ancestors for a given plate in a single query. On the flip side, this makes insert operations more expensive as the graph grows.

As a result, try and avoid adding wells in to asset links, and link between Labware only.

The children,parents,ancestors,descendants methods are all Rails associations, and so can have further scopes applied to them

Examples:

Example methods

plate.children # => [<Plate: child of plate>,<Plate: child of plate>]
plate.parents # => [<Plate: parent of plate>]
plate.descendants # => [<Plate: child of plate>,<Plate: child of plate>,<Plate: grandchild of plate>]
plate.ancestors # => [<Plate: parent of plate>,<Plate: grandparent of plate>]

Retrieve all ancestors of a particular purpose

plate.ancestors.where(purpose_id: 4)

See Also:

Defined Under Namespace

Modules: Associations Classes: BuilderJob, Job

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Uuid::Uuidable

included, #unsaved_uuid!, #uuid

Methods included from Api::AssetLinkIo::Extensions

included

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Class Method Details

.create_edge(ancestor, descendant) ⇒ Boolean

Creates an edge between the ancestor and descendant nodes using save.

This method first attempts to find an existing link between the ancestor and descendant. If no link is found, it builds a new edge and saves it. If a link is found, it makes the link an edge and saves it.

This method is overridden to handle race conditions in finding an existing link and has_duplicates validation. It also assumes that there is a unique-together index on ancestor_id and descendant_id columns.

Parameters:

  • ancestor (Dag::Standard::EndPoint)

    The ancestor node.

  • descendant (Dag::Standard::EndPoint)

    The descendant node.

Returns:

  • (Boolean)

    Returns true if the edge is successfully created or already exists, false otherwise.

Raises:

  • (ActiveRecord::RecordNotUnique)

    Re-raises any exception if it is not a constraint violation that involves ancestor_id and descendant_id columns.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/asset_link.rb', line 102

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

.save_edge_or_handle_error(edge) ⇒ Boolean

Saves the edge between the ancestor and descendant nodes or handles errors.

Parameters:

  • edge (AssetLink)

    The edge object containing the errors.

Returns:

  • (Boolean)

    Returns true if the edge is successfully saved, nil if the error is unique validation or constraint violation, false if the error is another validation error.

Raises:

  • (ActiveRecord::RecordNotUnique)

    Re-raises an exception if the exception caught is not a unique constraint violation.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/models/asset_link.rb', line 129

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

.unique_validation_error?(edge) ⇒ Boolean

Checks if the validation error includes a specific message indicating a unique link already exists.

Parameters:

  • edge (AssetLink)

    The edge object containing the errors.

Returns:

  • (Boolean)

    Returns true if the errors include the message "Link already exists between these points", false otherwise.



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/models/asset_link.rb', line 149

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

.unique_violation_error?(edge, exception) ⇒ Boolean

Checks if the unique constraint violation involves the specified columns.

Parameters:

  • edge (AssetLink)

    The edge object containing the column names.

  • exception (ActiveRecord::RecordNotUnique)

    The exception raised due to the unique constraint violation.

Returns:

  • (Boolean)

    Returns true if the exception message includes both the ancestor and descendant column names, false otherwise.



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/models/asset_link.rb', line 160

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

Instance Method Details

#destroy!Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/asset_link.rb', line 34

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end