From 9a5ba5c67444d9b71597dd8c43955a055a047100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matija=20=C4=8Cupi=C4=87?= Date: Tue, 13 Feb 2018 19:46:02 +0100 Subject: [PATCH] Add more information in variable_duplicates validator error message --- app/validators/variable_duplicates_validator.rb | 4 +++- spec/support/features/variable_list_shared_examples.rb | 2 +- spec/validators/variable_duplicates_validator_spec.rb | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/app/validators/variable_duplicates_validator.rb b/app/validators/variable_duplicates_validator.rb index 7887b217829..ae9ebdf28cf 100644 --- a/app/validators/variable_duplicates_validator.rb +++ b/app/validators/variable_duplicates_validator.rb @@ -20,7 +20,9 @@ class VariableDuplicatesValidator < ActiveModel::EachValidator def validate_duplicates(record, attribute, values) duplicates = values.reject(&:marked_for_destruction?).group_by(&:key).select { |_, v| v.many? }.map(&:first) if duplicates.any? - record.errors.add(attribute, "Duplicate variables: #{duplicates.join(", ")}") + error_message = "has duplicate variables (#{duplicates.join(", ")})" + error_message += " in #{options[:scope]} scope" if options[:scope] + record.errors.add(attribute, error_message) end end end diff --git a/spec/support/features/variable_list_shared_examples.rb b/spec/support/features/variable_list_shared_examples.rb index 4315bf5d037..b7d896c9409 100644 --- a/spec/support/features/variable_list_shared_examples.rb +++ b/spec/support/features/variable_list_shared_examples.rb @@ -263,7 +263,7 @@ shared_examples 'variable list' do # We check the first row because it re-sorts to alphabetical order on refresh page.within('.js-ci-variable-list-section') do - expect(find('.js-ci-variable-error-box')).to have_content('Validation failed Variables Duplicate variables: samekey') + expect(find('.js-ci-variable-error-box')).to have_content(/Validation failed Variables has duplicate variables \(.+\)/) end end end diff --git a/spec/validators/variable_duplicates_validator_spec.rb b/spec/validators/variable_duplicates_validator_spec.rb index be80b901b88..0b71a67f94d 100644 --- a/spec/validators/variable_duplicates_validator_spec.rb +++ b/spec/validators/variable_duplicates_validator_spec.rb @@ -30,7 +30,7 @@ describe VariableDuplicatesValidator do it 'has a duplicate key error' do subject - expect(project.errors[:variables]).to include("Duplicate variables: #{project.variables.last.key}") + expect(project.errors).to have_key(:variables) end end end @@ -59,7 +59,7 @@ describe VariableDuplicatesValidator do it 'has a duplicate key error' do subject - expect(project.errors[:variables]).to include("Duplicate variables: #{project.variables.last.key}") + expect(project.errors).to have_key(:variables) end end end