From c5d62cb86d958aaa68a4057b3c2578329044e674 Mon Sep 17 00:00:00 2001 From: Ian Ker-Seymer Date: Mon, 23 Feb 2015 18:45:46 -0700 Subject: activemodel: make .model_name json encodable Previously, calling `User.model_name.to_json` would result in an infinite recursion as `.model_name` inherited its `.as_json` behavior from Object. This patch fixes that unexpected behavior by delegating `.as_json` to :name. --- activemodel/lib/active_model/naming.rb | 2 +- activemodel/test/cases/serializers/json_serialization_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'activemodel') diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 2bc3eeaa19..22010b517c 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -130,7 +130,7 @@ module ActiveModel # # Equivalent to +to_s+. delegate :==, :===, :<=>, :=~, :"!~", :eql?, :to_s, - :to_str, to: :name + :to_str, :as_json, to: :name # Returns a new ActiveModel::Name instance. By default, the +namespace+ # and +name+ option will take the namespace and name of the given class diff --git a/activemodel/test/cases/serializers/json_serialization_test.rb b/activemodel/test/cases/serializers/json_serialization_test.rb index e2eb91eeb0..d765a47636 100644 --- a/activemodel/test/cases/serializers/json_serialization_test.rb +++ b/activemodel/test/cases/serializers/json_serialization_test.rb @@ -195,4 +195,8 @@ class JsonSerializationTest < ActiveModel::TestCase assert_no_match %r{"awesome":}, json assert_no_match %r{"preferences":}, json end + + test "Class.model_name should be json encodable" do + assert_match %r{"Contact"}, Contact.model_name.to_json + end end -- cgit v1.2.3