Skip to content

Commit

Permalink
feat(smrtlink): adds custom validator for use_adaptive_loading smrt l…
Browse files Browse the repository at this point in the history
…ink option
  • Loading branch information
BenTopping committed Jan 9, 2025
1 parent b411d5a commit 987009d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
14 changes: 11 additions & 3 deletions app/validators/smrt_link_options_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# - Or you need to create your own validator as per
# https://api.rubyonrails.org/classes/ActiveModel/Validator.html
class SmrtLinkOptionsValidator < ActiveModel::Validator
def validate(record)
def validate(record) # rubocop:disable Metrics/MethodLength
# If the version is not present no point validating
return if record&.run&.smrt_link_version.blank?

Expand All @@ -38,7 +38,15 @@ def validate(record)
# @example
# key = required
# validator => ActiveModel::Validations::RequiredValidator
validator = "ActiveModel::Validations::#{key.capitalize}Validator".constantize

# Check if there is a custom validator first
validator_class_name = "#{key.camelize}Validator"
validator = if Object.const_defined?(validator_class_name)
validator_class_name.constantize
else
# If no custom validator then use an active model one
"ActiveModel::Validations::#{key.camelize}Validator".constantize
end

# We then need to create a new instance of the validator
# and pass the options along with the attribute name which is the key
Expand All @@ -52,5 +60,5 @@ def validate(record)
instance.validate(record)
end
end
end
end # rubocop:enable Metrics/MethodLength
end
11 changes: 11 additions & 0 deletions app/validators/use_adaptive_loading_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

# Validator for ensuring use_adaptive_loading is consistent across all wells
class UseAdaptiveLoadingValidator < ActiveModel::Validator
# @param [ActiveRecord::Base] record
def validate(record)
return if record.use_adaptive_loading == record.run.wells.first.use_adaptive_loading

record.errors.add(:base, "well #{record.position} has a differing 'Use Adaptive Loading' value")
end
end
1 change: 1 addition & 0 deletions config/pacbio_smrt_link_versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ default: &default
presence: {}
inclusion:
in: *true_false
use_adaptive_loading: {}
data_type: list
select_options: *select_true_false
versions:
Expand Down
1 change: 0 additions & 1 deletion spec/exchanges/run_csv/pacbio_sample_sheet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
before do
# Create a default smrt link version
create(:pacbio_smrt_link_version, name: 'v13_revio', default: true)
create(:pacbio_smrt_link_version, name: 'v25_1_revio')
end

describe '#payload' do
Expand Down
2 changes: 1 addition & 1 deletion spec/validators/smrt_link_options_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
end
end

it 'onlies mark the record as invalid for the correct version' do
it 'only marks the record as invalid for the correct version' do
run = create(:pacbio_sequel_run, smrt_link_version: versions.first)
well = build(:pacbio_well, plate: run.plates.first, movie_time: nil)
described_class.new.validate(well)
Expand Down
27 changes: 27 additions & 0 deletions spec/validators/use_adaptive_loading_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe UseAdaptiveLoadingValidator do
before do
create(:pacbio_smrt_link_version, name: 'v13_1_revio', default: true)
end

let(:run) { create(:pacbio_revio_run) }
let(:validator) { described_class.new(run:, exclude_marked_for_destruction: true) }

it 'is valid when all wells have the same use_adaptive_loading value' do
# Factory defaults to false for use_adaptive_loading for all wells
well = run.wells.first
validator.validate(well)
expect(well.errors).to be_empty
end

it 'is invalid when all wells do not have the same use_adaptive_loading value' do
well = run.wells.last
well.use_adaptive_loading = true

validator.validate(well)
expect(well.errors[:base]).to include("well #{run.wells.last.position} has a differing 'Use Adaptive Loading' value")
end
end

0 comments on commit 987009d

Please sign in to comment.