Skip to content

Commit

Permalink
Follow up to rails-api#1535
Browse files Browse the repository at this point in the history
- The removed classes and modules were added back with deprecation
  warning and deprecation test were added for them.
- One test was renamed because it contained `__`.
- Some tests were refactored.
- The ActiveModelSerializers::Deserialization module is now called
  Adapter instead of ActiveModelSerializers::Adapter.
- The changelog was added for rails-api#1535
  • Loading branch information
Yohan Robert committed Feb 29, 2016
1 parent 8a04005 commit d935acc
Show file tree
Hide file tree
Showing 24 changed files with 790 additions and 608 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Fixes:
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)

Misc:
- [#1543](https://github.com/rails-api/active_model_serializers/pull/1543) Add the changes missing from #1535. (@groyoh)
- [#1535](https://github.com/rails-api/active_model_serializers/pull/1535) Move the adapter and adapter folder to
active_model_serializers folder and changes the module namespace. (@domitian @bf4)
- [#1497](https://github.com/rails-api/active_model_serializers/pull/1497) Add JRuby-9000 to appveyor.yml(@corainchicago)

### v0.10.0.rc4 (2016/01/27 11:00 +00:00)
Expand Down
3 changes: 2 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Serializer
include Links
include Meta
include Type
require 'active_model/serializer/adapter'

# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
# @return [ActiveModel::Serializer]
Expand All @@ -44,7 +45,7 @@ def self.serializer_for(resource, options = {})
# @see ActiveModelSerializers::Adapter.lookup
# Deprecated
def self.adapter
warn 'Calling adapter method in Serializer, please use the ActiveModelSerializers::configured_adapter'
warn 'Calling adapter method in Serializer, please use the ActiveModelSerializers::Adapter.configured_adapter'
ActiveModelSerializers::Adapter.lookup(config.adapter)
end

Expand Down
59 changes: 59 additions & 0 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module ActiveModel
class Serializer
module Adapter
class << self
def create(resource, options = {})
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.create(resource, options)
end

# @see ActiveModel::Serializer::Adapter.lookup
def adapter_class(adapter)
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.adapter_class(adapter)
end

# @return Hash<adapter_name, adapter_class>
def adapter_map
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.adapter_map
end

# @return [Array<Symbol>] list of adapter names
def adapters
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.adapters
end

# Adds an adapter 'klass' with 'name' to the 'adapter_map'
# Names are stringified and underscored
# @param name [Symbol, String, Class] name of the registered adapter
# @param klass [Class] adapter class itself, optional if name is the class
# @example
# AMS::Adapter.register(:my_adapter, MyAdapter)
# @note The registered name strips out 'ActiveModel::Serializer::Adapter::'
# so that registering 'ActiveModel::Serializer::Adapter::Json' and
# 'Json' will both register as 'json'.
def register(name, klass = name)
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.register(name, klass)
end

# @param adapter [String, Symbol, Class] name to fetch adapter by
# @return [ActiveModel::Serializer::Adapter] subclass of Adapter
# @raise [UnknownAdapterError]
def lookup(adapter)
warn "Calling deprecated ActiveModel::Serializer::Adater in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter"
ActiveModelSerializers::Adapter.lookup(adapter)
end
end

# Gotta be at the bottom to use the code above it :(
require 'active_model/serializer/adapter/base'
require 'active_model/serializer/adapter/null'
require 'active_model/serializer/adapter/attributes'
require 'active_model/serializer/adapter/json'
require 'active_model/serializer/adapter/json_api'
end
end
end
12 changes: 12 additions & 0 deletions lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ActiveModel
class Serializer
module Adapter
class Attributes < DelegateClass(ActiveModelSerializers::Adapter::Attributes)
def initialize(serializer, options = {})
warn "Calling deprecated ActiveModel::Serializer::Adater::Attributes in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter::Attributes"
super(ActiveModelSerializers::Adapter::Attributes.new(serializer, options))
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/active_model/serializer/adapter/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module ActiveModel
class Serializer
module Adapter
Base = ActiveModelSerializers::Adapter::Base
end
end
end
12 changes: 12 additions & 0 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ActiveModel
class Serializer
module Adapter
class Json < DelegateClass(ActiveModelSerializers::Adapter::Json)
def initialize(serializer, options = {})
warn "Calling deprecated ActiveModel::Serializer::Adater::Json in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter::Json"
super(ActiveModelSerializers::Adapter::Json.new(serializer, options))
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ActiveModel
class Serializer
module Adapter
class JsonApi < DelegateClass(ActiveModelSerializers::Adapter::JsonApi)
def initialize(serializer, options = {})
warn "Calling deprecated ActiveModel::Serializer::Adater::JsonApi in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter::JsonApi"
super(ActiveModelSerializers::Adapter::JsonApi.new(serializer, options))
end
end
end
end
end
13 changes: 0 additions & 13 deletions lib/active_model/serializer/adapter/json_api/api_objects.rb

This file was deleted.

This file was deleted.

This file was deleted.

29 changes: 0 additions & 29 deletions lib/active_model/serializer/adapter/json_api/meta.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/active_model/serializer/adapter/null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ActiveModel
class Serializer
module Adapter
class Null < DelegateClass(ActiveModelSerializers::Adapter::Null)
def initialize(serializer, options = {})
warn "Calling deprecated ActiveModel::Serializer::Adater::Null in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter::Null"
super(ActiveModelSerializers::Adapter::Null.new(serializer, options))
end
end
end
end
end
13 changes: 7 additions & 6 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class JsonApi < Base
autoload :PaginationLinks
autoload :FragmentCache
autoload :Link
require 'active_model/serializer/adapter/json_api/meta'
autoload :Meta
autoload :Deserialization
require 'active_model/serializer/adapter/json_api/api_objects'
autoload :ResourceIdentifier
autoload :Relationship

# TODO: if we like this abstraction and other API objects to it,
# then extract to its own file and require it.
Expand Down Expand Up @@ -98,7 +99,7 @@ def resource_objects_for(serializers)
end

def process_resource(serializer, primary)
resource_identifier = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::ResourceIdentifier.new(serializer).as_json
resource_identifier = ResourceIdentifier.new(serializer).as_json
return false unless @resource_identifiers.add?(resource_identifier)

resource_object = resource_object_for(serializer)
Expand Down Expand Up @@ -134,7 +135,7 @@ def attributes_for(serializer, fields)

def resource_object_for(serializer)
resource_object = cache_check(serializer) do
resource_object = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::ResourceIdentifier.new(serializer).as_json
resource_object = ResourceIdentifier.new(serializer).as_json

requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
Expand All @@ -158,7 +159,7 @@ def resource_object_for(serializer)
def relationships_for(serializer, requested_associations)
include_tree = ActiveModel::Serializer::IncludeTree.from_include_args(requested_associations)
serializer.associations(include_tree).each_with_object({}) do |association, hash|
hash[association.key] = ActiveModel::Serializer::Adapter::JsonApi::ApiObjects::Relationship.new(
hash[association.key] = Relationship.new(
serializer,
association.serializer,
association.options,
Expand All @@ -179,7 +180,7 @@ def pagination_links_for(serializer, options)
end

def meta_for(serializer)
ActiveModel::Serializer::Adapter::JsonApi::Meta.new(serializer).as_json
Meta.new(serializer).as_json
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions lib/active_model_serializers/adapter/json_api/meta.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module ActiveModelSerializers
module Adapter
class JsonApi
class Meta
def initialize(serializer)
@object = serializer.object
@scope = serializer.scope

# Use the return value of the block unless it is nil.
if serializer._meta.respond_to?(:call)
@value = instance_eval(&serializer._meta)
else
@value = serializer._meta
end
end

def as_json
@value
end

protected

attr_reader :object, :scope
end
end
end
end
Loading

0 comments on commit d935acc

Please sign in to comment.