forked from zerohistory/toystore
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated object and association serialization.
Moved object serialization into Toy::Object and included association serialization in Toy::Store.
- Loading branch information
1 parent
162f141
commit d943155
Showing
7 changed files
with
166 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,50 @@ | |||
module Toy | |||
module AssociationSerialization | |||
extend ActiveSupport::Concern | |||
include Serialization | |||
|
|||
def serializable_hash(options = nil) | |||
options ||= {} | |||
super.tap { |hash| | |||
serializable_add_includes(options) do |association, records, opts| | |||
hash[association] = records.is_a?(Enumerable) ? | |||
records.map { |r| r.serializable_hash(opts) } : | |||
records.serializable_hash(opts) | |||
end | |||
} | |||
end | |||
|
|||
private | |||
|
|||
# Add associations specified via the <tt>:includes</tt> option. | |||
# Expects a block that takes as arguments: | |||
# +association+ - name of the association | |||
# +records+ - the association record(s) to be serialized | |||
# +opts+ - options for the association records | |||
def serializable_add_includes(options = {}) | |||
return unless include_associations = options.delete(:include) | |||
|
|||
base_only_or_except = { :except => options[:except], | |||
:only => options[:only] } | |||
|
|||
include_has_options = include_associations.is_a?(Hash) | |||
associations = include_has_options ? include_associations.keys : Array.wrap(include_associations) | |||
|
|||
for association in associations | |||
records = if self.class.list?(association) | |||
send(association).to_a | |||
elsif self.class.reference?(association) || self.class.parent_reference?(association) | |||
send(association) | |||
end | |||
|
|||
unless records.nil? | |||
association_options = include_has_options ? include_associations[association] : base_only_or_except | |||
opts = options.merge(association_options) | |||
yield(association, records, opts) | |||
end | |||
end | |||
|
|||
options[:include] = include_associations | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,103 @@ | |||
require 'helper' | |||
|
|||
describe Toy::AssociationSerialization do | |||
uses_constants('User', 'Game', 'Move') | |||
|
|||
before do | |||
User.attribute :name, String | |||
User.attribute :age, Integer | |||
end | |||
|
|||
describe "serializing relationships" do | |||
before do | |||
User.list :games, :inverse_of => :user | |||
Game.reference :user | |||
end | |||
|
|||
it "should include references" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
|
|||
MultiJson.load(game.to_json(:include => [:user])).should == { | |||
'game' => { | |||
'id' => game.id, | |||
'user_id' => user.id, | |||
'user' => { | |||
'name' => 'John', | |||
'game_ids' => [game.id], | |||
'id' => user.id, | |||
'age' => 28, | |||
} | |||
} | |||
} | |||
end | |||
|
|||
it "should include lists" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
MultiJson.load(user.to_json(:include => [:games])).should == { | |||
'user' => { | |||
'name' => 'John', | |||
'game_ids' => [game.id], | |||
'id' => user.id, | |||
'age' => 28, | |||
'games' => [{'id' => game.id, 'user_id' => user.id}], | |||
} | |||
} | |||
end | |||
|
|||
it "should not cause circular reference JSON errors for references" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
|
|||
MultiJson.load(ActiveSupport::JSON.encode(game.user)).should == { | |||
'user' => { | |||
'name' => 'John', | |||
'game_ids' => [game.id], | |||
'id' => user.id, | |||
'age' => 28 | |||
} | |||
} | |||
end | |||
|
|||
it "should not cause circular reference JSON errors for references when called indirectly" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
|
|||
MultiJson.load(ActiveSupport::JSON.encode([game.user])).should == [ | |||
'user' => { | |||
'name' => 'John', | |||
'game_ids' => [game.id], | |||
'id' => user.id, | |||
'age' => 28 | |||
} | |||
] | |||
end | |||
|
|||
it "should not cause circular reference JSON errors for lists" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
|
|||
MultiJson.load(ActiveSupport::JSON.encode(user.games)).should == [{ | |||
'game' => { | |||
'id' => game.id, | |||
'user_id' => user.id | |||
} | |||
}] | |||
end | |||
|
|||
it "should not cause circular reference JSON errors for lists when called indirectly" do | |||
user = User.create(:name => 'John', :age => 28) | |||
game = user.games.create | |||
|
|||
MultiJson.load(ActiveSupport::JSON.encode({:games => user.games})).should == { | |||
'games' => [{ | |||
'game' => { | |||
'id' => game.id, | |||
'user_id' => user.id | |||
} | |||
}] | |||
} | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters