Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Made errors[] ActiveModel compliant. Now always returns an array. See…

…ms weird, but thems the ropes.
  • Loading branch information...
commit f4050e5977beedcc86fbb865178f66c373c76f11 1 parent 7a84884
@jnunemaker authored
Showing with 24 additions and 9 deletions.
  1. +11 −8 lib/validatable/errors.rb
  2. +13 −1 test/unit/test_errors.rb
View
19 lib/validatable/errors.rb
@@ -20,7 +20,7 @@ def invalid?(attribute)
end
# call-seq: on(attribute)
- #
+ #
# * Returns nil, if no errors are associated with the specified +attribute+.
# * Returns the error message, if one error is associated with the specified +attribute+.
# * Returns an array of error messages, if more than one error is associated with the specified +attribute+.
@@ -28,8 +28,11 @@ def on(attribute)
return nil if errors[attribute.to_sym].nil?
errors[attribute.to_sym].size == 1 ? errors[attribute.to_sym].first : errors[attribute.to_sym]
end
-
- alias [] on
+
+ # Rails 3 API for errors, always return array.
+ def [](attribute)
+ errors[attribute.to_sym] || []
+ end
def add(attribute, message) #:nodoc:
errors[attribute.to_sym] = [] if errors[attribute.to_sym].nil?
@@ -42,19 +45,19 @@ def merge!(errors) #:nodoc:
end
# call-seq: replace(attribute)
- #
+ #
# * Replaces the errors value for the given +attribute+
def replace(attribute, value)
errors[attribute.to_sym] = value
end
# call-seq: raw(attribute)
- #
+ #
# * Returns an array of error messages associated with the specified +attribute+.
def raw(attribute)
errors[attribute.to_sym]
end
-
+
def errors #:nodoc:
@errors ||= {}
end
@@ -64,7 +67,7 @@ def count #:nodoc:
end
# call-seq: full_messages -> an_array_of_messages
- #
+ #
# Returns an array containing the full list of error messages.
def full_messages
full_messages = []
@@ -82,7 +85,7 @@ def full_messages
end
full_messages
end
-
+
def humanize(lower_case_and_underscored_word) #:nodoc:
lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
end
View
14 test/unit/test_errors.rb
@@ -62,9 +62,21 @@
errors.count
end
- expect "message" do
+ expect [] do
+ errors = Validatable::Errors.new
+ errors[:attribute]
+ end
+
+ expect ["message"] do
errors = Validatable::Errors.new
errors.add(:attribute, "message")
errors[:attribute]
end
+
+ expect ["message1", "message2"] do
+ errors = Validatable::Errors.new
+ errors.add(:attribute, "message1")
+ errors.add(:attribute, "message2")
+ errors[:attribute]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.