From a6e3cdae0ce1d05a6b9f7dff4499f6be3fbf63c2 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Thu, 28 May 2015 16:26:49 -0600 Subject: Allow proc defaults with the Attributes API This is a variant implementation of the changes proposed in #19914. Unlike that PR, the change in behavior is isolated in its own class. This is to prevent wonky behavior if a Proc is assigned outside of the default, and it is a natural place to place the behavior required by #19921 as well. Close #19914. [Sean Griffin & Kir Shatrov] --- activerecord/test/cases/attributes_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'activerecord/test/cases') diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 927d7950a5..4619293ac6 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -125,6 +125,16 @@ module ActiveRecord assert_equal "from user", model.wibble end + test "procs for default values" do + klass = Class.new(OverloadedType) do + @@counter = 0 + attribute :counter, :integer, default: -> { @@counter += 1 } + end + + assert_equal 1, klass.new.counter + assert_equal 2, klass.new.counter + end + if current_adapter?(:PostgreSQLAdapter) test "arrays types can be specified" do klass = Class.new(OverloadedType) do -- cgit v1.2.3