From 0af36c62a5710e023402e37b019ad9982e69de4b Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 23 Jan 2018 14:08:46 -0700 Subject: Allow attributes with a proc default to be marshalled We don't implement much custom marshalling logic for these objects, but the proc default case needs to be handled separately. Unfortunately there's no way to just say "do what you would have done but with this value for one ivar", so we have to manually implement `marshal_load` as well. The test case is a little bit funky, but I'd really like an equality test in there, and there's no easy way to add one now that this is out of AR (since the `attributes` method isn't here) Fixes #31216 --- .../attribute/user_provided_default.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute/user_provided_default.rb b/activemodel/lib/active_model/attribute/user_provided_default.rb index f274b687d4..a5dc6188d2 100644 --- a/activemodel/lib/active_model/attribute/user_provided_default.rb +++ b/activemodel/lib/active_model/attribute/user_provided_default.rb @@ -22,6 +22,28 @@ module ActiveModel self.class.new(name, user_provided_value, type, original_attribute) end + def marshal_dump + result = [ + name, + value_before_type_cast, + type, + original_attribute, + ] + result << value if defined?(@value) + result + end + + def marshal_load(values) + name, user_provided_value, type, original_attribute, value = values + @name = name + @user_provided_value = user_provided_value + @type = type + @original_attribute = original_attribute + if values.length == 5 + @value = value + end + end + protected attr_reader :user_provided_value -- cgit v1.2.3