Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add documentation/tests around interpolated values in ActiveModel::Errors #14067

Open
wants to merge 3 commits into from

3 participants

@tbloncar

Document and test :count option in #add and #added? methods. The purpose of this documentation is to make it clear that the #add and #added? methods should be invoked with the :count option for messages that make use of an interpolated count (by default). Failure to do so will result in an I18n::MissingInterpolationArgument exception.

@tbloncar tbloncar Add documentation/tests around interpolated values in
ActiveModel::Errors.

Document and test :count option in #add and #added? methods.
a137690
@robin850
Collaborator

Hello @tbloncar,

Thank you so much for the patch! This looks good to me. This is just a nitpick but you can now use ++ delimiters for symbols instead of <tt> (this is not yet in the guides though ; see 225bcad).

@rafaelfranca

I think this is not only about the count value. Any other interpolation values can be added to the messages. I think we should be more generic about it.

@tbloncar

Thanks for the feedback, @robin850 and @rafaelfranca. I updated the patch to make use of ++ and revised the documentation so that it addresses the more general case of interpolated values. I chose count to illustrate this, as it is prominently featured here.

@rafaelfranca rafaelfranca commented on the diff
activemodel/lib/active_model/errors.rb
@@ -289,6 +289,16 @@ def to_hash(full_messages = false)
# # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
+ #
+ # If the +message+ being added makes use of an interpolated value
+ # (e.g., +count+), this value must be specified as an option. Failure
@rafaelfranca Owner

I think we don't need to talk about the failure case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca rafaelfranca commented on the diff
activemodel/lib/active_model/errors.rb
@@ -331,6 +341,18 @@ def add_on_blank(attributes, options = {})
#
# person.errors.add :name, :blank
# person.errors.added? :name, :blank # => true
+ #
+ # When used to check for a +message+ that includes an interpolated value
+ # (e.g., +count+), it requires that we specify this value as an option.
@rafaelfranca Owner
it is required to specify this values as options. This interpolated value
is used in the check for the message's presence. For instance, providing
a +:count+ that does not match the +:count+ provided when the message
was added will return +false+.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 15, 2014
  1. @tbloncar

    Add documentation/tests around interpolated values in

    tbloncar authored
    ActiveModel::Errors.
    
    Document and test :count option in #add and #added? methods.
Commits on Feb 17, 2014
  1. @tbloncar
  2. @tbloncar

    Fix documentation syntax.

    tbloncar authored
This page is out of date. Refresh to see the latest.
View
22 activemodel/lib/active_model/errors.rb
@@ -289,6 +289,16 @@ def to_hash(full_messages = false)
# # => NameIsInvalid: name is invalid
#
# person.errors.messages # => {}
+ #
+ # If the +message+ being added makes use of an interpolated value
+ # (e.g., +count+), this value must be specified as an option. Failure
@rafaelfranca Owner

I think we don't need to talk about the failure case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # to do so will result in an +I18n::MissingInterpolationArgument+
+ # exception.
+ #
+ # person.errors.add(:name, :too_short)
+ # # => I18n::MissingInterpolationArgument
+ # person.errors.add(:name, :too_short, count: 7)
+ # # => ["is too short (minimum is 7 characters)"]
def add(attribute, message = :invalid, options = {})
message = normalize_message(attribute, message, options)
if exception = options[:strict]
@@ -331,6 +341,18 @@ def add_on_blank(attributes, options = {})
#
# person.errors.add :name, :blank
# person.errors.added? :name, :blank # => true
+ #
+ # When used to check for a +message+ that includes an interpolated value
+ # (e.g., +count+), it requires that we specify this value as an option.
@rafaelfranca Owner
it is required to specify this values as options. This interpolated value
is used in the check for the message's presence. For instance, providing
a +:count+ that does not match the +:count+ provided when the message
was added will return +false+.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ # This interpolated value is used in the check for the message's presence.
+ # For instance, providing a +:count+ that does not match the +:count+
+ # provided in +add+ for an attribute-message pair will result in a +false+
+ # return value.
+ #
+ # person.errors.add(:name, :too_long, count: 22)
+ # person.errors.added?(:name, :too_long) # => I18n::MissingInterpolationArgument
+ # person.errors.added?(:name, :too_long, count: 33) # => false
+ # person.errors.added?(:name, :too_long, count: 22) # => true
def added?(attribute, message = :invalid, options = {})
message = normalize_message(attribute, message, options)
self[attribute].include? message
View
24 activemodel/test/cases/errors_test.rb
@@ -132,6 +132,13 @@ def test_has_no_key
assert_equal [message], person.errors[:name]
end
+ test "add an error message that includes an interpolated count" do
+ person = Person.new
+ person.errors.add(:name, :wrong_length, count: 1)
+ message = person.errors.generate_message(:name, :wrong_length, count: 1)
+ assert_equal [message], person.errors[:name]
+ end
+
test "add an error with a proc" do
person = Person.new
message = Proc.new { "cannot be blank" }
@@ -182,6 +189,23 @@ def test_has_no_key
assert !person.errors.added?(:name, "cannot be blank")
end
+ test "added? returns true when checking for an existing error message that includes an interpolated count" do
+ person = Person.new
+ person.errors.add(:age, :greater_than_or_equal_to, count: 1)
+ assert person.errors.added?(:age, :greater_than_or_equal_to, count: 1)
+ end
+
+ test "added? returns false when checking for a nonexisting error message that includes an interpolated count" do
+ person = Person.new
+ assert !person.errors.added?(:name, :too_short, count: 1)
+ end
+
+ test "added? returns false when checking for an existing error message and providing a non-matching interpolated count" do
+ person = Person.new
+ person.errors.add(:name, :too_long, count: 1)
+ assert !person.errors.added?(:name, :too_long, count: 2)
+ end
+
test "size calculates the number of error messages" do
person = Person.new
person.errors.add(:name, "cannot be blank")
Something went wrong with that request. Please try again.