Skip to content

Instantly share code, notes, and snippets.

@kurko
Created March 27, 2015 14:33
Show Gist options
  • Save kurko/fa4944e5adbd1b10b70c to your computer and use it in GitHub Desktop.
Save kurko/fa4944e5adbd1b10b70c to your computer and use it in GitHub Desktop.
diff --cc lib/active_model/serializer/adapter/json_api.rb
index f604b67,cd8de8e..0000000
--- a/lib/active_model/serializer/adapter/json_api.rb
+++ b/lib/active_model/serializer/adapter/json_api.rb
@@@ -16,18 -17,19 +17,33 @@@ module ActiveMode
end
def serializable_hash(options = {})
+ @root = (@options[:root] || serializer.json_key.to_s.pluralize).to_sym
+
if serializer.respond_to?(:each)
++<<<<<<< HEAD
+ @hash[@root] = serializer.map do |s|
+ self.class.new(s, @options.merge(top: @top, fieldset: @fieldset)).serializable_hash[@root]
+ end
+ else
+ @hash = cached_object do
+ @hash[@root] = attributes_for_serializer(serializer, @options)
+ add_resource_links(@hash[@root], serializer)
+ @hash
+ end
++=======
+ serializer.each do |s|
+ result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
+ @hash[:data] << result[:data]
+
+ if result[:included]
+ @hash[:included] ||= []
+ @hash[:included] |= result[:included]
+ end
+ end
+ else
+ @hash[:data] = attributes_for_serializer(serializer, @options)
+ add_resource_links(@hash[:data], serializer)
++>>>>>>> joaomdmoura/fragment-cache
end
@hash
end
@@@ -35,33 -42,17 +56,33 @@@
private
def add_links(resource, name, serializers)
+ type = serialized_object_type(serializers)
resource[:links] ||= {}
- resource[:links][name] ||= { linkage: [] }
- resource[:links][name][:linkage] += serializers.map { |serializer| { type: serializer.type, id: serializer.id.to_s } }
+
+ if name.to_s == type || !type
+ resource[:links][name] ||= []
+ resource[:links][name] += serializers.map{|serializer| serializer.id.to_s }
+ else
+ resource[:links][name] ||= {}
+ resource[:links][name][:type] = type
+ resource[:links][name][:ids] ||= []
+ resource[:links][name][:ids] += serializers.map{|serializer| serializer.id.to_s }
+ end
end
- def add_link(resource, name, serializer)
+ def add_link(resource, name, serializer, val=nil)
resource[:links] ||= {}
- resource[:links][name] = { linkage: nil }
+ resource[:links][name] = nil
if serializer && serializer.object
- resource[:links][name][:linkage] = { type: serializer.type, id: serializer.id.to_s }
+ type = serialized_object_type(serializer)
+ if name.to_s == type || !type
+ resource[:links][name] = serializer.id.to_s
+ else
+ resource[:links][name] ||= {}
+ resource[:links][name][:type] = type
+ resource[:links][name][:id] = serializer.id.to_s
+ end
end
end
@@@ -70,17 -61,15 +91,26 @@@
resource_path = [parent, resource_name].compact.join('.')
++<<<<<<< HEAD
+ if include_assoc?(resource_path) && resource_type = serialized_object_type(serializers)
+ plural_name = resource_type.pluralize.to_sym
+ @top[:linked] ||= {}
+ @top[:linked][plural_name] ||= []
++=======
+ if include_assoc?(resource_path)
+ @hash[:included] ||= []
++>>>>>>> joaomdmoura/fragment-cache
serializers.each do |serializer|
attrs = attributes_for_serializer(serializer, @options)
- add_resource_links(attrs, serializer, add_included: false)
+ add_resource_links(attrs, serializer, add_linked: false)
++<<<<<<< HEAD
+ @top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs)
++=======
+ @hash[:included].push(attrs) unless @hash[:included].include?(attrs)
++>>>>>>> joaomdmoura/fragment-cache
end
end
@@@ -97,16 -85,22 +126,33 @@@
result = []
serializer.each do |object|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
++<<<<<<< HEAD
+ attributes = object.attributes(options)
+ attributes[:id] = attributes[:id].to_s if attributes[:id]
+ result << attributes
+ end
+ else
+ options[:fields] = @fieldset && @fieldset.fields_for(serializer)
+ result = serializer.attributes(options)
+ result[:id] = result[:id].to_s if result[:id]
++=======
+ result << cache_check(object) do
+ options[:required_fields] = [:id, :type]
+ attributes = object.attributes(options)
+ attributes[:id] = attributes[:id].to_s
+ result << attributes
+ end
+ end
+ else
+ options[:fields] = @fieldset && @fieldset.fields_for(serializer)
+ options[:required_fields] = [:id, :type]
+ result = cache_check(serializer) do
+ result = serializer.attributes(options)
+ result[:id] = result[:id].to_s
+ result
+ end
++>>>>>>> joaomdmoura/fragment-cache
end
-
result
end
@@@ -147,12 -131,16 +193,16 @@@
if association.respond_to?(:each)
add_links(attrs, name, association)
else
- add_link(attrs, name, association)
+ if opts[:virtual_value]
+ add_link(attrs, name, nil, opts[:virtual_value])
+ else
+ add_link(attrs, name, association)
+ end
end
- if options[:add_included]
+ if @options[:embed] != :ids && options[:add_linked]
Array(association).each do |association|
- add_included(name, association)
+ add_linked(name, association)
end
end
end
diff --cc test/adapter/json_api/has_one_test.rb
index 247bb2f,170caf8..0000000
--- a/test/adapter/json_api/has_one_test.rb
+++ b/test/adapter/json_api/has_one_test.rb
@@@ -35,7 -37,20 +35,24 @@@ module ActiveMode
def test_includes_linked_bio
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')
++<<<<<<< HEAD
+ assert_equal([{id: "43", :content=>"AMS Contributor", :links=>{:author=>"1"}}], @adapter.serializable_hash[:linked][:bios])
++=======
+
+ expected = [
+ {
+ id: "43",
+ rating: nil,
+ type: "bios",
+ content:"AMS Contributor",
+ links: {
+ author: { linkage: { type: "authors", id: "1" } }
+ }
+ }
+ ]
+
+ assert_equal(expected, @adapter.serializable_hash[:included])
++>>>>>>> joaomdmoura/fragment-cache
end
end
end
diff --cc test/adapter/json_api/linked_test.rb
index 7289723,b5b372e..0000000
--- a/test/adapter/json_api/linked_test.rb
+++ b/test/adapter/json_api/linked_test.rb
@@@ -51,80 -51,82 +51,139 @@@ module ActiveMode
)
expected = {
++<<<<<<< HEAD
+ linked: {
+ comments: [
+ {
+ id: "1",
+ body: "ZOMG A COMMENT",
+ links: {
+ post: "1",
+ author: nil
+ }
+ }, {
+ id: "2",
+ body: "ZOMG ANOTHER COMMENT",
+ links: {
+ post: "1",
+ author: nil
+ }
+ }
+ ],
+ authors: [
+ {
+ id: "1",
+ name: "Steve K.",
+ links: {
+ posts: ["1", "3"],
+ roles: [],
+ bio: "1"
+ }
+ }, {
+ id: "2",
+ name: "Tenderlove",
+ links: {
+ posts: ["2"],
+ roles: [],
+ bio: "2"
+ }
+ }
+ ],
+ bios: [
+ {
+ id: "1",
+ content: "AMS Contributor",
+ links: {
+ author: "1"
+ }
+ }, {
+ id: "2",
+ content: "Rails Contributor",
+ links: {
+ author: "2"
+ }
+ }
+ ]
+ },
+ posts: [
++=======
+ data: [
++>>>>>>> joaomdmoura/fragment-cache
{
id: "10",
title: "Hello!!",
body: "Hello, world!!",
+ type: "posts",
links: {
- comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
- blog: { linkage: { type: "blogs", id: "999" } },
- author: { linkage: { type: "authors", id: "1" } }
+ comments: ['1', '2'],
+ blog: "999",
+ author: "1"
}
},
{
- id: "2",
+ id: "20",
title: "New Post",
body: "Body",
+ type: "posts",
links: {
- comments: { linkage: [] },
- blog: { linkage: { type: "blogs", id: "999" } },
- author: { linkage: { type: "authors", id: "2" } }
+ comments: [],
+ blog: "999",
+ author: "2"
}
}
+ ],
+ included: [
+ {
+ id: "1",
+ body: "ZOMG A COMMENT",
+ type: "comments",
+ links: {
+ post: { linkage: { type: "posts", id: "10" } },
+ author: { linkage: nil }
+ }
+ }, {
+ id: "2",
+ body: "ZOMG ANOTHER COMMENT",
+ type: "comments",
+ links: {
+ post: { linkage: { type: "posts", id: "10" } },
+ author: { linkage: nil }
+ }
+ }, {
+ id: "1",
+ name: "Steve K.",
+ type: "authors",
+ links: {
+ posts: { linkage: [ { type: "posts", id: "10" }, { type: "posts", id: "30" } ] },
+ roles: { linkage: [] },
+ bio: { linkage: { type: "bios", id: "1" } }
+ }
+ }, {
+ id: "1",
+ rating: nil,
+ type: "bios",
+ content: "AMS Contributor",
+ links: {
+ author: { linkage: { type: "authors", id: "1" } }
+ }
+ }, {
+ id: "2",
+ name: "Tenderlove",
+ type: "authors",
+ links: {
+ posts: { linkage: [ { type: "posts", id:"20" } ] },
+ roles: { linkage: [] },
+ bio: { linkage: { type: "bios", id: "2" } }
+ }
+ }, {
+ id: "2",
+ rating: nil,
+ type: "bios",
+ content: "Rails Contributor",
+ links: {
+ author: { linkage: { type: "authors", id: "2" } }
+ }
+ }
]
}
assert_equal expected, adapter.serializable_hash
diff --cc test/fixtures/poro.rb
index d04d79d,b66395c..0000000
--- a/test/fixtures/poro.rb
+++ b/test/fixtures/poro.rb
@@@ -57,12 -57,17 +57,26 @@@ class ProfilePreviewSerializer < Active
urls :posts, :comments
end
++<<<<<<< HEAD
+Post = Class.new(Model)
+Comment = Class.new(Model)
+Author = Class.new(Model)
+Bio = Class.new(Model)
+Blog = Class.new(Model)
+Role = Class.new(Model)
++=======
+ Post = Class.new(Model)
+ Like = Class.new(Model)
+ Comment = Class.new(Model)
+ Author = Class.new(Model)
+ Bio = Class.new(Model)
+ Blog = Class.new(Model)
+ Role = Class.new(Model)
+ User = Class.new(Model)
+ Location = Class.new(Model)
+ Place = Class.new(Model)
+
++>>>>>>> joaomdmoura/fragment-cache
module Spam; end
Spam::UnrelatedLink = Class.new(Model)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment