Commit 3c82cc5b4dea43024fd8d2946a53719500a5b5f6
Committed by
Rafael Manzo
1 parent
c7a946fa
Exists in
colab
and in
4 other branches
Created validator for compound metric configurations name and script fields
Signed off by: Heitor Reis <marcheing@gmail.com>
Showing
2 changed files
with
12 additions
and
0 deletions
Show diff stats
app/models/metric_configuration.rb
| 1 | require "validators/code_uniqueness_validator.rb" | 1 | require "validators/code_uniqueness_validator.rb" |
| 2 | +require "validators/name_script_presence_validator.rb" | ||
| 2 | 3 | ||
| 3 | class MetricConfiguration < KalibroGatekeeperClient::Entities::MetricConfiguration | 4 | class MetricConfiguration < KalibroGatekeeperClient::Entities::MetricConfiguration |
| 4 | include KalibroRecord | 5 | include KalibroRecord |
| @@ -8,6 +9,7 @@ class MetricConfiguration < KalibroGatekeeperClient::Entities::MetricConfigurati | @@ -8,6 +9,7 @@ class MetricConfiguration < KalibroGatekeeperClient::Entities::MetricConfigurati | ||
| 8 | validates :code, presence: true, code_uniqueness: true | 9 | validates :code, presence: true, code_uniqueness: true |
| 9 | validates :weight, presence: true, numericality: { greater_than: 0 } | 10 | validates :weight, presence: true, numericality: { greater_than: 0 } |
| 10 | validates :aggregation_form, presence: true, unless: "metric.compound == true" | 11 | validates :aggregation_form, presence: true, unless: "metric.compound == true" |
| 12 | + validates_with NameScriptPresenceValidator, unless: "metric.compound == false" | ||
| 11 | 13 | ||
| 12 | def mezuro_ranges | 14 | def mezuro_ranges |
| 13 | MezuroRange.ranges_of self.id | 15 | MezuroRange.ranges_of self.id |
| @@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
| 1 | +class NameScriptPresenceValidator < ActiveModel::Validator | ||
| 2 | + def validate(record) | ||
| 3 | + if record.metric.name.empty? | ||
| 4 | + record.errors[:name] << "can't be blank" | ||
| 5 | + end | ||
| 6 | + if record.metric.script.empty? | ||
| 7 | + record.errors[:script] << "can't be blank" | ||
| 8 | + end | ||
| 9 | + end | ||
| 10 | +end |