validatable 1.3.2 → 1.3.4

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/errors.rb CHANGED
@@ -7,14 +7,17 @@ module Validatable
7
7
 
8
8
  # call-seq: on(attribute)
9
9
  #
10
- # Returns nil, if no errors are associated with the specified attribute.
11
- # Returns the error message, if one error is associated with the specified attribute.
10
+ # * Returns nil, if no errors are associated with the specified +attribute+.
11
+ # * Returns the error message, if one error is associated with the specified +attribute+.
12
+ # * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
12
13
  def on(attribute)
13
- errors[attribute.to_sym]
14
+ return nil if errors[attribute.to_sym].nil?
15
+ errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
14
16
  end
15
-
17
+
16
18
  def add(attribute, message) #:nodoc:
17
- errors[attribute.to_sym] = message
19
+ errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
20
+ errors[attribute.to_sym] << message
18
21
  end
19
22
 
20
23
  def merge!(errors) #:nodoc:
@@ -22,14 +25,25 @@ module Validatable
22
25
  self
23
26
  end
24
27
 
28
+ def replace(attribute, value)
29
+ errors[attribute.to_sym] = value
30
+ end
31
+
32
+ def raw(attribute)
33
+ errors[attribute.to_sym]
34
+ end
35
+
25
36
  def errors #:nodoc:
26
37
  @errors ||= {}
27
38
  end
28
39
 
29
- def count
40
+ def count #:nodoc:
30
41
  size
31
42
  end
32
43
 
44
+ # call-seq: full_messages -> an_array_of_messages
45
+ #
46
+ # Returns an array containing the full list of error messages.
33
47
  def full_messages
34
48
  full_messages = []
35
49
 
@@ -47,7 +61,7 @@ module Validatable
47
61
  full_messages
48
62
  end
49
63
 
50
- def humanize(lower_case_and_underscored_word)
64
+ def humanize(lower_case_and_underscored_word) #:nodoc:
51
65
  lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
52
66
  end
53
67
  end
@@ -200,8 +200,14 @@ module Validatable
200
200
  next unless child_validation.should_validate?(instance)
201
201
  child = instance.send child_validation.attribute
202
202
  child.valid?(*groups)
203
- child.errors.each do |attribute, message|
204
- add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
203
+ child.errors.each do |attribute, messages|
204
+ if messages.is_a?(String)
205
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, messages)
206
+ else
207
+ messages.each do |message|
208
+ add_error(instance, child_validation.map[attribute.to_sym] || attribute, message)
209
+ end
210
+ end
205
211
  end
206
212
  end
207
213
  end
@@ -210,8 +216,8 @@ module Validatable
210
216
  @validations ||= []
211
217
  end
212
218
 
213
- def add_error(instance, attribute, message) #:nodoc:
214
- instance.errors.add(attribute, message)
219
+ def add_error(instance, attribute, msg) #:nodoc:
220
+ instance.errors.add(attribute, msg)
215
221
  end
216
222
 
217
223
  protected
@@ -36,7 +36,7 @@ module Validatable
36
36
 
37
37
  def run_validation(validation) #:nodoc:
38
38
  validation_result = validation.valid?(self)
39
- add_error(validation.attribute, validation.message) unless validation_result
39
+ add_error(validation.attribute, validation.message(self)) unless validation_result
40
40
  validation.run_after_validate(validation_result, self, validation.attribute)
41
41
  end
42
42
 
@@ -4,7 +4,7 @@ module Validatable
4
4
  instance.send(self.attribute) == "true"
5
5
  end
6
6
 
7
- def message
7
+ def message(instance)
8
8
  super || "must be accepted"
9
9
  end
10
10
  end
@@ -8,7 +8,7 @@ module Validatable
8
8
  instance.send(self.attribute).to_s.casecmp(instance.send("#{self.attribute}_confirmation".to_sym).to_s) == 0
9
9
  end
10
10
 
11
- def message
11
+ def message(instance)
12
12
  super || "doesn't match confirmation"
13
13
  end
14
14
  end
@@ -6,7 +6,7 @@ module Validatable
6
6
  not (instance.send(self.attribute).to_s =~ self.with).nil?
7
7
  end
8
8
 
9
- def message
9
+ def message(instance)
10
10
  super || "is invalid"
11
11
  end
12
12
  end
@@ -2,7 +2,7 @@ module Validatable
2
2
  class ValidatesLengthOf < ValidationBase #:nodoc:
3
3
  option :minimum, :maximum, :is, :within, :allow_nil
4
4
 
5
- def message
5
+ def message(instance)
6
6
  super || "is invalid"
7
7
  end
8
8
 
@@ -22,4 +22,4 @@ module Validatable
22
22
  valid
23
23
  end
24
24
  end
25
- end
25
+ end
@@ -8,7 +8,7 @@ module Validatable
8
8
  not (value =~ regex).nil?
