Skip to content

Commit

Permalink
Merge pull request #1695 from beauby/meta-madness
Browse files Browse the repository at this point in the history
Meta no longer handled in Base adapter.
  • Loading branch information
beauby committed Apr 21, 2016
2 parents f0fa743 + e804d37 commit b4e2ac3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 29 deletions.
5 changes: 0 additions & 5 deletions lib/active_model_serializers/adapter/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ def relationship_value_for(association, options)
Attributes.new(association.serializer, opts).serializable_hash(options)
end

# no-op: Attributes adapter does not include meta data, because it does not support root.
def include_meta(json)
json
end

# Set @cached_attributes
def cache_attributes
return if @cached_attributes.present?
Expand Down
17 changes: 1 addition & 16 deletions lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def serializable_hash(_options = nil)
end

def as_json(options = nil)
hash = serializable_hash(options)
include_meta(hash)
hash
serializable_hash(options)
end

def fragment_cache(cached_hash, non_cached_hash)
Expand All @@ -49,23 +47,10 @@ def serialization_options(options)
options ||= {} # rubocop:disable Lint/UselessAssignment
end

def meta
instance_options.fetch(:meta, nil)
end

def meta_key
instance_options.fetch(:meta_key, 'meta'.freeze)
end

def root
serializer.json_key.to_sym if serializer.json_key
end

def include_meta(json)
json[meta_key] = meta unless meta.blank?
json
end

class << self
# Sets the default transform for the adapter.
#
Expand Down
10 changes: 10 additions & 0 deletions lib/active_model_serializers/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ class Json < Base
def serializable_hash(options = nil)
options = serialization_options(options)
serialized_hash = { root => Attributes.new(serializer, instance_options).serializable_hash(options) }
serialized_hash[meta_key] = meta unless meta.blank?

self.class.transform_key_casing!(serialized_hash, instance_options)
end

def meta
instance_options.fetch(:meta, nil)
end

def meta_key
instance_options.fetch(:meta_key, 'meta'.freeze)
end
end
end
end
4 changes: 4 additions & 0 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def serializable_hash(*)
# links: toplevel_links,
# jsonapi: toplevel_jsonapi
# }.reject! {|_,v| v.nil? }
# rubocop:disable Metrics/CyclomaticComplexity
def success_document
is_collection = serializer.respond_to?(:each)
serializers = is_collection ? serializer : [serializer]
Expand Down Expand Up @@ -130,8 +131,11 @@ def success_document
hash[:links].update(pagination_links_for(serializer))
end

hash[:meta] = instance_options[:meta] if instance_options[:meta].is_a?(Hash)

hash
end
# rubocop:enable Metrics/CyclomaticComplexity

# {http://jsonapi.org/format/#errors JSON API Errors}
# TODO: look into caching
Expand Down
15 changes: 7 additions & 8 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_meta_key_is_used
assert_equal(expected, actual)
end

def test_meta_key_is_used_with_json_api
def test_meta_key_is_not_used_with_json_api
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json_api,
Expand All @@ -105,25 +105,25 @@ def test_meta_key_is_used_with_json_api
type: 'blogs',
attributes: { title: 'AMS Hints' }
},
'haha_meta' => { total: 10 }
meta: { total: 10 }
}
assert_equal(expected, actual)
end

def test_meta_key_is_not_present_when_blank_object_with_json_api
def test_meta_key_is_present_when_empty_hash_with_json_api
actual = ActiveModelSerializers::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: {},
meta_key: 'haha_meta'
meta: {}
).as_json
expected = {
data: {
id: '1',
type: 'blogs',
attributes: { title: 'AMS Hints' }
}
},
meta: {}
}
assert_equal(expected, actual)
end
Expand All @@ -133,8 +133,7 @@ def test_meta_key_is_not_present_when_empty_string_with_json_api
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: '',
meta_key: 'haha_meta'
meta: ''
).as_json
expected = {
data: {
Expand Down

0 comments on commit b4e2ac3

Please sign in to comment.