Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to_json after changes_applied for ActiveModel::Dirty object #42855

Merged
merged 1 commit into from Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -141,7 +141,7 @@ def initialize_dup(other) # :nodoc:
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

Expand Down
10 changes: 8 additions & 2 deletions activemodel/test/cases/dirty_test.rb
Expand Up @@ -244,13 +244,19 @@ def save
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