client_side_validations 2.4.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -11,25 +11,25 @@ module DNCLabs
|
|
|
11
11
|
def validation_to_hash(_attr, _options = {})
|
|
12
12
|
validation_hash = {}
|
|
13
13
|
base._validators[_attr.to_sym].each do |validation|
|
|
14
|
-
if can_validate?(validation)
|
|
14
|
+
if can_validate?(validation) && supported_validation?(validation)
|
|
15
15
|
message = get_validation_message(validation, _options[:locale])
|
|
16
16
|
validation.options.delete(:message)
|
|
17
17
|
options = get_validation_options(validation.options)
|
|
18
18
|
method = get_validation_method(validation.kind)
|
|
19
|
-
if conditional_method = remove_reserved_conditionals(options[
|
|
19
|
+
if conditional_method = remove_reserved_conditionals(options[:if])
|
|
20
20
|
if base.instance_eval(conditional_method.to_s)
|
|
21
|
-
options.delete(
|
|
22
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
21
|
+
options.delete(:if)
|
|
22
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
23
23
|
end
|
|
24
|
-
elsif conditional_method = options[
|
|
24
|
+
elsif conditional_method = options[:unless]
|
|
25
25
|
unless base.instance_eval(conditional_method.to_s)
|
|
26
|
-
options.delete(
|
|
27
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
26
|
+
options.delete(:unless)
|
|
27
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
28
28
|
end
|
|
29
29
|
else
|
|
30
|
-
options.delete(
|
|
31
|
-
options.delete(
|
|
32
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
30
|
+
options.delete(:if)
|
|
31
|
+
options.delete(:unless)
|
|
32
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -43,6 +43,11 @@ module DNCLabs
|
|
|
43
43
|
|
|
44
44
|
private
|
|
45
45
|
|
|
46
|
+
def supported_validation?(validation)
|
|
47
|
+
[:presence, :format, :length, :numericality, :uniqueness,
|
|
48
|
+
:confirmation ].include?(validation.kind.to_sym)
|
|
49
|
+
end
|
|
50
|
+
|
|
46
51
|
def can_validate?(validation)
|
|
47
52
|
if on = validation.options[:on]
|
|
48
53
|
on = on.to_sym
|
|
@@ -89,14 +94,14 @@ module DNCLabs
|
|
|
89
94
|
end
|
|
90
95
|
|
|
91
96
|
def get_validation_options(options)
|
|
92
|
-
options
|
|
93
|
-
options.delete(
|
|
94
|
-
options.delete(
|
|
95
|
-
options.delete(
|
|
96
|
-
options.delete(
|
|
97
|
-
options.delete(
|
|
98
|
-
if options[
|
|
99
|
-
options[
|
|
97
|
+
options.symbolize_keys!
|
|
98
|
+
options.delete(:on) == :create
|
|
99
|
+
options.delete(:tokenizer)
|
|
100
|
+
options.delete(:only_integer)
|
|
101
|
+
options.delete(:allow_nil)
|
|
102
|
+
options.delete(:case_sensitive)
|
|
103
|
+
if options[:with].kind_of?(Regexp)
|
|
104
|
+
options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
|
|
100
105
|
end
|
|
101
106
|
options
|
|
102
107
|
end
|
|
@@ -10,26 +10,26 @@ module DNCLabs
|
|
|
10
10
|
self.base = base
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def validation_to_hash(
|
|
13
|
+
def validation_to_hash(attr, options = {})
|
|
14
14
|
validation_hash = {}
|
|
15
|
-
base.class.reflect_on_validations_for(
|
|
16
|
-
if can_validate?(validation)
|
|
17
|
-
message = get_validation_message(validation,
|
|
15
|
+
base.class.reflect_on_validations_for(attr).each do |validation|
|
|
16
|
+
if can_validate?(validation) && supported_validation?(validation)
|
|
17
|
+
message = get_validation_message(validation, options[:locale])
|
|
18
18
|
validation.options.delete(:message)
|
|
19
|
-
options =
|
|
19
|
+
options = get_validationoptions(validation.options)
|
|
20
20
|
method = get_validation_method(validation.macro)
|
|
21
|
-
if conditional_method = options[
|
|
21
|
+
if conditional_method = options[:if]
|
|
22
22
|
if base.instance_eval(conditional_method.to_s)
|
|
23
|
-
options.delete(
|
|
24
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
23
|
+
options.delete(:if)
|
|
24
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
25
25
|
end
|
|
26
|
-
elsif conditional_method = options[
|
|
26
|
+
elsif conditional_method = options[:unless]
|
|
27
27
|
unless base.instance_eval(conditional_method.to_s)
|
|
28
|
-
options.delete(
|
|
29
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
28
|
+
options.delete(:unless)
|
|
29
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
30
30
|
end
|
|
31
31
|
else
|
|
32
|
-
validation_hash[method] = { 'message' => message }.merge(options)
|
|
32
|
+
validation_hash[method] = { 'message' => message }.merge(options.stringify_keys)
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -43,6 +43,12 @@ module DNCLabs
|
|
|
43
43
|
|
|
44
44
|
private
|
|
45
45
|
|
|
46
|
+
def supported_validation?(validation)
|
|
47
|
+
[:validates_presence_of, :validates_format_of, :validates_length_of,
|
|
48
|
+
:validates_numericality_of, :validates_uniqueness_of,
|
|
49
|
+
:validates_confirmation_of].include?(validation.macro.to_sym)
|
|
50
|
+
end
|
|
51
|
+
|
|
46
52
|
def can_validate?(validation)
|
|
47
53
|
if on = validation.options[:on]
|
|
48
54
|
on = on.to_sym
|
|
@@ -55,22 +61,22 @@ module DNCLabs
|
|
|
55
61
|
end
|
|
56
62
|
|
|
57
63
|
def get_validation_message(validation, locale)
|
|
58
|
-
default = case validation.macro.
|
|
59
|
-
when
|
|
64
|
+
default = case validation.macro.to_sym
|
|
65
|
+
when :validates_presence_of
|
|
60
66
|
I18n.translate('activerecord.errors.messages.blank', :locale => locale)
|
|
61
|
-
when
|
|
67
|
+
when :validates_format_of
|
|
62
68
|
I18n.translate('activerecord.errors.messages.invalid', :locale => locale)
|
|
63
|
-
when
|
|
69
|
+
when :validates_length_of
|
|
64
70
|
if count = validation.options[:minimum]
|
|
65
71
|
I18n.translate('activerecord.errors.messages.too_short', :locale => locale).sub('{{count}}', count.to_s)
|
|
66
72
|
elsif count = validation.options[:maximum]
|
|
67
73
|
I18n.translate('activerecord.errors.messages.too_long', :locale => locale).sub('{{count}}', count.to_s)
|
|
68
74
|
end
|
|
69
|
-
when
|
|
75
|
+
when :validates_numericality_of
|
|
70
76
|
I18n.translate('activerecord.errors.messages.not_a_number', :locale => locale)
|
|
71
|
-
when
|
|
77
|
+
when :validates_uniqueness_of
|
|
72
78
|
I18n.translate('activerecord.errors.messages.taken', :locale => locale)
|
|
73
|
-
when
|
|
79
|
+
when :validates_confirmation_of
|
|
74
80
|
I18n.translate('activerecord.errors.messages.confirmation', :locale => locale)
|
|
75
81
|
end
|
|
76
82
|
|
|
@@ -84,30 +90,32 @@ module DNCLabs
|
|
|
84
90
|
end
|
|
85
91
|
end
|
|
86
92
|
|
|
87
|
-
def
|
|
88
|
-
options
|
|
89
|
-
options.delete(
|
|
90
|
-
if options[
|
|
91
|
-
options[
|
|
93
|
+
def get_validationoptions(options)
|
|
94
|
+
options.symbolize_keys!
|
|
95
|
+
options.delete(:on)
|
|
96
|
+
if options[:with].kind_of?(Regexp)
|
|
97
|
+
options[:with] = options[:with].inspect.to_s.sub("\\A","^").sub("\\Z","$").sub(%r{^/},"").sub(%r{/i?$}, "")
|
|
92
98
|
end
|
|
93
99
|
options
|
|
94
100
|
end
|
|
95
101
|
|
|
96
102
|
def get_validation_method(method)
|
|
97
|
-
case method.
|
|
98
|
-
when
|
|
99
|
-
|
|
100
|
-
when
|
|
101
|
-
|
|
102
|
-
when
|
|
103
|
-
|
|
104
|
-
when
|
|
105
|
-
|
|
106
|
-
when
|
|
107
|
-
|
|
108
|
-
when
|
|
109
|
-
|
|
103
|
+
method = case method.to_sym
|
|
104
|
+
when :validates_presence_of
|
|
105
|
+
:presence
|
|
106
|
+
when :validates_format_of
|
|
107
|
+
:format
|
|
108
|
+
when :validates_numericality_of
|
|
109
|
+
:numericality
|
|
110
|
+
when :validates_length_of
|
|
111
|
+
:length
|
|
112
|
+
when :validates_uniqueness_of
|
|
113
|
+
:uniqueness
|
|
114
|
+
when :validates_confirmation_of
|
|
115
|
+
:confirmation
|
|
110
116
|
end
|
|
117
|
+
|
|
118
|
+
method.to_s
|
|
111
119
|
end
|
|
112
120
|
end
|
|
113
121
|
end
|