From 1acaf75d9f3afea468bf4116602d9f27ac71dc2d Mon Sep 17 00:00:00 2001 From: Shinya Maeda Date: Thu, 6 Jul 2017 00:45:26 +0900 Subject: Implement ayufan validator --- app/models/ci/pipeline_schedule.rb | 6 +--- .../uniqueness_of_in_memory_validator.rb | 37 ---------------------- .../projects/pipeline_schedules_controller_spec.rb | 4 +-- 3 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 app/validators/uniqueness_of_in_memory_validator.rb diff --git a/app/models/ci/pipeline_schedule.rb b/app/models/ci/pipeline_schedule.rb index ad9f8b89924..e4ae1b35f66 100644 --- a/app/models/ci/pipeline_schedule.rb +++ b/app/models/ci/pipeline_schedule.rb @@ -15,11 +15,7 @@ module Ci validates :cron_timezone, cron_timezone: true, presence: { unless: :importing? } validates :ref, presence: { unless: :importing? } validates :description, presence: true - validates :variables, uniqueness_of_in_memory: { - :collection => :variables, - :attrs => [:pipeline_schedule_id, :key], - :message => ['variables.key', 'keys are duplicated'] - } + validates :variables, variable_duplicates: true before_save :set_next_run_at diff --git a/app/validators/uniqueness_of_in_memory_validator.rb b/app/validators/uniqueness_of_in_memory_validator.rb deleted file mode 100644 index 84e88b2eb76..00000000000 --- a/app/validators/uniqueness_of_in_memory_validator.rb +++ /dev/null @@ -1,37 +0,0 @@ -# UniquenessOfInMemoryValidator -# -# This validtor is designed for especially the following condition -# - Use `accepts_nested_attributes_for :xxx` in a parent model -# - Use `validates :xxx, uniqueness: { scope: :xxx_id }` in a child model -# -# Inspired by https://stackoverflow.com/a/2883129/2522666 -module ActiveRecord - class Base - # Validate that the the objects in +collection+ are unique - # when compared against all their non-blank +attrs+. If not - # add +message+ to the base errors. - def validate_uniqueness_of_in_memory(collection, attrs, message) - hashes = collection.inject({}) do |hash, record| - key = attrs.map { |a| record.send(a).to_s }.join - if key.blank? || record.marked_for_destruction? - key = record.object_id - end - hash[key] = record unless hash[key] - hash - end - - if collection.length > hashes.length - self.errors.add(*message) - end - end - end -end - -class UniquenessOfInMemoryValidator < ActiveModel::Validator - def validate(record) - record.validate_uniqueness_of_in_memory( - record.public_send(options[:collection]), - options[:attrs], - options[:message]) - end -end diff --git a/spec/controllers/projects/pipeline_schedules_controller_spec.rb b/spec/controllers/projects/pipeline_schedules_controller_spec.rb index 3b695953f26..5dff41df9c0 100644 --- a/spec/controllers/projects/pipeline_schedules_controller_spec.rb +++ b/spec/controllers/projects/pipeline_schedules_controller_spec.rb @@ -171,7 +171,7 @@ describe Projects::PipelineSchedulesController do .to change { Ci::PipelineSchedule.count }.by(0) .and change { Ci::PipelineScheduleVariable.count }.by(0) - expect(assigns(:schedule).errors['variables.key']).not_to be_empty + expect(assigns(:schedule).errors['variables']).not_to be_empty end end end @@ -269,7 +269,7 @@ describe Projects::PipelineSchedulesController do it 'returns an error that variables are duplciated' do go - expect(assigns(:schedule).errors['variables.key']).not_to be_empty + expect(assigns(:schedule).errors['variables']).not_to be_empty end end end -- cgit v1.2.1