diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2010-01-19 13:40:26 +0530 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2010-01-19 13:40:26 +0530 |
commit | c71120e29caddda295c133adfb279870733a3f81 (patch) | |
tree | da81981de36bc146c36cae3bc73cb5020ba05919 /activemodel | |
parent | 087b67805e3785159cb4da524ad37782bd182b93 (diff) | |
parent | 71d67fc6bd504956bce66e274e6228dd00a814c1 (diff) | |
download | rails-c71120e29caddda295c133adfb279870733a3f81.tar.gz rails-c71120e29caddda295c133adfb279870733a3f81.tar.bz2 rails-c71120e29caddda295c133adfb279870733a3f81.zip |
Merge remote branch 'mainstream/master'
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/lib/active_model/errors.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/validations/validates.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/validations_test.rb | 8 |
3 files changed, 12 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 93935c0473..76e6ad93a7 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -169,9 +169,11 @@ module ActiveModel # If no +messsage+ is supplied, :invalid is assumed. # # If +message+ is a Symbol, it will be translated, using the appropriate scope (see translate_error). + # If +message+ is a Proc, it will be called, allowing for things like Time.now to be used within an error def add(attribute, message = nil, options = {}) message ||= :invalid message = generate_message(attribute, message, options) if message.is_a?(Symbol) + message = message.call if message.is_a?(Proc) self[attribute] << message end diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 4c82a993ae..90b244228a 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -1,3 +1,5 @@ +require 'active_support/core_ext/hash/slice' + module ActiveModel module Validations module ClassMethods diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 38a2a716a7..eb100d1c35 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -212,4 +212,12 @@ class ValidationsTest < ActiveModel::TestCase all_errors = t.errors.to_a assert_deprecated { assert_equal all_errors, t.errors.each_full{|err| err} } end + + def test_validation_with_message_as_proc + Topic.validates_presence_of(:title, :message => proc { "no blanks here".upcase }) + + t = Topic.new + assert !t.valid? + assert ["NO BLANKS HERE"], t.errors[:title] + end end |