validation_hints 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -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 show
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
- key = v.class.to_s.underscore
52
- regex = /\//
53
- main_key = key.match(regex) ? key.gsub('/','.') : "inline_forms.validations." + key
54
- [ t(main_key), v.options.keys.map do |o|
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
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module ValidationHints
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
@@ -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'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation_hints
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: