This commit is contained in:
Mike Burns 2011-12-09 16:39:20 -05:00
commit 25c2623bb9
3 changed files with 101 additions and 5 deletions

View File

@ -37,7 +37,7 @@ module Shoulda # :nodoc:
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?

View File

@ -16,11 +16,19 @@ module Shoulda # :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

View File

@ -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] = 'Dont 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 attributes 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 attributes error exists" do
context "and the translation for the models 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 models 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