Permalink
Browse files

Merge pull request #13557 from gmarik/patch-1

Use `Array#wrap` instead `Array()`
  • Loading branch information...
1 parent 6612de4 commit 20b12c9ea68499570da5143f3196c7e58bd81416 @rafaelfranca rafaelfranca committed Jan 1, 2014
@@ -1,3 +1,7 @@
+* Fix presence validator for association when the associated record responds to `to_a`.
+
+ *gmarik*
+
* Fix bug in `belongs_to` association with `touch: true`.
Associated records are no longer touched, when they are already destroyed.
@@ -5,7 +5,7 @@ def validate(record)
super
attributes.each do |attribute|
next unless record.class.reflect_on_association(attribute)
- associated_records = Array(record.send(attribute))
+ associated_records = Array.wrap(record.send(attribute))
# Superclass validates presence. Ensure present records aren't about to be destroyed.
if associated_records.present? && associated_records.all? { |r| r.marked_for_destruction? }
@@ -3,6 +3,8 @@
require 'models/man'
require 'models/face'
require 'models/interest'
+require 'models/speedometer'
+require 'models/dashboard'
class PresenceValidationTest < ActiveRecord::TestCase
class Boy < Man; end
@@ -48,4 +50,18 @@ def test_validates_presence_of_has_many_marked_for_destruction
i2.mark_for_destruction
assert b.invalid?
end
+
+ def test_validates_presence_doesnt_convert_to_array
+ Speedometer.validates_presence_of :dashboard
+
+ dash = Dashboard.new
+
+ # dashboard has to_a method
+ def dash.to_a; ['(/)', '(\)']; end
+
+ s = Speedometer.new
+ s.dashboard = dash
+
+ assert_nothing_raised { s.valid? }
+ end
end

0 comments on commit 20b12c9

Please sign in to comment.