-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from sanger/develop
Merging develop to master
- Loading branch information
Showing
8 changed files
with
243 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.23.0 | ||
1.24.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddHuMFreCodeToSample < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :sample, :huMFre_code, :string, limit: 16 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
# This module is used to store the latest version of a record and manage versioned records. | ||
# It provides methods to check if a record is the latest and if any attributes have changed, | ||
# and to create or update records accordingly. This ensures an audit trail of updates by | ||
# creating new records when necessary. | ||
module SingularResourceVersionedTools | ||
extend ActiveSupport::Concern | ||
|
||
EXCLUDED_ATTRIBUTES = %w[id created_at updated_at last_updated recorded_at].freeze | ||
|
||
# Checks if the attributes of the given record have changed and if the given record is more recent. | ||
# If both conditions are met, returns true. Otherwise, returns false. | ||
# | ||
# @param other [ActiveRecord::Base] The record to compare with. | ||
# @return [Boolean] Returns true if the given record is the latest and has changed, false otherwise. | ||
# | ||
# Note: This method is used to ensure that only the most recent and changed records are processed, | ||
# maintaining an audit trail of updates. | ||
def latest?(other) | ||
attributes_changed?(other) && (other.last_updated > last_updated) | ||
end | ||
|
||
# Compares the attributes of the current record with the given record, excluding 'id', 'created_at', and 'updated_at'. | ||
# | ||
# @param other [ActiveRecord::Base] The record to compare with. | ||
# @return [Boolean] Returns true if the attributes have changed, false otherwise. | ||
def attributes_changed?(other) | ||
attributes.except(*EXCLUDED_ATTRIBUTES) != other.attributes.except(*EXCLUDED_ATTRIBUTES) | ||
end | ||
|
||
# This module is used to create or update a record | ||
module ClassMethods | ||
# Creates a record based on the given attributes. | ||
# If an existing record with the same base resource key is found, it checks if the new record is the latest | ||
# and if any attributes have changed. If both conditions are met, it creates a new record to maintain an audit trail. | ||
# | ||
# @param attributes [Hash] The attributes of the record to create or update which is an instance of Aliquot::JsonHandler. | ||
# @return [ActiveRecord::Base] Returns the created record. | ||
def create_or_update(attributes) | ||
new_record = new(attributes.to_hash) | ||
|
||
existing_record = for_lims(attributes.id_lims).with_id(attributes[base_resource_key]).order(last_updated: :desc) | ||
.first | ||
return unless existing_record.nil? || existing_record.latest?(new_record) | ||
|
||
create!(attributes.to_hash) | ||
end | ||
private :create_or_update | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.