Skip to content

Instantly share code, notes, and snippets.

@joaomdmoura
Created March 27, 2015 14:52
Show Gist options
  • Save joaomdmoura/1e1f22313b064babcce9 to your computer and use it in GitHub Desktop.
Save joaomdmoura/1e1f22313b064babcce9 to your computer and use it in GitHub Desktop.
Conflict solved
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)
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash
@hash[:data] << result[:data]
end
else
@hash[:data] = attributes_for_serializer(serializer, @options)
add_resource_links(@hash[:data], serializer)
end
@hash
end
@@@ -35,33 -42,17 +56,33 @@@
private
def add_links(resource, name, serializers)
type = serialized_object_type(serializers)
resource[:links] ||= {}
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, val=nil)
resource[:links] ||= {}
resource[:links][name] = nil
if serializer && serializer.object
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('.')
if include_assoc?(resource_path) && resource_type = serialized_object_type(serializers)
plural_name = resource_type.pluralize.to_sym
@top[:linked] ||= {}
@top[:linked][plural_name] ||= []
serializers.each do |serializer|
attrs = attributes_for_serializer(serializer, @options)
add_resource_links(attrs, serializer, add_linked: false)
@top[:linked][plural_name].push(attrs) unless @top[:linked][plural_name].include?(attrs)
end
end
@@@ -97,16 -85,22 +126,33 @@@
result = []
serializer.each do |object|
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
result << cache_check(object) do
attributes = object.attributes(options)
attributes[:id] = attributes[:id].to_s if attributes[:id]
result << attributes
end
end
else
result = cache_check(serializer) do
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
result = serializer.attributes(options)
result[:id] = result[:id].to_s if result[:id]
result
end
end
result
end
@@@ -147,12 -131,16 +193,16 @@@
if association.respond_to?(:each)
add_links(attrs, name, association)
else
if opts[:virtual_value]
add_link(attrs, name, nil, opts[:virtual_value])
else
add_link(attrs, name, association)
end
end
if @options[:embed] != :ids && options[:add_linked]
Array(association).each do |association|
add_linked(name, association)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment