Rename validation

This commit is contained in:
Urban Rotnik 2019-10-22 09:03:33 +02:00
parent d5716c4260
commit b8bf7bbd9b
3 changed files with 5 additions and 7 deletions

View file

@ -17,7 +17,7 @@ class MyModule < ApplicationRecord
validates :experiment, presence: true validates :experiment, presence: true
validates :my_module_group, presence: true, validates :my_module_group, presence: true,
if: proc { |mm| !mm.my_module_group_id.nil? } if: proc { |mm| !mm.my_module_group_id.nil? }
validate :coordinates, if: proc { |mm| !mm.archived? } validate :coordinates_uniqueness_check, if: :active?
belongs_to :created_by, belongs_to :created_by,
foreign_key: 'created_by_id', foreign_key: 'created_by_id',
@ -502,10 +502,8 @@ class MyModule < ApplicationRecord
protocols << Protocol.new_blank_for_module(self) protocols << Protocol.new_blank_for_module(self)
end end
def coordinates def coordinates_uniqueness_check
my_modules_on_same_position = if experiment && experiment.my_modules.active.where(x: x, y: y).where.not(id: id).any?
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')) errors.add(:position, I18n.t('activerecord.errors.models.my_module.attributes.position.not_unique'))
end end
end end

View file

@ -9,7 +9,7 @@ namespace :my_modules do
query = MyModule.select('COUNT(*) as duplicates', :x, :y, :experiment_id) query = MyModule.select('COUNT(*) as duplicates', :x, :y, :experiment_id)
.where(archived: false) .where(archived: false)
.group(:x, :y, :experiment_id) .group(:x, :y, :experiment_id)
.having('COUNT(*)>1') .having('COUNT(my_modules.id)>1')
Rails.logger.info '*********************************************************************************' Rails.logger.info '*********************************************************************************'
Rails.logger.info "You have to relocate #{query.sum { |a| a.duplicates - 1 }} tasks" Rails.logger.info "You have to relocate #{query.sum { |a| a.duplicates - 1 }} tasks"

View file

@ -34,7 +34,7 @@ describe 'my_modules:fix_positions' do
my_module.valid? my_module.valid?
expect(my_module.errors.messages[:position]) expect(my_module.errors.messages[:position])
.to(eq ['and Y position has already been taken by another task in the experiment.']) .to(eq ['X and Y position has already been taken by another task in the experiment.'])
end end
end end
end end