Skip to content

Commit

Permalink
feat: added wells to versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Herrmann committed Sep 25, 2024
1 parent 1dbe9bc commit 6efcb38
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 3 deletions.
2 changes: 2 additions & 0 deletions app/models/well.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#

class Well < ApplicationRecord
has_logidze
acts_as_paranoid
belongs_to :wellplate
belongs_to :sample, optional: true
Expand All @@ -42,6 +43,7 @@ def readouts
# translates well position within wellplate: X=2 Y=3 -> C2
def alphanumeric_position
return 'n/a' if position_x.nil? || position_y.nil?

row = ('A'..'ZZ').to_a[position_y - 1]
"#{row}#{position_x}"
end
Expand Down
20 changes: 19 additions & 1 deletion app/services/versioning/fetchers/wellplate_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

# rubocop:disable Metrics/AbcSize

module Versioning
module Fetchers
class WellplateFetcher
Expand All @@ -14,6 +16,19 @@ def self.call(**args)
def call
versions = Versioning::Serializers::WellplateSerializer.call(wellplate)

wellplate.wells.with_log_data.each do |well|
versions += if well.position_y < 27
Versioning::Serializers::WellSerializer.call(
well, ["Well: (#{(well.position_y + 64).chr}#{well.position_x})"]
)
else
Versioning::Serializers::WellSerializer.call(
well, ["Well: (#{((well.position_y / 26) + 64).chr}" \
"#{((well.position_y % 26) + 64).chr}#{well.position_x})"]
)
end
end

analyses_container = wellplate.container.children.where(container_type: :analyses).first
analyses_container.children.where(container_type: :analysis).with_deleted.with_log_data.each do |analysis|
versions += Versioning::Serializers::ContainerSerializer.call(analysis, ["Analysis: #{analysis.name}"])
Expand All @@ -25,7 +40,8 @@ def call

versions += dataset.attachments.with_log_data.flat_map do |attachment|
Versioning::Serializers::AttachmentSerializer.call(attachment,
["Analysis: #{analysis.name}", "Dataset: #{dataset.name}",
["Analysis: #{analysis.name}",
"Dataset: #{dataset.name}",
"Attachment: #{attachment.filename}"])
end
end
Expand All @@ -36,3 +52,5 @@ def call
end
end
end

# rubocop:enable Metrics/AbcSize
2 changes: 2 additions & 0 deletions app/services/versioning/reverter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ def call
Versioning::Reverters::ScreenReverter.call(change)
when 'Wellplate'
Versioning::Reverters::WellplateReverter.call(change)
when 'Well'
Versioning::Reverters::WellReverter.call(change)
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions app/services/versioning/reverters/well_reverter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class Versioning::Reverters::WellReverter < Versioning::Reverters::BaseReverter
def self.scope
Well.with_deleted
end
end
4 changes: 4 additions & 0 deletions app/services/versioning/serializers/base_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def jsonb_formatter(*attributes)
->(key, value) { default_formatter.call(key, value)&.dig(*attributes) }
end

def array_formatter
->(key, value) { default_formatter.call(key, value)&.join("\n") }
end

def non_formatter
->(_key, value) { value }
end
Expand Down
29 changes: 29 additions & 0 deletions app/services/versioning/serializers/well_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module Versioning
module Serializers
class WellSerializer < Versioning::Serializers::BaseSerializer
def self.call(record, name)
new(record: record, name: name).call
end

def field_definitions
{
color_code: {
label: 'Color Code',
revert: %i[color_code],
},
sample_id: {
label: 'Sample ID',
revert: %i[sample_id],
},
readouts: {
label: 'Readouts',
revert: %i[readouts],
formatter: array_formatter,
},
}.with_indifferent_access
end
end
end
end
5 changes: 5 additions & 0 deletions app/services/versioning/serializers/wellplate_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def field_definitions
label: 'Height',
revert: %i[heigth],
},
readout_titles: {
label: 'Readout Title',
revert: %i[readout_titles],
formatter: array_formatter,
},
}.with_indifferent_access
end
end
Expand Down
13 changes: 12 additions & 1 deletion db/migrate/20240920154515_add_logidze_to_wellplates.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddLogidzeToWellplates < ActiveRecord::Migration[6.1]
def change
add_column :wellplates, :log_data, :jsonb
Expand All @@ -8,10 +10,19 @@ def change
end

dir.down do
execute <<~SQL
execute <<~SQL.squish
DROP TRIGGER IF EXISTS "logidze_on_wellplates" on "wellplates";
SQL
end
end

reversible do |dir|
dir.up do
execute <<~SQL.squish
UPDATE wellplates as t
SET log_data = logidze_snapshot(to_jsonb(t), 'updated_at');
SQL
end
end
end
end
28 changes: 28 additions & 0 deletions db/migrate/20240920155000_add_logidze_to_wells.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

class AddLogidzeToWells < ActiveRecord::Migration[6.1]
def change
add_column :wells, :log_data, :jsonb

reversible do |dir|
dir.up do
create_trigger :logidze_on_wells, on: :wells
end

dir.down do
execute <<~SQL.squish
DROP TRIGGER IF EXISTS "logidze_on_wells" on "wells";
SQL
end
end

reversible do |dir|
dir.up do
execute <<~SQL.squish
UPDATE wells as t
SET log_data = logidze_snapshot(to_jsonb(t), 'updated_at');
SQL
end
end
end
end
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_09_20_154515) do
ActiveRecord::Schema.define(version: 2024_09_20_155000) do

# These are extensions that must be enabled in order to support this database
enable_extension "hstore"
Expand Down Expand Up @@ -1494,6 +1494,7 @@
t.jsonb "readouts", default: [{"unit"=>"", "value"=>""}]
t.string "label", default: "Molecular structure", null: false
t.string "color_code"
t.jsonb "log_data"
t.index ["deleted_at"], name: "index_wells_on_deleted_at"
t.index ["sample_id"], name: "index_wells_on_sample_id"
t.index ["wellplate_id"], name: "index_wells_on_wellplate_id"
Expand Down Expand Up @@ -2136,6 +2137,9 @@
create_trigger :logidze_on_wellplates, sql_definition: <<-SQL
CREATE TRIGGER logidze_on_wellplates BEFORE INSERT OR UPDATE ON public.wellplates FOR EACH ROW WHEN ((COALESCE(current_setting('logidze.disabled'::text, true), ''::text) <> 'on'::text)) EXECUTE FUNCTION logidze_logger('null', 'updated_at')
SQL
create_trigger :logidze_on_wells, sql_definition: <<-SQL
CREATE TRIGGER logidze_on_wells BEFORE INSERT OR UPDATE ON public.wells FOR EACH ROW WHEN ((COALESCE(current_setting('logidze.disabled'::text, true), ''::text) <> 'on'::text)) EXECUTE FUNCTION logidze_logger('null', 'updated_at')
SQL

create_view "v_samples_collections", sql_definition: <<-SQL
SELECT cols.id AS cols_id,
Expand Down
6 changes: 6 additions & 0 deletions db/triggers/logidze_on_wells_v01.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TRIGGER "logidze_on_wells"
BEFORE UPDATE OR INSERT ON "wells" FOR EACH ROW
WHEN (coalesce(current_setting('logidze.disabled', true), '') <> 'on')
-- Parameters: history_size_limit (integer), timestamp_column (text), filtered_columns (text[]),
-- include_columns (boolean), debounce_time_ms (integer)
EXECUTE PROCEDURE logidze_logger(null, 'updated_at');

0 comments on commit 6efcb38

Please sign in to comment.