Skip to content

Commit

Permalink
Addded validation for validate all the associated objects before decl…
Browse files Browse the repository at this point in the history
…aring failure with validates_associated #618 [Tim Bates]. Added that validates_* now accept blocks to perform validations #618 [Tim Bates]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@650 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Feb 17, 2005
1 parent 8151c4e commit 0e764a5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
8 changes: 8 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
*SVN*

* Added that validates_* now accept blocks to perform validations #618 [Tim Bates]. Example:

class Person < ActiveRecord::Base
validate { |person| person.errors.add("title", "will never be valid") if SHOULD_NEVER_BE_VALID }
end

* Addded validation for validate all the associated objects before declaring failure with validates_associated #618 [Tim Bates]

* Added keyword-style approach to defining the custom relational bindings #545 [Jamis Buck]. Example:

class Project < ActiveRecord::Base
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def self.append_features(base) # :nodoc:
alias_method :update_attribute_without_validation_skipping, :update_attribute
alias_method :update_attribute, :update_attribute_with_validation_skipping

VALIDATIONS.each { |vd| base.class_eval("def self.#{vd}(*methods) write_inheritable_array(\"#{vd}\", methods - (read_inheritable_attribute(\"#{vd}\") || [])) end") }
VALIDATIONS.each { |vd| base.class_eval("def self.#{vd}(*methods, &block) write_inheritable_array(\"#{vd}\", methods + [block].compact - (read_inheritable_attribute(\"#{vd}\") || [])) end") }
end

base.extend(ClassMethods)
Expand Down Expand Up @@ -300,7 +300,7 @@ def validates_associated(*attr_names)
for attr_name in attr_names
class_eval(%(#{validation_method(configuration[:on])} %{
errors.add("#{attr_name}", "#{configuration[:message]}") unless
(#{attr_name}.is_a?(Array) ? #{attr_name} : [#{attr_name}]).inject(true){ |memo, record| memo and (record.nil? or record.valid?) }
(#{attr_name}.is_a?(Array) ? #{attr_name} : [#{attr_name}]).inject(true){ |memo, record| (record.nil? or record.valid?) and memo }
}))
end
end
Expand Down Expand Up @@ -378,7 +378,7 @@ def run_validations(validation_method)
eval(validation, binding)
elsif validation_block?(validation)
validation.call(self)
elsif filter_class?(validation, validation_method)
elsif validation_class?(validation, validation_method)
validation.send(validation_method, self)
else
raise(
Expand Down
19 changes: 17 additions & 2 deletions activerecord/test/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,12 @@ def test_validates_length_of_custom_errors_for_is_with_wrong_length
def test_validates_associated_many
Topic.validates_associated( :replies )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
t.replies << [r = Reply.create("title" => "A reply"), Reply.create("title" => "Another reply", "content" => "with content!")]
t.replies << [r = Reply.create("title" => "A reply"), r2 = Reply.create("title" => "Another reply")]
assert !t.valid?
assert t.errors.on(:replies)
r.content = "non-empty"
assert_equal 1, r.errors.count # make sure all associated objects have been validated
assert_equal 1, r2.errors.count
r.content = r2.content = "non-empty"
assert t.valid?
end

Expand All @@ -410,6 +412,19 @@ def test_validates_associated_one
assert r.valid?
end

def test_validate_block
Topic.validate { |topic| topic.errors.add("title", "will never be valid") }
t = Topic.create("title" => "Title", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
assert_equal "will never be valid", t.errors["title"]
end

def test_invalid_validator
Topic.validate 3
assert_raise(ActiveRecord::ActiveRecordError) { t = Topic.create }
end

def test_throw_away_typing
d = Developer.create "name" => "David", "salary" => "100,000"
assert !d.valid?
Expand Down

0 comments on commit 0e764a5

Please sign in to comment.