Merge pull request #42855 from kamipo/fix_to_json

Fix `to_json` after `changes_applied` for `ActiveModel::Dirty` object
This commit is contained in:
Ryuta Kamizono 2021-07-24 08:38:23 +09:00 committed by GitHub
commit 2a6459d8ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 3 deletions

View File

@ -141,7 +141,7 @@ module ActiveModel
end
def as_json(options = {}) # :nodoc:
options[:except] = [options[:except], "mutations_from_database"].flatten
options[:except] = [*options[:except], "mutations_from_database", "mutations_before_last_save"]
super(options)
end

View File

@ -244,13 +244,19 @@ class DirtyTest < ActiveModel::TestCase
assert_equal "{\"name\":\"Dmitry\",\"color\":null,\"size\":null,\"status\":\"initialized\"}", @model.to_json
end
test "to_json should work on model with :except string option " do
test "to_json should work on model with :except string option" do
@model.name = "Dmitry"
assert_equal "{\"color\":null,\"size\":null,\"status\":\"initialized\"}", @model.to_json(except: "name")
end
test "to_json should work on model with :except array option " do
test "to_json should work on model with :except array option" do
@model.name = "Dmitry"
assert_equal "{\"color\":null,\"size\":null,\"status\":\"initialized\"}", @model.to_json(except: ["name"])
end
test "to_json should work on model after save" do
@model.name = "Dmitry"
@model.save
assert_equal "{\"name\":\"Dmitry\",\"color\":null,\"size\":null,\"status\":\"initialized\"}", @model.to_json
end
end