Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlattenJson adapter no longer inherits Json adapter, renamed to Attributes #1117

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
FlattenJson adapter no longer inherits Json adapter
  • Loading branch information
bf4 committed Sep 18, 2015
commit ceef214f1e8d8cbb35ab3b2422097a3d8fd3071d
4 changes: 3 additions & 1 deletion lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,14 @@ def cache_check(serializer)
end
end

private

def meta
serializer.meta if serializer.respond_to?(:meta)
end

def meta_key
serializer.meta_key || 'meta'
serializer.meta_key || 'meta'.freeze
end

def root
Expand Down
42 changes: 40 additions & 2 deletions lib/active_model/serializer/adapter/flatten_json.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,44 @@
class ActiveModel::Serializer::Adapter::FlattenJson < ActiveModel::Serializer::Adapter::Json
def serializable_hash(options = {})
super.each_value.first
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
else
hash = {}

core = cache_check(serializer) do
serializer.attributes(options)
end

serializer.associations.each do |association|
serializer = association.serializer
association_options = association.options

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

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

private
Expand Down
40 changes: 4 additions & 36 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,12 @@ class ActiveModel::Serializer::Adapter::Json < ActiveModel::Serializer::Adapter

def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
result = serializer.map { |s| FlattenJson.new(s).serializable_hash(options) }
else
hash = {}

core = cache_check(serializer) do
serializer.attributes(options)
end

serializer.associations.each do |association|
serializer = association.serializer
association_options = association.options

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

{ root => result }
{ root => FlattenJson.new(serializer).serializable_hash(options) }
end

private

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