From 1b75b94de6d474d56a6c47e74bdbd985ec14087b Mon Sep 17 00:00:00 2001 From: Andrew White Date: Wed, 23 Jan 2013 14:54:56 +0000 Subject: Remove warning by using a custom coder The native JSON library bypasses the `to_json` overrides in active_support/core_ext/object/to_json.rb by calling its native implementation directly. However `ActiveRecord::Store` uses a HWIA so `JSON.dump` will call our `to_json` instead with a `State` object for options rather than a `Hash`. This generates a warning when the `:encoding`, `:only` & `:except` keys are accessed in `Hash#as_json` because the `State` object delegates unknown keys to `instance_variable_get` in its `:[]` method. Workaround this warning in the test by using a custom coder that calls `ActiveSupport::JSON.encode` directly. --- activerecord/test/models/admin/user.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/activerecord/test/models/admin/user.rb b/activerecord/test/models/admin/user.rb index 467f3ccd39..024fede266 100644 --- a/activerecord/test/models/admin/user.rb +++ b/activerecord/test/models/admin/user.rb @@ -1,10 +1,24 @@ class Admin::User < ActiveRecord::Base + class Coder + def initialize(default = {}) + @default = default + end + + def dump(o) + ActiveSupport::JSON.encode(o || @default) + end + + def load(s) + s.present? ? ActiveSupport::JSON.decode(s) : @default.clone + end + end + belongs_to :account store :settings, :accessors => [ :color, :homepage ] store_accessor :settings, :favorite_food store :preferences, :accessors => [ :remember_login ] - store :json_data, :accessors => [ :height, :weight ], :coder => JSON - store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => JSON + store :json_data, :accessors => [ :height, :weight ], :coder => Coder.new + store :json_data_empty, :accessors => [ :is_a_good_guy ], :coder => Coder.new def phone_number read_store_attribute(:settings, :phone_number).gsub(/(\d{3})(\d{3})(\d{4})/,'(\1) \2-\3') -- cgit v1.2.3