Commit a9e49194 authored by Kamil Trzciński's avatar Kamil Trzciński

Improve model validation

parent f28eeb9f
......@@ -25,7 +25,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) }
......@@ -87,14 +88,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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment