mattmatt-validatable 1.8.3 → 1.8.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/validatable.rb +9 -1
- data/lib/validatable/errors.rb +25 -3
- data/lib/validatable/validatable_instance_methods.rb +1 -1
- data/lib/validatable/validations/validates_acceptance_of.rb +4 -0
- data/lib/validatable/validations/validates_associated.rb +4 -0
- data/lib/validatable/validations/validates_confirmation_of.rb +4 -0
- data/lib/validatable/validations/validates_each.rb +4 -0
- data/lib/validatable/validations/validates_exclusion_of.rb +5 -0
- data/lib/validatable/validations/validates_format_of.rb +4 -0
- data/lib/validatable/validations/validates_inclusion_of.rb +4 -0
- data/lib/validatable/validations/validates_length_of.rb +4 -0
- data/lib/validatable/validations/validates_numericality_of.rb +4 -0
- data/lib/validatable/validations/validates_presence_of.rb +4 -0
- data/lib/validatable/validations/validates_true_for.rb +4 -0
- data/lib/validatable/validations/validation_base.rb +14 -2
- data/test/functional/test_validatable.rb +39 -0
- data/test/unit/test_validatable.rb +10 -0
- data/test/unit/test_validation_base.rb +1 -1
- metadata +12 -6
- data/VERSION.yml +0 -5
data/lib/validatable.rb
CHANGED
@@ -24,5 +24,13 @@ require 'validatable/validations/validates_each'
|
|
24
24
|
require 'validatable/validations/validates_associated'
|
25
25
|
|
26
26
|
module Validatable
|
27
|
-
Version = '1.8.
|
27
|
+
Version = '1.8.4'
|
28
|
+
|
29
|
+
def self.use_i18n=(i18n)
|
30
|
+
@i18n = i18n
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.use_i18n?
|
34
|
+
@i18n
|
35
|
+
end
|
28
36
|
end
|
data/lib/validatable/errors.rb
CHANGED
@@ -5,6 +5,10 @@ module Validatable
|
|
5
5
|
|
6
6
|
def_delegators :errors, :clear, :each, :each_pair, :empty?, :length, :size
|
7
7
|
|
8
|
+
def initialize(owner = nil)
|
9
|
+
@owner = owner
|
10
|
+
end
|
11
|
+
|
8
12
|
# Returns true if the specified +attribute+ has errors associated with it.
|
9
13
|
#
|
10
14
|
# class Company < ActiveRecord::Base
|
@@ -26,7 +30,7 @@ module Validatable
|
|
26
30
|
# * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
|
27
31
|
def on(attribute)
|
28
32
|
return nil if errors[attribute.to_sym].nil?
|
29
|
-
errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
|
33
|
+
error = errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
|
30
34
|
end
|
31
35
|
|
32
36
|
# Rails 3 API for errors, always return array.
|
@@ -36,7 +40,7 @@ module Validatable
|
|
36
40
|
|
37
41
|
def add(attribute, message) #:nodoc:
|
38
42
|
errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
|
39
|
-
errors[attribute.to_sym] << message
|
43
|
+
errors[attribute.to_sym] << (Validatable.use_i18n? ? i18n_message(attribute, message) : message)
|
40
44
|
end
|
41
45
|
|
42
46
|
def merge!(errors) #:nodoc:
|
@@ -66,6 +70,24 @@ module Validatable
|
|
66
70
|
errors.values.flatten.size
|
67
71
|
end
|
68
72
|
|
73
|
+
def translate_attribute(attribute)
|
74
|
+
I18n.t("validatable.attributes.#{klazz_name}.#{attribute.to_s}", :default => humanize(attribute.to_s))
|
75
|
+
end
|
76
|
+
|
77
|
+
def klazz_name
|
78
|
+
@owner.class.name.empty? ?
|
79
|
+
'generic' :
|
80
|
+
@owner.class.name.to_s.gsub(/::/, '/').
|
81
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
82
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
83
|
+
tr("-", "_").
|
84
|
+
downcase
|
85
|
+
end
|
86
|
+
|
87
|
+
def i18n_message(attribute, msg)
|
88
|
+
I18n.t(msg, :attribute => translate_attribute(attribute.to_s))
|
89
|
+
end
|
90
|
+
|
69
91
|
# call-seq: full_messages -> an_array_of_messages
|
70
92
|
#
|
71
93
|
# Returns an array containing the full list of error messages.
|
@@ -79,7 +101,7 @@ module Validatable
|
|
79
101
|
if attribute.to_s == "base"
|
80
102
|
full_messages << msg
|
81
103
|
else
|
82
|
-
full_messages << humanize(attribute.to_s) + " " + msg
|
104
|
+
full_messages << (Validatable.use_i18n? ? msg : humanize(attribute.to_s) + " " + msg)
|
83
105
|
end
|
84
106
|
end
|
85
107
|
end
|
@@ -15,7 +15,7 @@ module Validatable
|
|
15
15
|
#
|
16
16
|
# Returns the Errors object that holds all information about attribute error messages.
|
17
17
|
def errors
|
18
|
-
@errors ||= Validatable::Errors.new
|
18
|
+
@errors ||= Validatable::Errors.new(self)
|
19
19
|
end
|
20
20
|
|
21
21
|
def valid_for_group?(group) #:nodoc:
|
@@ -39,7 +39,7 @@ module Validatable
|
|
39
39
|
include Understandable
|
40
40
|
include Requireable
|
41
41
|
|
42
|
-
option :message, :if, :times, :level, :groups, :key, :after_validate, :allow_nil, :allow_blank
|
42
|
+
option :message, :if, :times, :level, :groups, :key, :after_validate, :allow_nil, :allow_blank, :i18n
|
43
43
|
default :level => 1, :groups => []
|
44
44
|
attr_accessor :attribute
|
45
45
|
|
@@ -71,9 +71,21 @@ module Validatable
|
|
71
71
|
end
|
72
72
|
result
|
73
73
|
end
|
74
|
+
|
75
|
+
def i18n
|
76
|
+
@i18n
|
77
|
+
end
|
78
|
+
|
79
|
+
def i18n_prefix
|
80
|
+
"validatable"
|
81
|
+
end
|
74
82
|
|
75
83
|
def message(instance)
|
76
|
-
|
84
|
+
if Validatable.use_i18n?
|
85
|
+
i18n
|
86
|
+
else
|
87
|
+
@message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
|
88
|
+
end
|
77
89
|
end
|
78
90
|
|
79
91
|
def validate_this_time?(instance)
|
@@ -1,5 +1,14 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
|
2
2
|
|
3
|
+
require 'i18n'
|
4
|
+
|
5
|
+
I18n.backend.store_translations(:en, {
|
6
|
+
:validatable => {
|
7
|
+
:blank => "{{attribute}} must be specified",
|
8
|
+
:attributes => {:user_address => {:street => "Strasse"}}
|
9
|
+
}
|
10
|
+
})
|
11
|
+
|
3
12
|
functional_tests do
|
4
13
|
|
5
14
|
expect "can't be empty" do
|
@@ -516,4 +525,34 @@ functional_tests do
|
|
516
525
|
instance.validate_only("presence_of/name")
|
517
526
|
instance.errors.on(:address)
|
518
527
|
end
|
528
|
+
|
529
|
+
test "should use the i18n key if i18n is enabled" do
|
530
|
+
Validatable.use_i18n = true
|
531
|
+
klass = Class.new do
|
532
|
+
include Validatable
|
533
|
+
validates_presence_of :name, :address
|
534
|
+
attr_accessor :name, :address
|
535
|
+
end
|
536
|
+
instance = klass.new
|
537
|
+
instance.valid?
|
538
|
+
assert_equal "Address must be specified", instance.errors.on(:address)
|
539
|
+
Validatable.use_i18n = false
|
540
|
+
end
|
541
|
+
|
542
|
+
test "should translate the attribute if i18n is enabled" do
|
543
|
+
Validatable.use_i18n = true
|
544
|
+
class UserAddress
|
545
|
+
include Validatable
|
546
|
+
validates_presence_of :street
|
547
|
+
attr_accessor :street
|
548
|
+
end
|
549
|
+
instance = UserAddress.new
|
550
|
+
instance.valid?
|
551
|
+
assert_equal "Strasse must be specified", instance.errors.on(:street)
|
552
|
+
end
|
553
|
+
|
554
|
+
def teardown
|
555
|
+
Validatable.use_i18n = false
|
556
|
+
|
557
|
+
end
|
519
558
|
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mattmatt-validatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 8
|
8
|
+
- 4
|
9
|
+
version: 1.8.4
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Jay Fields
|
@@ -10,7 +15,7 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2010-04
|
18
|
+
date: 2010-05-04 00:00:00 +02:00
|
14
19
|
default_executable:
|
15
20
|
dependencies: []
|
16
21
|
|
@@ -25,7 +30,6 @@ extra_rdoc_files:
|
|
25
30
|
files:
|
26
31
|
- README.rdoc
|
27
32
|
- Rakefile
|
28
|
-
- VERSION.yml
|
29
33
|
- lib/validatable.rb
|
30
34
|
- lib/validatable/child_validation.rb
|
31
35
|
- lib/validatable/errors.rb
|
@@ -88,18 +92,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
92
|
requirements:
|
89
93
|
- - ">="
|
90
94
|
- !ruby/object:Gem::Version
|
95
|
+
segments:
|
96
|
+
- 0
|
91
97
|
version: "0"
|
92
|
-
version:
|
93
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
99
|
requirements:
|
95
100
|
- - ">="
|
96
101
|
- !ruby/object:Gem::Version
|
102
|
+
segments:
|
103
|
+
- 0
|
97
104
|
version: "0"
|
98
|
-
version:
|
99
105
|
requirements: []
|
100
106
|
|
101
107
|
rubyforge_project:
|
102
|
-
rubygems_version: 1.3.
|
108
|
+
rubygems_version: 1.3.6
|
103
109
|
signing_key:
|
104
110
|
specification_version: 3
|
105
111
|
summary: Validatable is a library for adding validations.
|