Skip to content

Commit

Permalink
Deprecate marshalling load from legacy attributes format
Browse files Browse the repository at this point in the history
Since #31827, marshalling attributes hash format is changed to improve
performance because materializing lazy attribute hash is too expensive.

In that time, we had kept an ability to load from legacy attributes
format, since that performance improvement is backported to 5-1-stable
and 5-0-stable.

Now all supported versions will dump attributes as new format, the
backward compatibity should no longer be needed.
  • Loading branch information
kamipo committed May 2, 2020
1 parent 483330e commit f93b04a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions activemodel/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Deprecate marshalling load from legacy attributes format.

*Ryuta Kamizono*

* `*_previously_changed?` accepts `:from` and `:to` keyword arguments like `*_changed?`.

topic.update!(status: :archived)
Expand Down
3 changes: 3 additions & 0 deletions activemodel/lib/active_model/attribute_set/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def marshal_dump

def marshal_load(values)
if values.is_a?(Hash)
ActiveSupport::Deprecation.warn(<<~MSG.squish)
Marshalling load from legacy attributes format is deprecated and will be removed in Rails 6.2.
MSG
empty_hash = {}.freeze
initialize(empty_hash, empty_hash, empty_hash, empty_hash, values)
@materialized = true
Expand Down
5 changes: 3 additions & 2 deletions activemodel/test/cases/attribute_set_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,13 @@ def assert_valid_value(*)
attributes.instance_variable_get(:@attributes).instance_eval do
class << self
def marshal_dump
materialize
materialize # legacy marshal format before Rails 5.1
end
end
end

attributes = Marshal.load(Marshal.dump(attributes))
data = Marshal.dump(attributes)
attributes = assert_deprecated { Marshal.load(data) }
assert_equal({ foo: "1" }, attributes.to_hash)
end

Expand Down

0 comments on commit f93b04a

Please sign in to comment.