mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Reports Refactor 2: Move all code to lib/reporting
This commit is contained in:
committed by
Jean-Baptiste Bellet
parent
392166b57a
commit
5f78fdce8b
@@ -35,8 +35,13 @@
|
||||
# post.valid? # => false
|
||||
# post.errors[:related_post_ids] # => ["must contain only valid integers"]
|
||||
class IntegerArrayValidator < ActiveModel::EachValidator
|
||||
NOT_ARRAY_ERROR = I18n.t("validators.integer_array_validator.not_array_error")
|
||||
INVALID_ELEMENT_ERROR = I18n.t("validators.integer_array_validator.invalid_element_error")
|
||||
def self.not_array_error
|
||||
I18n.t("validators.integer_array_validator.not_array_error")
|
||||
end
|
||||
|
||||
def self.invalid_element_error
|
||||
I18n.t("validators.integer_array_validator.invalid_element_error")
|
||||
end
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
return if value.nil?
|
||||
@@ -50,7 +55,7 @@ class IntegerArrayValidator < ActiveModel::EachValidator
|
||||
def validate_attribute_is_array(record, attribute, value)
|
||||
return if value.is_a?(Array)
|
||||
|
||||
record.errors.add(attribute, NOT_ARRAY_ERROR)
|
||||
record.errors.add(attribute, IntegerArrayValidator.not_array_error)
|
||||
end
|
||||
|
||||
def validate_attribute_elements_are_integer(record, attribute, array)
|
||||
@@ -60,6 +65,6 @@ class IntegerArrayValidator < ActiveModel::EachValidator
|
||||
Integer(element)
|
||||
end
|
||||
rescue ArgumentError
|
||||
record.errors.add(attribute, INVALID_ELEMENT_ERROR)
|
||||
record.errors.add(attribute, IntegerArrayValidator.invalid_element_error)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user