From b8bf7bbd9b35d8bf494b2dfb3d485dbdefbe7583 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 22 Oct 2019 09:03:33 +0200 Subject: [PATCH] Rename validation --- app/models/my_module.rb | 8 +++----- lib/tasks/my_modules.rake | 2 +- spec/lib/tasks/my_modules_spec.rb | 2 +- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/models/my_module.rb b/app/models/my_module.rb index 96f7c3a3c..ff451892e 100644 --- a/app/models/my_module.rb +++ b/app/models/my_module.rb @@ -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? } - validate :coordinates, if: proc { |mm| !mm.archived? } + validate :coordinates_uniqueness_check, if: :active? belongs_to :created_by, foreign_key: 'created_by_id', @@ -502,10 +502,8 @@ class MyModule < ApplicationRecord 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? + def coordinates_uniqueness_check + if experiment && experiment.my_modules.active.where(x: x, y: y).where.not(id: id).any? errors.add(:position, I18n.t('activerecord.errors.models.my_module.attributes.position.not_unique')) end end diff --git a/lib/tasks/my_modules.rake b/lib/tasks/my_modules.rake index 615f6cf23..b60e7e5d0 100644 --- a/lib/tasks/my_modules.rake +++ b/lib/tasks/my_modules.rake @@ -9,7 +9,7 @@ namespace :my_modules do query = MyModule.select('COUNT(*) as duplicates', :x, :y, :experiment_id) .where(archived: false) .group(:x, :y, :experiment_id) - .having('COUNT(*)>1') + .having('COUNT(my_modules.id)>1') Rails.logger.info '*********************************************************************************' Rails.logger.info "You have to relocate #{query.sum { |a| a.duplicates - 1 }} tasks" diff --git a/spec/lib/tasks/my_modules_spec.rb b/spec/lib/tasks/my_modules_spec.rb index afb84ad6b..41a3b6891 100644 --- a/spec/lib/tasks/my_modules_spec.rb +++ b/spec/lib/tasks/my_modules_spec.rb @@ -34,7 +34,7 @@ describe 'my_modules:fix_positions' do my_module.valid? 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