validation_hints 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +33 -3
- data/lib/active_model/hints.rb +22 -21
- data/lib/validation_hints/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,4 +1,34 @@
|
|
1
|
-
validation_hints
|
2
|
-
================
|
3
1
|
|
4
|
-
|
2
|
+
## Validation Hints
|
3
|
+
|
4
|
+
Delivers hints derived from the validation on a model.
|
5
|
+
|
6
|
+
### Install
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'validation_hints'
|
10
|
+
```
|
11
|
+
|
12
|
+
### Example
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
class Person < ActiveRecord::Base
|
16
|
+
validates :name, :presence => true
|
17
|
+
validates :password, :length => { :within => 1...5 }
|
18
|
+
end
|
19
|
+
```
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Person.new.hints[:name] => ["can't be blank"]
|
23
|
+
Person.new.hints[:password] => ["must not be shorter than 1 characters", "must not be longer than 4 characters"]
|
24
|
+
Person.new.hints.messages => {:id=>[], :password=>["must not be shorter than 1 characters", "must not be longer than 4 characters"], :name => ["can't be blank"] }
|
25
|
+
```
|
26
|
+
|
27
|
+
### Disclaimer
|
28
|
+
|
29
|
+
It's work in progress.
|
30
|
+
validation_hints was for the most part derived from activerecord-3.2.3/lib/active_record/errors.rb
|
31
|
+
|
32
|
+
|
33
|
+
|
34
|
+
|
data/lib/active_model/hints.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
module ActiveModel
|
2
2
|
# == Active Model Hints
|
3
3
|
#
|
4
|
+
# p = Person.new
|
5
|
+
# p.hints
|
6
|
+
# p.hints[:name]
|
7
|
+
#
|
4
8
|
# more documentation needed
|
5
9
|
|
6
10
|
class Hints
|
@@ -23,10 +27,27 @@ module ActiveModel
|
|
23
27
|
@base = base
|
24
28
|
@messages = ActiveSupport::OrderedHash.new
|
25
29
|
@base.attributes.keys.each do |a|
|
26
|
-
@messages[a.to_sym] =
|
30
|
+
@messages[a.to_sym] = hints_for(a.to_sym)
|
27
31
|
end
|
28
32
|
end
|
29
33
|
|
34
|
+
def hints_for(attribute)
|
35
|
+
result = Array.new
|
36
|
+
@base.class.validators_on(attribute).map do |v|
|
37
|
+
# check for validators that have no options
|
38
|
+
validator = v.class.to_s.split('::').last.downcase.gsub('validator','')
|
39
|
+
if MESSAGES_FOR_VALIDATORS.include?(validator)
|
40
|
+
result << generate_message(attribute, validator)
|
41
|
+
end
|
42
|
+
v.options.each do |o|
|
43
|
+
if MESSAGES_FOR_OPTIONS.include?(o.first.to_s)
|
44
|
+
result << generate_message(attribute, [ validator, o.first.to_s ].join('.'), { :count => o.last } )
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
result
|
49
|
+
end
|
50
|
+
|
30
51
|
def initialize_dup(other)
|
31
52
|
@messages = other.messages.dup
|
32
53
|
end
|
@@ -242,25 +263,6 @@ module ActiveModel
|
|
242
263
|
})
|
243
264
|
end
|
244
265
|
|
245
|
-
def validation_hints_for(attribute)
|
246
|
-
result = Array.new
|
247
|
-
@base.class.validators_on(attribute).map do |v|
|
248
|
-
# puts "** ** ** ** V " + v.class.to_s.split('::').last.downcase.gsub('validator','')
|
249
|
-
# check for validators that have no options
|
250
|
-
validator = v.class.to_s.split('::').last.downcase.gsub('validator','')
|
251
|
-
if MESSAGES_FOR_VALIDATORS.include?(validator)
|
252
|
-
result << generate_message(attribute, validator)
|
253
|
-
end
|
254
|
-
v.options.each do |o|
|
255
|
-
# puts "** ** ** ** O " + o.inspect
|
256
|
-
if MESSAGES_FOR_OPTIONS.include?(o.first.to_s)
|
257
|
-
result << generate_message(attribute, [ validator, o.first.to_s ].join('.'), { :count => o.last } )
|
258
|
-
end
|
259
|
-
end
|
260
|
-
end
|
261
|
-
result
|
262
|
-
end
|
263
|
-
|
264
266
|
def generate_message(attribute, type, options = {})
|
265
267
|
if @base.class.respond_to?(:i18n_scope)
|
266
268
|
defaults = @base.class.lookup_ancestors.map do |klass|
|
@@ -285,7 +287,6 @@ module ActiveModel
|
|
285
287
|
:model => @base.class.model_name.human,
|
286
288
|
:attribute => @base.class.human_attribute_name(attribute),
|
287
289
|
}.merge(options)
|
288
|
-
# puts "*" + File.basename(__FILE__) + ": " + "ATTR #{attribute}, OPTIONS #{options.inspect} "
|
289
290
|
I18n.translate(key, options)
|
290
291
|
end
|
291
292
|
|