mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove deprecated support to YAML load ActiveRecord::Base
instance in the Rails 4.2 and 4.1 formats
This commit is contained in:
parent
99b54b6ded
commit
415709a76e
5 changed files with 13 additions and 50 deletions
|
@ -1,3 +1,7 @@
|
|||
* Remove deprecated support to YAML load `ActiveRecord::Base` instance in the Rails 4.2 and 4.1 formats.
|
||||
|
||||
*Rafael Mendonça França*
|
||||
|
||||
* Remove deprecated option `:spec_name` in the `configs_for` method.
|
||||
|
||||
*Rafael Mendonça França*
|
||||
|
|
|
@ -489,7 +489,7 @@ module ActiveRecord
|
|||
# post.init_with(coder)
|
||||
# post.title # => 'hello world'
|
||||
def init_with(coder, &block)
|
||||
coder = LegacyYamlAdapter.convert(self.class, coder)
|
||||
coder = LegacyYamlAdapter.convert(coder)
|
||||
attributes = self.class.yaml_encoder.decode(coder)
|
||||
init_with_attributes(attributes, coder["new_record"], &block)
|
||||
end
|
||||
|
|
|
@ -2,50 +2,13 @@
|
|||
|
||||
module ActiveRecord
|
||||
module LegacyYamlAdapter # :nodoc:
|
||||
def self.convert(klass, coder)
|
||||
def self.convert(coder)
|
||||
return coder unless coder.is_a?(Psych::Coder)
|
||||
|
||||
case coder["active_record_yaml_version"]
|
||||
when 1, 2 then coder
|
||||
else
|
||||
ActiveSupport::Deprecation.warn(<<-MSG.squish)
|
||||
YAML loading from legacy format older than Rails 5.0 is deprecated
|
||||
and will be removed in Rails 7.0.
|
||||
MSG
|
||||
if coder["attributes"].is_a?(ActiveModel::AttributeSet)
|
||||
Rails420.convert(klass, coder)
|
||||
else
|
||||
Rails41.convert(klass, coder)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Rails420 # :nodoc:
|
||||
def self.convert(klass, coder)
|
||||
attribute_set = coder["attributes"]
|
||||
|
||||
klass.attribute_names.each do |attr_name|
|
||||
attribute = attribute_set[attr_name]
|
||||
if attribute.type.is_a?(Delegator)
|
||||
type_from_klass = klass.type_for_attribute(attr_name)
|
||||
attribute_set[attr_name] = attribute.with_type(type_from_klass)
|
||||
end
|
||||
end
|
||||
|
||||
coder
|
||||
end
|
||||
end
|
||||
|
||||
module Rails41 # :nodoc:
|
||||
def self.convert(klass, coder)
|
||||
attributes = klass.attributes_builder
|
||||
.build_from_database(coder["attributes"])
|
||||
new_record = coder["attributes"][klass.primary_key].blank?
|
||||
|
||||
{
|
||||
"attributes" => attributes,
|
||||
"new_record" => new_record,
|
||||
}
|
||||
raise("Active Record doesn't know how to load YAML with this format.")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -112,25 +112,19 @@ class YamlSerializationTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_deserializing_rails_41_yaml
|
||||
topic = assert_deprecated do
|
||||
error = assert_raises(RuntimeError) do
|
||||
yaml_load(yaml_fixture("rails_4_1"))
|
||||
end
|
||||
|
||||
assert_predicate topic, :new_record?
|
||||
assert_nil topic.id
|
||||
assert_equal "The First Topic", topic.title
|
||||
assert_equal({ omg: :lol }, topic.content)
|
||||
assert_equal "Active Record doesn't know how to load YAML with this format.", error.message
|
||||
end
|
||||
|
||||
def test_deserializing_rails_4_2_0_yaml
|
||||
topic = assert_deprecated do
|
||||
error = assert_raises(RuntimeError) do
|
||||
yaml_load(yaml_fixture("rails_4_2_0"))
|
||||
end
|
||||
|
||||
assert_not_predicate topic, :new_record?
|
||||
assert_equal 1, topic.id
|
||||
assert_equal "The First Topic", topic.title
|
||||
assert_equal("Have a nice day", topic.content)
|
||||
assert_equal "Active Record doesn't know how to load YAML with this format.", error.message
|
||||
end
|
||||
|
||||
def test_yaml_encoding_keeps_mutations
|
||||
|
|
|
@ -104,6 +104,8 @@ Please refer to the [Changelog][active-record] for detailed changes.
|
|||
|
||||
* Remove deprecated option `:spec_name` in the `configs_for` method.
|
||||
|
||||
* Remove deprecated support to YAML load `ActiveRecord::Base` instance in the Rails 4.2 and 4.1 formats.
|
||||
|
||||
### Deprecations
|
||||
|
||||
### Notable changes
|
||||
|
|
Loading…
Reference in a new issue