From e317521e5095c83b7bfab12df8789034260cff51 Mon Sep 17 00:00:00 2001 From: Aaron Stewart Date: Tue, 9 Aug 2011 11:17:55 -0700 Subject: [PATCH 1/4] Fix bug requiring empty hash passed to last call of chained relationships. --- lib/dm-serializer/to_json.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/dm-serializer/to_json.rb b/lib/dm-serializer/to_json.rb index febbde3..42f1d08 100644 --- a/lib/dm-serializer/to_json.rb +++ b/lib/dm-serializer/to_json.rb @@ -40,6 +40,7 @@ def as_json(options = {}) # #to_yaml if options[:relationships] options[:relationships].each do |relationship_name, opts| + opts ||= {} if respond_to?(relationship_name) result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false)) end From 2d1dee5b1e45cf5d6378c262a1833f5e19844847 Mon Sep 17 00:00:00 2001 From: Aaron Stewart Date: Tue, 9 Aug 2011 11:41:39 -0700 Subject: [PATCH 2/4] Added test for empty hash requirement in :relationships => to_json check. --- spec/public/to_json_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/public/to_json_spec.rb b/spec/public/to_json_spec.rb index e3a8919..aca97c0 100644 --- a/spec/public/to_json_spec.rb +++ b/spec/public/to_json_spec.rb @@ -69,6 +69,12 @@ def deserialize(result) expect { Cow.new.as_json(nil) }.to_not raise_error end + it "handles nil for :relationships => options" do + # This is to prevent :relationships having to be called as: + # cow.to_json(:relationships => {:baby_cows => {}}) + expect { Cow.new.as_json(:relationships => [:baby_cows])}.to_not raise_error + end + it "serializes Discriminator types as strings" do Motorcycle.new.as_json[:type].should == "Motorcycle" end From c66e87cd23ffbb32c4f7a3e5359718a6d6fc897a Mon Sep 17 00:00:00 2001 From: Aaron Stewart Date: Tue, 9 Aug 2011 12:03:57 -0700 Subject: [PATCH 3/4] Added signature to to_json, now callable like thing.to_json(:relationships => :foo) --- lib/dm-serializer/to_json.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/dm-serializer/to_json.rb b/lib/dm-serializer/to_json.rb index 42f1d08..ca764ae 100644 --- a/lib/dm-serializer/to_json.rb +++ b/lib/dm-serializer/to_json.rb @@ -39,7 +39,11 @@ def as_json(options = {}) # TODO: This needs tests and also needs to be ported to #to_xml and # #to_yaml if options[:relationships] - options[:relationships].each do |relationship_name, opts| + # check for symbol or string argument, and splat-collect if we have one + rel = options[:relationships] + rel = *rel if (rel.kind_of?(Symbol) || rel.kind_of?(String)) + rel.each do |relationship_name, opts| + # opts will be null from splat, or with nested :relationships opts ||= {} if respond_to?(relationship_name) result[relationship_name] = __send__(relationship_name).to_json(opts.merge(:to_json => false)) From 663718f4207e31774443297794faf01f0096639b Mon Sep 17 00:00:00 2001 From: Aaron Stewart Date: Tue, 9 Aug 2011 12:04:30 -0700 Subject: [PATCH 4/4] Added crude spec for new to_json signature --- spec/public/to_json_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/public/to_json_spec.rb b/spec/public/to_json_spec.rb index aca97c0..a610671 100644 --- a/spec/public/to_json_spec.rb +++ b/spec/public/to_json_spec.rb @@ -75,6 +75,11 @@ def deserialize(result) expect { Cow.new.as_json(:relationships => [:baby_cows])}.to_not raise_error end + it "handles string and symbol arguments for :relationships" do + expect { Cow.new.as_json(:relationships => :baby_cows)}.to_not raise_error + expect { Cow.new.as_json(:relationships => 'baby_cows')}.to_not raise_error + end + it "serializes Discriminator types as strings" do Motorcycle.new.as_json[:type].should == "Motorcycle" end