aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2016-03-24 14:54:02 -0600
committerSean Griffin <sean@seantheprogrammer.com>2016-03-24 14:58:23 -0600
commitba06dab5451cdce9c2264abd6c9dd20cb2722c8d (patch)
tree75bd8d2c153462db7a845c57dbaa31423701ba18 /activerecord/test
parent133befb59b8389cb483fd4194acdcddc5e288ab4 (diff)
downloadrails-ba06dab5451cdce9c2264abd6c9dd20cb2722c8d.tar.gz
rails-ba06dab5451cdce9c2264abd6c9dd20cb2722c8d.tar.bz2
rails-ba06dab5451cdce9c2264abd6c9dd20cb2722c8d.zip
Memoize user provided defaults before type casting
When a proc is given as a default value, the form builder ends up displaying `Proc#to_s` when the default is used. That's because we didn't handle the proc until type casting. This issue technically can occur any time that a proc is the value before type casting, but in reality the only place that will occur is when a proc default is provided through the attributes API, so the best place to handle this edge case is there. I've opted to memoize instead of just moving the `Proc#call` up, as this made me realize that it could potentially interact very poorly with dirty checking. The code here is a little redundant, but I don't want to rely on how `value_before_type_cast` is implemented in the super class, even if it's just an `attr_reader`. Fixes #24249 Close #24306
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/attributes_test.rb11
1 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb
index 2991ca8b76..2bebbfa205 100644
--- a/activerecord/test/cases/attributes_test.rb
+++ b/activerecord/test/cases/attributes_test.rb
@@ -135,6 +135,17 @@ module ActiveRecord
assert_equal 2, klass.new.counter
end
+ test "procs are memoized before type casting" do
+ klass = Class.new(OverloadedType) do
+ @@counter = 0
+ attribute :counter, :integer, default: -> { @@counter += 1 }
+ end
+
+ model = klass.new
+ assert_equal 1, model.counter_before_type_cast
+ assert_equal 1, model.counter_before_type_cast
+ end
+
test "user provided defaults are persisted even if unchanged" do
model = OverloadedType.create!