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.
@@ -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.3'
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
@@ -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:
@@ -10,5 +10,9 @@ module Validatable
10
10
  def message(instance)
11
11
  super || "must be accepted"
12
12
  end
13
+
14
+ def i18n
15
+ super || "#{i18n_prefix}.accepted"
16
+ end
13
17
  end
14
18
  end
@@ -9,5 +9,9 @@ module Validatable
9
9
  def message(instance)
10
10
  super || "is invalid"
11
11
  end
12
+
13
+ def i18n
14
+ super || "#{i18n_prefix}.associated"
15
+ end
12
16
  end
13
17
  end
@@ -19,5 +19,9 @@ module Validatable
19
19
  def message(instance)
20
20
  super || "doesn't match confirmation"
21
21
  end
22
+
23
+ def i18n
24
+ super || "#{i18n_prefix}.confirmation"
25
+ end
22
26
  end
23
27
  end
@@ -10,5 +10,9 @@ module Validatable
10
10
  def message(instance)
11
11
  super || "is invalid"
12
12
  end
13
+
14
+ def i18n
15
+ super || "#{i18n_prefix}.invalid"
16
+ end
13
17
  end
14
18
  end
@@ -13,5 +13,10 @@ module Validatable
13
13
  def message(instance)
14
14
  super || "is reserved"
15
15
  end
16
+
17
+ def i18n
18
+ super || "#{i18n_prefix}.exclusion"
19
+ end
20
+
16
21
  end
17
22
  end
@@ -12,5 +12,9 @@ module Validatable
12
12
  def message(instance)
13
13
  super || "is invalid"
14
14
  end
15
+
16
+ def i18n
17
+ super || "#{i18n_prefix}.invalid"
18
+ end
15
19
  end
16
20
  end
@@ -13,5 +13,9 @@ module Validatable
13
13
  def message(instance)
14
14
  super || "is not in the list"
15
15
  end
16
+
17
+ def i18n
18
+ super || "#{i18n_prefix}.inclusion"
19
+ end
16
20
  end
17
21
  end
@@ -6,6 +6,10 @@ module Validatable
6
6
  super || "is invalid"
7
7
  end
8
8
 
9
+ def i18n
10
+ super || "#{i18n_prefix}.wrong_length"
11
+ end
12
+
9
13
  def valid?(instance)
10
14
  valid = true
11
15
  value = instance.send(self.attribute)
@@ -15,6 +15,10 @@ module Validatable
15
15
  def message(instance)
16
16
  super || "must be a number"
17
17
  end
18
+
19
+ def i18n
20
+ super || "#{i18n_prefix}.not_a_number"
21
+ end
18
22
 
19
23
  private
20
24
  def value_for(instance)
@@ -12,6 +12,10 @@ module Validatable
12
12
  def message(instance)
13
13
  super || "can't be empty"
14
14
  end
15
+
16
+ def i18n
17
+ super || "#{i18n_prefix}.blank"
18
+ end
15
19
  end
16
20
  end
17
21
 
@@ -9,5 +9,9 @@ module Validatable
9
9
  def message(instance)
10
10
  super || "is invalid"
11
11
  end
12
+
13
+ def i18n
14
+ super || "#{i18n_prefix}.true"
15
+ end
12
16
  end
13
17
  end
@@ -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
- @message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
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
@@ -35,4 +35,14 @@ Expectations do
35
35
  end
36
36
  klass.validation_keys_include?("key")
37
37
  end
38
+
39
+ expect true do
40
+ Validatable.use_i18n = true
41
+ Validatable.use_i18n?
42
+ end
43
+
44
+ expect false do
45
+ Validatable.use_i18n = false
46
+ Validatable.use_i18n?
47
+ end
38
48
  end
@@ -48,5 +48,5 @@ Expectations do
48
48
  options = {:message => nil, :if => nil, :times => nil, :level => nil, :groups => nil, :key => nil}
49
49
  Validatable::ValidationBase.new(stub_everything, :base).must_understand(options)
50
50
  end
51
-
51
+
52
52
  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
- version: 1.8.3
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-19 00:00:00 +02:00
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.5
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.
@@ -1,5 +0,0 @@
1
- ---
2
- :minor: 8
3
- :patch: 1
4
- :major: 1
5
- :build: