Skip to content

Commit

Permalink
Pass serialization context into adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 1, 2016
1 parent 0dd111c commit 001ecb9
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 83 deletions.
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# reified when subclassed to decorate a resource.
module ActiveModel
class Serializer
SERIALIZABLE_HASH_VALID_KEYS = Set.new([:only, :except, :methods, :include])
SERIALIZABLE_HASH_VALID_KEYS = [:only, :except, :methods, :include].freeze
extend ActiveSupport::Autoload
autoload :Adapter
autoload :Null
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/adapter/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def cached_name
end

def serializable_hash(options = nil)
(options ||= {}).slice(ActiveModel::Serializer::SERIALIZABLE_HASH_VALID_KEYS)
(options ||= {}).slice(*ActiveModel::Serializer::SERIALIZABLE_HASH_VALID_KEYS) # rubocop:disable Lint/UselessAssignment
end

def as_json(options = nil)
Expand Down
13 changes: 6 additions & 7 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ def default_key_transform

# {http://jsonapi.org/format/#crud Requests are transactional, i.e. success or failure}
# {http://jsonapi.org/format/#document-top-level data and errors MUST NOT coexist in the same document.}
def serializable_hash(options = nil)
options = super
def serializable_hash(*)
document = if serializer.success?
success_document(options)
success_document
else
failure_document
end
Expand All @@ -68,7 +67,7 @@ def serializable_hash(options = nil)
# links: toplevel_links,
# jsonapi: toplevel_jsonapi
# }.reject! {|_,v| v.nil? }
def success_document(options)
def success_document
is_collection = serializer.respond_to?(:each)
serializers = is_collection ? serializer : [serializer]
primary_data, included = resource_objects_for(serializers)
Expand Down Expand Up @@ -128,7 +127,7 @@ def success_document(options)

if is_collection && serializer.paginated?
hash[:links] ||= {}
hash[:links].update(pagination_links_for(serializer, options))
hash[:links].update(pagination_links_for(serializer))
end

hash
Expand Down Expand Up @@ -498,8 +497,8 @@ def links_for(serializer)
# end
# prs:
# https://github.com/rails-api/active_model_serializers/pull/1041
def pagination_links_for(serializer, options)
PaginationLinks.new(serializer.object, options).as_json
def pagination_links_for(serializer)
PaginationLinks.new(serializer.object, instance_options).as_json
end

# {http://jsonapi.org/format/#document-meta Docment Meta}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ class PaginationLinks

attr_reader :collection, :context

def initialize(collection, options)
def initialize(collection, adapter_options)
@collection = collection
@options = options
@context = options.fetch(:serialization_context)
@adapter_options = adapter_options
@context = adapter_options.fetch(:serialization_context)
end

def as_json
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
pages_from.each_with_object({}) do |(key, value), hash|
params = query_parameters.merge(page: { number: value, size: per_page }).to_query

hash[key] = "#{url(options)}?#{params}"
hash[key] = "#{url(adapter_options)}?#{params}"
end
end

protected

attr_reader :options
attr_reader :adapter_options

private

Expand Down
2 changes: 1 addition & 1 deletion lib/active_model_serializers/serializable_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module ActiveModelSerializers
class SerializableResource
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links])
ADAPTER_OPTION_KEYS = Set.new([:include, :fields, :adapter, :meta, :meta_key, :links, :serialization_context])
include ActiveModelSerializers::Logging

delegate :serializable_hash, :as_json, :to_json, to: :adapter
Expand Down
22 changes: 11 additions & 11 deletions test/adapter/json/key_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ def mock_request(key_transform = nil)
context.expect(:key_transform, key_transform)
@options = {}
@options[:serialization_context] = context
serializer = CustomBlogSerializer.new(@blog)
@adapter = ActiveModelSerializers::Adapter::Json.new(serializer, @options)
end

Post = Class.new(::Model)
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :publish_at
end

def setup
setup do
ActionController::Base.cache_store.clear
@blog = Blog.new(id: 1, name: 'My Blog!!', special_attribute: 'neat')
serializer = CustomBlogSerializer.new(@blog)
@adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
end

def test_key_transform_default
mock_request
assert_equal({
blog: { id: 1, special_attribute: 'neat', articles: nil }
}, @adapter.serializable_hash(@options))
}, @adapter.serializable_hash)
end

def test_key_transform_global_config
mock_request
result = with_config(key_transform: :camel_lower) do
@adapter.serializable_hash(@options)
@adapter.serializable_hash
end
assert_equal({
blog: { id: 1, specialAttribute: 'neat', articles: nil }
Expand All @@ -45,7 +45,7 @@ def test_key_transform_global_config
def test_key_transform_serialization_ctx_overrides_global_config
mock_request(:camel)
result = with_config(key_transform: :camel_lower) do
@adapter.serializable_hash(@options)
@adapter.serializable_hash
end
assert_equal({
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
Expand All @@ -56,36 +56,36 @@ def test_key_transform_undefined
mock_request(:blam)
result = nil
assert_raises NoMethodError do
result = @adapter.serializable_hash(@options)
result = @adapter.serializable_hash
end
end

def test_key_transform_dashed
mock_request(:dashed)
assert_equal({
blog: { id: 1, :"special-attribute" => 'neat', articles: nil }
}, @adapter.serializable_hash(@options))
}, @adapter.serializable_hash)
end

def test_key_transform_unaltered
mock_request(:unaltered)
assert_equal({
blog: { id: 1, special_attribute: 'neat', articles: nil }
}, @adapter.serializable_hash(@options))
}, @adapter.serializable_hash)
end

def test_key_transform_camel
mock_request(:camel)
assert_equal({
Blog: { Id: 1, SpecialAttribute: 'neat', Articles: nil }
}, @adapter.serializable_hash(@options))
}, @adapter.serializable_hash)
end

def test_key_transform_camel_lower
mock_request(:camel_lower)
assert_equal({
blog: { id: 1, specialAttribute: 'neat', articles: nil }
}, @adapter.serializable_hash(@options))
}, @adapter.serializable_hash)
end
end
end
Expand Down
64 changes: 32 additions & 32 deletions test/adapter/json_api/key_case_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def setup
def test_success_document_key_transform_default
mock_request
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
assert_equal({
data: {
id: '1337',
Expand Down Expand Up @@ -102,8 +102,8 @@ def test_success_document_key_transform_global_config
mock_request
result = with_config(key_transform: :camel_lower) do
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
adapter.serializable_hash
end
assert_equal({
data: {
Expand Down Expand Up @@ -138,8 +138,8 @@ def test_success_doc_key_transform_serialization_ctx_overrides_global
mock_request(:camel)
result = with_config(key_transform: :camel_lower) do
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
adapter.serializable_hash
end
assert_equal({
Data: {
Expand Down Expand Up @@ -173,8 +173,8 @@ def test_success_doc_key_transform_serialization_ctx_overrides_global
def test_success_document_key_transform_dashed
mock_request(:dashed)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
assert_equal({
data: {
id: '1337',
Expand Down Expand Up @@ -207,8 +207,8 @@ def test_success_document_key_transform_dashed
def test_success_document_key_transform_unaltered
mock_request(:unaltered)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
assert_equal({
data: {
id: '1337',
Expand Down Expand Up @@ -241,17 +241,17 @@ def test_success_document_key_transform_unaltered
def test_success_document_key_transform_undefined
mock_request(:zoot)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
assert_raises NoMethodError do
adapter.serializable_hash(@options)
adapter.serializable_hash
end
end

def test_success_document_key_transform_camel
mock_request(:camel)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
assert_equal({
Data: {
Id: '1337',
Expand Down Expand Up @@ -284,8 +284,8 @@ def test_success_document_key_transform_camel
def test_success_document_key_transform_camel_lower
mock_request(:camel_lower)
serializer = PostSerializer.new(@post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
assert_equal({
data: {
id: '1337',
Expand Down Expand Up @@ -321,8 +321,8 @@ def test_error_document_key_transform_default
resource.errors.add(:published_at, 'must be in the future')
resource.errors.add(:title, 'must be longer')
serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash
expected_errors_object =
{ :errors =>
[
Expand All @@ -345,8 +345,8 @@ def test_error_document_key_transform_global_config
resource.errors.add(:published_at, 'must be in the future')
resource.errors.add(:title, 'must be longer')
serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
adapter.serializable_hash
end
expected_errors_object =
{ :Errors =>
Expand All @@ -371,8 +371,8 @@ def test_error_document_key_transform_serialization_ctx_overrides_global
resource.errors.add(:published_at, 'must be in the future')
resource.errors.add(:title, 'must be longer')
serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
adapter.serializable_hash
end
expected_errors_object =
{ :Errors =>
Expand All @@ -398,8 +398,8 @@ def test_error_document_key_transform_dashed
resource.errors.add(:title, 'must be longer')

serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash

expected_errors_object =
{ :errors =>
Expand All @@ -425,8 +425,8 @@ def test_error_document_key_transform_unaltered
resource.errors.add(:title, 'must be longer')

serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash

expected_errors_object =
{ :errors =>
Expand All @@ -446,10 +446,10 @@ def test_error_document_key_transform_undefined
resource.errors.add(:title, 'must be longer')

serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)

assert_raises NoMethodError do
adapter.serializable_hash(@options)
adapter.serializable_hash
end
end

Expand All @@ -461,8 +461,8 @@ def test_error_document_key_transform_camel
resource.errors.add(:title, 'must be longer')

serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash

expected_errors_object =
{ :Errors =>
Expand All @@ -482,8 +482,8 @@ def test_error_document_key_transform_camel_lower
resource.errors.add(:title, 'must be longer')

serializer = ActiveModel::Serializer::ErrorSerializer.new(resource)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
result = adapter.serializable_hash(@options)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer, @options)
result = adapter.serializable_hash

expected_errors_object =
{ :errors =>
Expand Down
Loading

0 comments on commit 001ecb9

Please sign in to comment.