Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Bug fix and improvement to handling of :relationships in to_json method #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/dm-serializer/to_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ 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))
end
Expand Down
11 changes: 11 additions & 0 deletions spec/public/to_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ 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 "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
Expand Down