Skip to content

Commit

Permalink
Update Json, FlattenJson adapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhide committed Aug 26, 2015
1 parent 0e43a6e commit 33c0300
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 107 deletions.
1 change: 0 additions & 1 deletion lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Adapter
extend ActiveSupport::Autoload
autoload :Json
autoload :FlattenJson
autoload :NestedJson
autoload :Null
autoload :JsonApi

Expand Down
11 changes: 5 additions & 6 deletions lib/active_model/serializer/adapter/flatten_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ module ActiveModel
class Serializer
class Adapter
class FlattenJson < Json
def serializable_hash(options = {})
super
@result
end

private

# no-op: FlattenJson adapter does not include meta data, because it does not support root.
def rooting?
false
end

def include_meta(json)
json
end
end
end
end
end

101 changes: 68 additions & 33 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,84 @@ module ActiveModel
class Serializer
class Adapter
class Json < Adapter
def serializable_hash(options = nil)
cattr_accessor :default_limit_depth, :default_check_depth_strategy
self.default_limit_depth = 1
self.default_check_depth_strategy = :trim

def serializable_hash options = nil
options ||= {}
if serializer.respond_to?(:each)
@result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
else
@hash = {}
@current_depth = options[:_current_depth] || 0
@without_root = options[:_without_root]
@limit_depth = options[:limit_depth] || default_limit_depth
@check_depth_strategy = options[:check_depth_strategy] || default_check_depth_strategy

@core = cache_check(serializer) do
serializer.attributes(options)
end
@result =
serialize_collection(serializer, options.merge(_without_root: true)) ||
serialize_attributes(options).merge(serialize_associations)
rooting? ? { root => @result } : @result
end

def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
end

private

def rooting?
!@without_root && (@current_depth == 0)
end

def serialize_object serializer, options = {}
if serializer.try(:object)
self.class.new(serializer).serializable_hash(options)
end
end

def serialize_collection serializers, options = {}
if serializers.respond_to?(:each)
serializers.map { |s| serialize_object(s, options) }
end
end

def serialize_attributes options
cache_check(serializer) do
serializer.attributes(options)
end
end

def serialize_associations
hash = {}
next_depth = @current_depth + 1
cascading_options = {
limit_depth: @limit_depth,
check_depth_strategy: @check_depth_strategy,
_current_depth: next_depth
}
unless too_deep? next_depth
serializer.associations.each do |association|
serializer = association.serializer
opts = association.options

if serializer.respond_to?(:each)
array_serializer = serializer
@hash[association.key] = array_serializer.map do |item|
cache_check(item) do
item.attributes(opts)
end
end
else
@hash[association.key] =
if serializer && serializer.object
cache_check(serializer) do
serializer.attributes(options)
end
elsif opts[:virtual_value]
opts[:virtual_value]
end
end
opts = association.options.merge(cascading_options)
hash[association.key] =
serialize_collection(serializer, opts) ||
serialize_object(serializer, opts) ||
opts[:virtual_value]
end
@result = @core.merge @hash
end

{ root => @result }
hash
end

def fragment_cache(cached_hash, non_cached_hash)
Json::FragmentCache.new().fragment_cache(cached_hash, non_cached_hash)
def too_deep? depth
if depth > @limit_depth
case @check_depth_strategy
when :fail
fail 'Too deep associations.'
when :trim
true
end
else
false
end
end

end
end
end
Expand Down
63 changes: 0 additions & 63 deletions lib/active_model/serializer/adapter/nested_json.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setup
@author.posts = [@post]

@serializer = AuthorNestedSerializer.new(@author)
@adapter = ActiveModel::Serializer::Adapter::NestedJson.new(@serializer)
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
end

def test_has_many
Expand All @@ -27,14 +27,28 @@ def test_has_many
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
]
}]
}, @adapter.serializable_hash)
}, @adapter.serializable_hash(limit_depth: 5)[:author])
end

def test_limit_depth
assert_raises do
@adapter.serializable_hash(limit_depth: 1)
assert_raises(StandardError) do
@adapter.serializable_hash(limit_depth: 1, check_depth_strategy: :fail)
end
end

def test_flatten_json
adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(@serializer)
assert_equal({
id: 1, name: 'Steve K.',
posts: [{
id: 1, title: 'New Post', body: 'Body',
comments: [
{id: 1, body: 'ZOMG A COMMENT'},
{id: 2, body: 'ZOMG ANOTHER COMMENT'}
]
}]
}, adapter.serializable_hash(limit_depth: 5))
end
end
end
end
Expand Down

0 comments on commit 33c0300

Please sign in to comment.