serial_translator 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d27d575ab149486ef5bbc0de313ed02910fc219
|
4
|
+
data.tar.gz: 1f451012813d7f174b899854cbe7df20c27d351b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb6562608cbf2444e6685dffda92c86a5ffae68ddfa0348f75951dc78a8c5558c1bacdf0f05f36172e7cf06eb04e1143bd26a1562994fcc238f5ce67fd688bb4
|
7
|
+
data.tar.gz: 58245bc9e7834be0f6a38dc6dfb7b12901edc1cdac60c435604d3f08fa9b5414d1c1a7416de1900414aeb33171ca1de5ecb71be3b13a0ef96d669a1481aa602d
|
@@ -1,36 +1,38 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
1
|
+
module SerialTranslator
|
2
|
+
class SerialTranslatorLengthValidator < ActiveModel::Validations::LengthValidator
|
3
|
+
def validate_each(record, attribute, _value)
|
4
|
+
translations = record.__send__("#{attribute}_translations")&.values || []
|
5
|
+
translations = [nil] if translations.empty?
|
6
|
+
translations.each do |value|
|
7
|
+
next if options[:allow_blank] && value.to_s == '' ||
|
8
|
+
options[:allow_nil] && value.nil?
|
9
|
+
validate_translation(record, attribute, value)
|
10
|
+
end
|
9
11
|
end
|
10
|
-
end
|
11
12
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
# this is ActiveModel::Validations::LengthValidator#validate_each,
|
14
|
+
# only extended with more interpolation args in error_options
|
15
|
+
def validate_translation(record, attribute, value)
|
16
|
+
value_length = value.respond_to?(:length) ? value.length : value.to_s.length
|
17
|
+
error_options = options.except(*RESERVED_OPTIONS)
|
17
18
|
|
18
|
-
|
19
|
-
|
19
|
+
CHECKS.each do |key, validity_check|
|
20
|
+
next unless check_value = options[key]
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
if !value.nil? || skip_nil_check?(key)
|
23
|
+
next if value_length.send(validity_check, check_value)
|
24
|
+
end
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
26
|
+
error_options.merge!(count: check_value, actual_length: value_length)
|
27
|
+
key == :minimum && error_options[:difference] = check_value - value_length
|
28
|
+
key == :maximum && error_options[:difference] = value_length - check_value
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
default_message = options[MESSAGES[key]]
|
31
|
+
error_options[:message] ||= default_message if default_message
|
32
|
+
record.errors.add(attribute, MESSAGES[key], error_options)
|
33
|
+
end
|
32
34
|
end
|
33
|
-
end
|
34
35
|
|
35
|
-
|
36
|
+
def kind; :length end
|
37
|
+
end
|
36
38
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module SerialTranslator
|
2
|
+
class SerialTranslatorPresenceValidator < ActiveModel::Validations::PresenceValidator
|
3
|
+
def validate_each(record, attribute, _value)
|
4
|
+
translations = record.__send__("#{attribute}_translations") || {}
|
5
|
+
return if translations.values.any?(&:present?)
|
6
|
+
record.errors.add_on_blank(attribute, options)
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
+
def kind; :presence end
|
10
|
+
end
|
9
11
|
end
|