mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 09:27:19 +02:00
Refactor object validator
This commit is contained in:
parent
7530d6ecab
commit
8b7ac451d0
1 changed files with 3 additions and 1 deletions
|
@ -1,7 +1,9 @@
|
||||||
class ObjectCountValidator < ActiveModel::EachValidator
|
class ObjectCountValidator < ActiveModel::EachValidator
|
||||||
def validate_each(record, attribute, value)
|
def validate_each(record, attribute, value)
|
||||||
min, max = options[:min].call, options[:max].call
|
min, max = options[:min].call, options[:max].call
|
||||||
return if value.count { |i| !i.marked_for_destruction? }.between?(min, max)
|
values = value.reject(&:marked_for_destruction?)
|
||||||
|
|
||||||
|
return if values.size.between?(min, max)
|
||||||
association = options[:association] || attribute
|
association = options[:association] || attribute
|
||||||
record.errors.add(association, :out_of_range, { min: min, max: max })
|
record.errors.add(association, :out_of_range, { min: min, max: max })
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue