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

Meta no longer handled in Base adapter. #1695

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alias method should be fine here, unless we want to keep the root option here like in the am:s:j in rails

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It failed because the alias would be bound to the pre-inheritance placeholder method.


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if instance_options[:meta].is_a?(Hash). maybe if instance_options[:meta].respond_to?(:fetch)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we kinda want to ensure the value of meta is a hash (as per the spec).


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