Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented all possible i18n error messages for validations.
  • Loading branch information
clemenshelm committed Nov 17, 2011
1 parent 1effd72 commit 662b674
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/shoulda/matchers/active_model/allow_value_matcher.rb
Expand Up @@ -37,7 +37,7 @@ def with_message(message)
def matches?(instance)
@instance = instance
if Symbol === @expected_message
@expected_message = default_error_message(@expected_message)
@expected_message = default_error_message(@expected_message, :model_name => @instance.class.to_s.underscore, :attribute => @attribute)
end
@instance.send("#{@attribute}=", @value)
!errors_match?
Expand Down
16 changes: 12 additions & 4 deletions lib/shoulda/matchers/active_model/helpers.rb
Expand Up @@ -16,11 +16,19 @@ def pretty_error_messages(obj) # :nodoc:
# default_error_message(:blank)
# default_error_message(:too_short, :count => 5)
# default_error_message(:too_long, :count => 60)
def default_error_message(key, values = {})
if Object.const_defined?(:I18n) # Rails >= 2.2
I18n.translate(:"activerecord.errors.messages.#{key}", {:default => :"errors.messages.#{key}"}.merge(values))
# default_error_message(:blank, :model_name => 'user', :attribute => 'name')
def default_error_message(key, options = {})
model_name = options.delete(:model_name)
attribute = options.delete(:attribute)
if Object.const_defined?(:I18n) # Rails >= 2.2
I18n.translate( :"activerecord.errors.models.#{model_name}.attributes.#{attribute}.#{key}", {
:default => [ :"activerecord.errors.models.#{model_name}.#{key}",
:"activerecord.errors.messages.#{key}",
:"errors.attributes.#{attribute}.#{key}",
:"errors.messages.#{key}"
]}.merge(options))
else # Rails <= 2.1.x
::ActiveRecord::Errors.default_error_messages[key] % values[:count]
::ActiveRecord::Errors.default_error_messages[key] % options[:count]
end
end
end
Expand Down
88 changes: 88 additions & 0 deletions spec/shoulda/active_model/helpers_spec.rb
@@ -0,0 +1,88 @@
# encoding: UTF-8
require "spec_helper"

include Shoulda::Matchers::ActiveModel

def store_translations(options = {:without => []})
options[:without] ||= []
options[:without] = [options[:without]] unless options[:without].is_a? Array

translations = {
:activerecord => {
:errors => {
:models => {
:example => {
:attributes => {
:attr => {}
}
}
},
:messages => {}
}
},
:errors => {
:attributes => {
:attr => {}
},
:messages => {}
}}
translations[:activerecord][:errors][:models][:example][:attributes][:attr][:blank] = 'Don’t you do that to me!' unless options[:without].include?(:model_attribute)
translations[:activerecord][:errors][:models][:example][:blank] = 'Give it one more try!' unless options[:without].include?(:model)
translations[:activerecord][:errors][:messages][:blank] = 'Oh no!' unless options[:without].include?(:message)
translations[:errors][:attributes][:attr][:blank] = 'Seriously?' unless options[:without].include?(:attribute)

I18n.backend.store_translations :en, translations
end

describe Shoulda::Matchers::ActiveModel::Helpers do
describe "default_error_message" do
before do
define_model :example, :attr => :string do
validates_presence_of :attr
end
@model = Example.new
end

after { I18n.backend.reload! }

context "if the translation for the model attribute’s error exists" do
it "provides the right error message" do
store_translations
@model.should validate_presence_of(:attr)
end
end

context "if no translation for the model attribute’s error exists" do
context "and the translation for the model’s error exists" do
it "provides the right error message" do
store_translations :without => :model_attribute
@model.should validate_presence_of(:attr)
end
end

context "and no translation for the model’s error exists" do
context "and the translation for the message exists" do
it "provides the right error message" do
store_translations :without => [:model_attribute, :model]
@model.should validate_presence_of(:attr)
end
end

context "and no translation for the message exists" do
context "and the translation for the attribute exists" do
it "provides the right error message" do
store_translations :without => [:model_attribute, :model, :message]
@model.should validate_presence_of(:attr)
end
end

context "and no translation for the attribute exists" do
it "provides the general error message" do
@model.should validate_presence_of(:attr)
end
end
end
end
end
end
end

0 comments on commit 662b674

Please sign in to comment.