validation_hints 0.0.4 → 0.0.5
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.
- data/lib/active_model/hints.rb +47 -16
- data/lib/validation_hints/version.rb +1 -1
- data/lib/validation_hints.rb +12 -0
- metadata +1 -1
data/lib/active_model/hints.rb
CHANGED
@@ -6,6 +6,10 @@ module ActiveModel
|
|
6
6
|
class Hints
|
7
7
|
include Enumerable
|
8
8
|
|
9
|
+
CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank, :strict]
|
10
|
+
MESSAGES_FOR_VALIDATORS = %w(confirmation acceptance presence uniqueness format inclusion exclusion associated numericality)
|
11
|
+
MESSAGES_FOR_OPTIONS = %w(within in is minimum maximum greater_than greater_than_or_equal_to equal_to less_than less_than_or_equal_to odd even)
|
12
|
+
|
9
13
|
attr_reader :messages
|
10
14
|
|
11
15
|
# Pass in the instance of the object that is using the errors object.
|
@@ -24,7 +28,7 @@ module ActiveModel
|
|
24
28
|
@messages = other.messages.dup
|
25
29
|
end
|
26
30
|
|
27
|
-
# Backport dup from 1.9 so that #initialize_dup gets called
|
31
|
+
# Backport dup from 1.9 so that #initialize_dup gets called
|
28
32
|
unless Object.respond_to?(:initialize_dup)
|
29
33
|
def dup # :nodoc:
|
30
34
|
copy = super
|
@@ -38,25 +42,52 @@ module ActiveModel
|
|
38
42
|
messages.clear
|
39
43
|
end
|
40
44
|
|
41
|
-
def
|
42
|
-
self
|
43
|
-
end
|
44
|
-
|
45
|
-
def t(a)
|
46
|
-
a
|
47
|
-
end
|
48
|
-
|
49
|
-
def validation_help_for(attribute)
|
45
|
+
def validation_hints_for(attribute)
|
50
46
|
@base.class.validators_on(attribute).map do |v|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
t(main_key + "." + o.to_s)
|
56
|
-
end ].flatten
|
47
|
+
validator = v.class.to_s.split('::').last.downcase.gsub('validator','')
|
48
|
+
if MESSAGES_FOR_VALIDATORS.include?(validator)
|
49
|
+
generate_keys(attribute, validator)
|
50
|
+
end
|
57
51
|
end.flatten.compact
|
52
|
+
# key = v.class.to_s.underscore.gsub('/','.')
|
53
|
+
# puts "************#{v.inspect}"
|
54
|
+
# key = [v.qualifier, key].join('.') if v.respond_to?(:qualifier)
|
55
|
+
# [ key, v.options.except(*CALLBACKS_OPTIONS).keys.map do |o|
|
56
|
+
# key + "." + o.to_s
|
57
|
+
# end ].flatten
|
58
58
|
end
|
59
59
|
|
60
|
+
def generate_keys(attribute, type)
|
61
|
+
|
62
|
+
if @base.class.respond_to?(:i18n_scope)
|
63
|
+
defaults = @base.class.lookup_ancestors.map do |klass|
|
64
|
+
[ :"#{@base.class.i18n_scope}.hints.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
65
|
+
:"#{@base.class.i18n_scope}.hints.models.#{klass.model_name.i18n_key}.#{type}" ]
|
66
|
+
end
|
67
|
+
else
|
68
|
+
defaults = []
|
69
|
+
end
|
70
|
+
|
71
|
+
defaults << :"#{@base.class.i18n_scope}.hints.messages.#{type}" if @base.class.respond_to?(:i18n_scope)
|
72
|
+
defaults << :"hints.attributes.#{attribute}.#{type}"
|
73
|
+
defaults << :"hints.messages.#{type}"
|
74
|
+
|
75
|
+
defaults.compact!
|
76
|
+
defaults.flatten!
|
77
|
+
|
78
|
+
#key = defaults.shift
|
79
|
+
|
80
|
+
options = {
|
81
|
+
:default => defaults,
|
82
|
+
:model => @base.class.model_name.human,
|
83
|
+
:attribute => @base.class.human_attribute_name(attribute),
|
84
|
+
}
|
85
|
+
puts "*" + File.basename(__FILE__) + ": " + "ATTR #{attribute}, OPTIONS #{options.inspect} "
|
86
|
+
#I18n.translate(key, options)
|
87
|
+
[ defaults, options ]
|
88
|
+
end
|
89
|
+
|
90
|
+
|
60
91
|
end
|
61
92
|
|
62
93
|
end
|
data/lib/validation_hints.rb
CHANGED
@@ -4,11 +4,23 @@ require 'validation_hints/version'
|
|
4
4
|
require 'active_model/hints'
|
5
5
|
|
6
6
|
module ActiveModel
|
7
|
+
|
7
8
|
module Validations
|
9
|
+
|
10
|
+
def has_validations?
|
11
|
+
! self.class.validators.empty?
|
12
|
+
end
|
13
|
+
|
8
14
|
def hints
|
9
15
|
@hints ||= Hints.new(self)
|
10
16
|
end
|
17
|
+
|
18
|
+
def hints_for(attribute)
|
19
|
+
hints.validation_hints_for(attribute)
|
20
|
+
end
|
21
|
+
|
11
22
|
end
|
23
|
+
|
12
24
|
end
|
13
25
|
|
14
26
|
require 'active_support/i18n'
|