Permalink
Browse files

Merge pull request #13557 from gmarik/patch-1

Use `Array#wrap` instead `Array()`
  • Loading branch information...
2 parents 6fc59d3 + aaf1af3 commit a28f1c279355ac129bb1976d97a7012dd219cb77 @rafaelfranca rafaelfranca committed Jan 1, 2014
@@ -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,19 @@ 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 a28f1c2

Please sign in to comment.