9
9
  end
10
10
 
11
- def message
11
+ def message(instance)
12
12
  super || "must be a number"
13
13
  end
14
14
  end
@@ -5,7 +5,7 @@ module Validatable
5
5
  instance.send(self.attribute).respond_to?(:strip) ? instance.send(self.attribute).strip.length != 0 : true
6
6
  end
7
7
 
8
- def message
8
+ def message(instance)
9
9
  super || "can't be empty"
10
10
  end
11
11
  end
@@ -6,7 +6,7 @@ module Validatable
6
6
  instance.instance_eval(&logic) == true
7
7
  end
8
8
 
9
- def message
9
+ def message(instance)
10
10
  super || "is invalid"
11
11
  end
12
12
  end
@@ -60,6 +60,10 @@ module Validatable
60
60
  instance.instance_eval(&self.if) && validate_this_time?
61
61
  end
62
62
 
63
+ def message(instance)
64
+ @message.respond_to?(:call) ? instance.instance_eval(&@message) : @message
65
+ end
66
+
63
67
  def validate_this_time?
64
68
  return true if @times.nil?
65
69
  self.times -= 1
data/rakefile.rb CHANGED
@@ -29,7 +29,7 @@ Gem::manage_gems
29
29
  specification = Gem::Specification.new do |s|
30
30
  s.name = "validatable"
31
31
  s.summary = "Validatable is a library for adding validations."
32
- s.version = "1.3.2"
32
+ s.version = "1.3.4"
33
33
  s.author = 'Jay Fields'
34
34
  s.description = "Validatable is a library for adding validations."
35
35
  s.email = 'validatable-developer@rubyforge.org'
@@ -175,17 +175,17 @@ module Functional
175
175
 
176
176
  Validatable::ValidationBase.class_eval do
177
177
  after_validate do |result, instance, attribute|
178
- instance.errors.add(attribute, instance.errors.on(attribute) + " changed message")
178
+ instance.errors.add(attribute, " changed message")
179
179
  end
180
180
  end
181
181
  Validatable::ValidatesPresenceOf.class_eval do
182
182
  after_validate do |result, instance, attribute|
183
- instance.errors.add(attribute, instance.errors.on(attribute) + " twice")
183
+ instance.errors.add(attribute, " twice")
184
184
  end
185
185
  end
186
186
  instance = klass.new
187
187
  instance.valid?
188
- assert_equal "can't be empty twice changed message", instance.errors.on(:name)
188
+ assert_equal "can't be empty twice changed message", instance.errors.on(:name).join
189
189
  Validatable::ValidatesPresenceOf.after_validations.clear
190
190
  Validatable::ValidationBase.after_validations.clear
191
191
  end
@@ -7,6 +7,19 @@ class ErrorsTest < Test::Unit::TestCase
7
7
  errors.on(:attribute)
8
8
  end
9
9
 
10
+ expect ["message"] do
11
+ errors = Validatable::Errors.new
12
+ errors.add(:attribute, "message")
13
+ errors.raw(:attribute)
14
+ end
15
+
16
+ expect "something new" do
17
+ errors = Validatable::Errors.new
18
+ errors.add(:attribute, "something old")
19
+ errors.replace(:attribute, ["something new"])
20
+ errors.on(:attribute)
21
+ end
22
+
10
23
  expect "Capitalized word" do
11
24
  errors = Validatable::Errors.new
12
25
  errors.humanize("capitalized_word")
@@ -27,4 +40,11 @@ class ErrorsTest < Test::Unit::TestCase
27
40
  test "includes enumerable" do
28
41
  assert_equal true, Validatable::Errors.included_modules.include?(Enumerable)
29
42
  end
43
+
44
+ expect ["message1", "message2"] do
45
+ errors = Validatable::Errors.new
46
+ errors.add(:attribute, "message1")
47
+ errors.add(:attribute, "message2")
48
+ errors.on(:attribute)
49
+ end
30
50
  end
@@ -33,6 +33,11 @@ class ValidationBaseTest < Test::Unit::TestCase
33
33
  validation.level
34
34
  end
35
35
 
36
+ expect "some message 100" do
37
+ validation = Validatable::ValidationBase.new :base, :message => lambda { "some message #{a_method}" }
38
+ validation.message(stub(:a_method=>'100'))
39
+ end
40
+
36
41
  test "invalid option causes raise" do
37
42
  assert_raises ArgumentError do
38
43
  Validatable::ValidationBase.new(:base).must_understand(:foo => 1, :bar => 2)
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
2
+ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: validatable
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.3.2
7
- date: 2007-05-30 00:00:00 -04:00
6
+ version: 1.3.4
7
+ date: 2007-05-31 00:00:00 -04:00
8
8
  summary: Validatable is a library for adding validations.
9
9
  require_paths:
10
10
  - lib