Improve model validation
This commit is contained in:
parent
e6d2465d97
commit
5646f3f551
|
@ -23,7 +23,8 @@ class PrometheusMetric < ActiveRecord::Base
|
|||
validates :y_label, presence: true
|
||||
validates :unit, presence: true
|
||||
|
||||
validate :require_project
|
||||
validates :project, presence: true, unless: :common?
|
||||
validates :project, absence: true, if: :common?
|
||||
|
||||
scope :common, -> { where(common: true) }
|
||||
|
||||
|
@ -85,14 +86,4 @@ class PrometheusMetric < ActiveRecord::Base
|
|||
}
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def require_project
|
||||
if project
|
||||
errors.add(:project, "cannot be set if this is common metric") if common?
|
||||
else
|
||||
errors.add(:project, "has to be set when this is project-specific metric") unless common?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue