diff options
author | Lawrence Pit <lawrence.pit@gmail.com> | 2011-09-09 18:28:25 +1000 |
---|---|---|
committer | Lawrence Pit <lawrence.pit@gmail.com> | 2011-09-09 18:28:25 +1000 |
commit | f5a944f662d3236f7bf3162d3b61850c61339b50 (patch) | |
tree | 41677553344d890943daa19fe342c496e01d26ab /activemodel/lib/active_model | |
parent | ac1a363c6ed889d11e8fabd6dd69a8a6df9e3cfd (diff) | |
download | rails-f5a944f662d3236f7bf3162d3b61850c61339b50.tar.gz rails-f5a944f662d3236f7bf3162d3b61850c61339b50.tar.bz2 rails-f5a944f662d3236f7bf3162d3b61850c61339b50.zip |
Add ability to get an individual full error message + test for full_messages.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 7828434927..d91e4a2b6a 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -180,6 +180,7 @@ module ActiveModel all? { |k, v| v && v.empty? } end alias_method :blank?, :empty? + # Returns an xml formatted representation of the Errors hash. # # p.errors.add(:name, "can't be blank") @@ -254,20 +255,22 @@ module ActiveModel # company.errors.full_messages # => # ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Email can't be blank"] def full_messages - map { |attribute, message| - if attribute == :base - message - else - attr_name = attribute.to_s.gsub('.', '_').humanize - attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) - - I18n.t(:"errors.format", { - :default => "%{attribute} %{message}", - :attribute => attr_name, - :message => message - }) - end - } + map { |attribute, message| full_message(attribute, message) } + end + + # Returns a full message for a given attribute. + # + # company.errors.full_message(:name, "is invalid") # => + # "Name is invalid" + def full_message(attribute, message) + return message if attribute == :base + attr_name = attribute.to_s.gsub('.', '_').humanize + attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) + I18n.t(:"errors.format", { + :default => "%{attribute} %{message}", + :attribute => attr_name, + :message => message + }) end # Translates an error message in its default scope |