瀏覽代碼

Patch ActiveRecord::Errors#full_messages so that it contains custom values error messages.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2518 e93f8b46-1217-0410-a6f0-8f06a7374b81
tags/0.9.0
Jean-Philippe Lang 15 年之前
父節點
當前提交
a64b8695c8
共有 2 個檔案被更改,包括 43 行新增0 行删除
  1. 30
    0
      config/initializers/10-patches.rb
  2. 13
    0
      test/unit/issue_test.rb

+ 30
- 0
config/initializers/10-patches.rb 查看文件

@@ -12,6 +12,36 @@ module ActiveRecord
end
end

module ActiveRecord
class Errors
def full_messages(options = {})
full_messages = []

@errors.each_key do |attr|
@errors[attr].each do |message|
next unless message

if attr == "base"
full_messages << message
elsif attr == "custom_values"
# Replace the generic "custom values is invalid"
# with the errors on custom values
@base.custom_values.each do |value|
value.errors.each do |attr, msg|
full_messages << value.custom_field.name + ' ' + msg
end
end
else
attr_name = @base.class.human_attribute_name(attr)
full_messages << attr_name + ' ' + message
end
end
end
full_messages
end
end
end

module ActionView
module Helpers
module DateHelper

+ 13
- 0
test/unit/issue_test.rb 查看文件

@@ -63,6 +63,19 @@ class IssueTest < Test::Unit::TestCase
assert_equal 'PostgreSQL', issue.custom_value_for(field).value
end
def test_errors_full_messages_should_include_custom_fields_errors
field = IssueCustomField.find_by_name('Database')
issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :subject => 'test_create', :description => 'IssueTest#test_create_with_required_custom_field')
assert issue.available_custom_fields.include?(field)
# Invalid value
issue.custom_field_values = { field.id => 'SQLServer' }
assert !issue.valid?
assert_equal 1, issue.errors.full_messages.size
assert_equal "Database #{I18n.translate('activerecord.errors.messages.inclusion')}", issue.errors.full_messages.first
end
def test_update_issue_with_required_custom_field
field = IssueCustomField.find_by_name('Database')
field.update_attribute(:is_required, true)

Loading…
取消
儲存