Project

General

Profile

Download (819 Bytes) Statistics
| Branch: | Tag: | Revision:

foreman-docker / app / models / concerns / foreman_docker / parameter_validators.rb @ 2b80fdd9

1
module ForemanDocker
2
  module ParameterValidators
3
    extend ActiveSupport::Concern
4

    
5
    included do
6
      validate :validate_unique_parameter_keys
7
    end
8

    
9
    def validate_unique_parameter_keys
10
      parameters_symbol = [:environment_variables, :exposed_ports, :dns]
11
      parameters_symbol.each do |param_symbol|
12
        keys  = []
13
        errors = false
14

    
15
        self.public_send(param_symbol).each do |param|
16
          errors = duplicate_key?(keys, param)
17
        end
18

    
19
        self.errors[param_symbol] = _('Please ensure the following parameters are unique') if errors
20
      end
21
    end
22

    
23
    def duplicate_key?(keys, param)
24
      if keys.include?(param.key)
25
        param.errors[:key] = _('has already been taken')
26
        return true
27
      else
28
        keys << param.key
29
      end
30

    
31
      false
32
    end
33
  end
34
end