mirror of
https://github.com/github/rails.git
synced 2026-01-10 07:07:54 -05:00
Merge pull request #3319 from martinsvalin/active_model_errors_fixes
ActiveModel::Errors#generate_message without i18n_scope, and more test cases for #add
This commit is contained in:
@@ -300,13 +300,17 @@ module ActiveModel
|
||||
def generate_message(attribute, type = :invalid, options = {})
|
||||
type = options.delete(:message) if options[:message].is_a?(Symbol)
|
||||
|
||||
defaults = @base.class.lookup_ancestors.map do |klass|
|
||||
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
||||
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
||||
if @base.class.respond_to?(:i18n_scope)
|
||||
defaults = @base.class.lookup_ancestors.map do |klass|
|
||||
[ :"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute}.#{type}",
|
||||
:"#{@base.class.i18n_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}" ]
|
||||
end
|
||||
else
|
||||
defaults = []
|
||||
end
|
||||
|
||||
defaults << options.delete(:message)
|
||||
defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}"
|
||||
defaults << :"#{@base.class.i18n_scope}.errors.messages.#{type}" if @base.class.respond_to?(:i18n_scope)
|
||||
defaults << :"errors.attributes.#{attribute}.#{type}"
|
||||
defaults << :"errors.messages.#{type}"
|
||||
|
||||
|
||||
@@ -66,6 +66,20 @@ class ErrorsTest < ActiveModel::TestCase
|
||||
assert_equal ["can not be blank"], person.errors[:name]
|
||||
end
|
||||
|
||||
test "should be able to add an error with a symbol" do
|
||||
person = Person.new
|
||||
person.errors.add(:name, :blank)
|
||||
message = person.errors.generate_message(:name, :blank)
|
||||
assert_equal [message], person.errors[:name]
|
||||
end
|
||||
|
||||
test "should be able to add an error with a proc" do
|
||||
person = Person.new
|
||||
message = Proc.new { "can not be blank" }
|
||||
person.errors.add(:name, message)
|
||||
assert_equal ["can not be blank"], person.errors[:name]
|
||||
end
|
||||
|
||||
test 'should respond to size' do
|
||||
person = Person.new
|
||||
person.errors.add(:name, "can not be blank")
|
||||
@@ -112,5 +126,12 @@ class ErrorsTest < ActiveModel::TestCase
|
||||
assert_equal ["is invalid"], hash[:email]
|
||||
end
|
||||
|
||||
test "generate_message should work without i18n_scope" do
|
||||
person = Person.new
|
||||
assert !Person.respond_to?(:i18n_scope)
|
||||
assert_nothing_raised {
|
||||
person.errors.generate_message(:name, :blank)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user