Skip to content

Commit

Permalink
Merge pull request #45872 from the-spectator/correct_hwia_encoding
Browse files Browse the repository at this point in the history
Fixes #45868 by Using #to_hash to serialize `AS::HWIA` for stored attributes
  • Loading branch information
byroot committed Aug 24, 2022
2 parents 61733bd + 6ac4ee9 commit aa85e89
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
9 changes: 1 addition & 8 deletions activerecord/lib/active_record/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,7 @@ def self.as_indifferent_hash(obj)

private
def as_regular_hash(obj)
case obj
when ActiveSupport::HashWithIndifferentAccess
obj.to_h
when Hash
obj
else
{}
end
obj.respond_to?(:to_hash) ? obj.to_hash : {}
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions activerecord/test/cases/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ class StoreTest < ActiveRecord::TestCase
assert_equal "heavy", @john.json_data["weight"]
end

test "serialize stored nested attributes" do
user = Admin::User.find_by_name("Jamis")
user.update(settings: { "color" => { "jenny" => "blue" }, homepage: "rails" })

assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)
assert_equal "blue", user.settings[:color][:jenny]
assert_equal "blue", user.color[:jenny]
end

def test_convert_store_attributes_from_Hash_to_HashWithIndifferentAccess_saving_the_data_and_access_attributes_indifferently
user = Admin::User.find_by_name("Jamis")
assert_equal "symbol", user.settings[:symbol]
Expand Down

0 comments on commit aa85e89

Please sign in to comment.