Update x, y, experiment unqiue validation

[SCI-3887]
This commit is contained in:
Urban Rotnik 2019-09-26 08:44:34 +02:00
parent 06ab087033
commit 43e3763ec8
3 changed files with 25 additions and 10 deletions

View file

@ -17,7 +17,7 @@ class MyModule < ApplicationRecord
validates :experiment, presence: true
validates :my_module_group, presence: true,
if: proc { |mm| !mm.my_module_group_id.nil? }
validates_uniqueness_of :x, scope: %i(y experiment_id), message: :not_unique
validate :coordinates, if: proc { |mm| !mm.archived? }
belongs_to :created_by,
foreign_key: 'created_by_id',
@ -501,4 +501,12 @@ class MyModule < ApplicationRecord
def create_blank_protocol
protocols << Protocol.new_blank_for_module(self)
end
def coordinates
my_modules_on_same_position =
MyModule.where(experiment_id: experiment_id, x: x, y: y, archived: false).where.not(id: id)
if my_modules_on_same_position.any?
errors.add(:position, I18n.t('activerecord.errors.models.my_module.attributes.position.not_unique'))
end
end
end

View file

@ -83,15 +83,14 @@ en:
same_team: "Inventory can't be shared to the same team as it belongs to"
my_module:
attributes:
x:
not_unique: "and Y position has already been taken by another task in the experiment."
position:
not_unique: "X and Y position has already been taken by another task in the experiment."
asset:
attributes:
file:
too_big: "is too big"
helpers:
label:
team:

View file

@ -79,15 +79,23 @@ describe MyModule, type: :model do
end
end
describe '#x, #y scoped to experiment' do
describe '#x, #y scoped to experiment for active modules' do
it { is_expected.to validate_presence_of :x }
it { is_expected.to validate_presence_of :y }
it do
expect(my_module)
.to(validate_uniqueness_of(:x)
.scoped_to(%i(y experiment_id))
.with_message('and Y position has already been taken by another task in the experiment.'))
it 'should be invalid for same x, y, and experiment' do
my_module.save
new_my_module = my_module.dup
expect(new_my_module).not_to be_valid
end
it 'should be valid when module with same x, y and expriment is archived' do
my_module.save
new_my_module = my_module.dup
my_module.update_column(:archived, true)
expect(new_my_module).to be_valid
end
end