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

Move meta/meta_key handling inside adapter. #1233

Merged
merged 1 commit into from
Oct 5, 2015
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fixes:
- [#1214](https://github.com/rails-api/active_model_serializers/pull/1214) retrieve the key from the reflection options when building associations (@NullVoxPopuli, @hut8)

Misc:
- [#1233](https://github.com/rails-api/active_model_serializers/pull/1233) Top-level meta and meta_key options no longer handled at serializer level (@beauby)
- [#1232](https://github.com/rails-api/active_model_serializers/pull/1232) fields option no longer handled at serializer level (@beauby)
- [#1178](https://github.com/rails-api/active_model_serializers/pull/1178) env CAPTURE_STDERR=false lets devs see hard failures (@bf4)
- [#1177](https://github.com/rails-api/active_model_serializers/pull/1177) Remove Adapter autoloads in favor of require (@bf4)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializable_resource.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'set'
module ActiveModel
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key])

# Primary interface to composing a resource with a serializer and adapter.
# @return the serializable_resource, ready for #as_json/#to_json/#serializable_hash.
Expand Down
4 changes: 1 addition & 3 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,13 @@ def self.get_serializer_for(klass)
end
end

attr_accessor :object, :root, :meta, :meta_key, :scope
attr_accessor :object, :root, :scope
class_attribute :_type, instance_writer: false

def initialize(object, options = {})
self.object = object
self.instance_options = options
self.root = instance_options[:root]
self.meta = instance_options[:meta]
self.meta_key = instance_options[:meta_key]
self.scope = instance_options[:scope]

scope_name = instance_options[:scope_name]
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model/serializer/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def cache_check(serializer)
private

def meta
serializer.meta if serializer.respond_to?(:meta)
instance_options.fetch(:meta, nil)
Copy link
Member

Choose a reason for hiding this comment

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

👍

end

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

def root
Expand Down
4 changes: 1 addition & 3 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ArraySerializer
include Enumerable
delegate :each, to: :@serializers

attr_reader :object, :root, :meta, :meta_key
attr_reader :object, :root

def initialize(resources, options = {})
@root = options[:root]
Expand All @@ -21,8 +21,6 @@ def initialize(resources, options = {})
serializer_class.new(resource, options.except(:serializer))
end
end
@meta = options[:meta]
@meta_key = options[:meta_key]
end

def json_key
Expand Down
8 changes: 0 additions & 8 deletions test/array_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@ def test_serializer_option_not_passed_to_each_serializer
refute serializers.first.custom_options.key?(:serializer)
end

def test_meta_and_meta_key_attr_readers
meta_content = { meta: 'the meta', meta_key: 'the meta key' }
@serializer = ArraySerializer.new([@comment, @post], meta_content)

assert_equal @serializer.meta, 'the meta'
assert_equal @serializer.meta_key, 'the meta key'
end

def test_root_default
@serializer = ArraySerializer.new([@comment, @post])
assert_equal @serializer.root, nil
Expand Down
65 changes: 37 additions & 28 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ module ActiveModel
class Serializer
class MetaTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@blog = Blog.new(id: 1,
name: 'AMS Hints',
writer: Author.new(id: 2, name: 'Steve'),
articles: [Post.new(id: 3, title: 'AMS')])
end

def test_meta_is_present_with_root
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 })
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
actual = ActiveModel::SerializableResource.new(
Copy link
Member

Choose a reason for hiding this comment

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

👍

@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: { total: 10 }).as_json
expected = {
blog: {
id: 1,
Expand All @@ -23,22 +25,29 @@ def test_meta_is_present_with_root
total: 10
}
}
assert_equal expected, adapter.as_json
assert_equal(expected, actual)
end

def test_meta_is_not_included_when_root_is_missing
# load_adapter uses Attributes Adapter
adapter = load_adapter(meta: { total: 10 })
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :attributes,
serializer: AlternateBlogSerializer,
meta: { total: 10 }).as_json
expected = {
id: 1,
title: 'AMS Hints'
}
assert_equal expected, adapter.as_json
assert_equal(expected, actual)
end

def test_meta_key_is_used
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json,
serializer: AlternateBlogSerializer,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
blog: {
id: 1,
Expand All @@ -48,12 +57,16 @@ def test_meta_key_is_used
total: 10
}
}
assert_equal expected, adapter.as_json
assert_equal(expected, actual)
end

def test_meta_key_is_used_with_json_api
serializer = AlternateBlogSerializer.new(@blog, meta: { total: 10 }, meta_key: 'haha_meta')
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
actual = ActiveModel::SerializableResource.new(
@blog,
adapter: :json_api,
serializer: AlternateBlogSerializer,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
data: {
id: '1',
Expand All @@ -62,13 +75,14 @@ def test_meta_key_is_used_with_json_api
},
'haha_meta' => { total: 10 }
}
assert_equal expected, adapter.as_json
assert_equal(expected, actual)
end

def test_meta_is_not_present_on_arrays_without_root
serializer = ArraySerializer.new([@blog], meta: { total: 10 })
# Attributes doesn't have support to root
adapter = ActiveModel::Serializer::Adapter::Attributes.new(serializer)
actual = ActiveModel::SerializableResource.new(
[@blog],
adapter: :attributes,
meta: { total: 10 }).as_json
expected = [{
id: 1,
name: 'AMS Hints',
Expand All @@ -82,13 +96,15 @@ def test_meta_is_not_present_on_arrays_without_root
body: nil
}]
}]
assert_equal expected, adapter.as_json
assert_equal(expected, actual)
end

def test_meta_is_present_on_arrays_with_root
serializer = ArraySerializer.new([@blog], meta: { total: 10 }, meta_key: 'haha_meta')
# JSON adapter adds root by default
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
actual = ActiveModel::SerializableResource.new(
[@blog],
adapter: :json,
meta: { total: 10 },
meta_key: 'haha_meta').as_json
expected = {
blogs: [{
id: 1,
Expand All @@ -107,14 +123,7 @@ def test_meta_is_present_on_arrays_with_root
total: 10
}
}
assert_equal expected, adapter.as_json
end

private

def load_adapter(options)
options = options.merge(adapter: :attributes, serializer: AlternateBlogSerializer)
ActiveModel::SerializableResource.new(@blog, options)
assert_equal(expected, actual)
end
end
end
Expand Down