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 0f789e4
Show file tree
Hide file tree
Showing 25 changed files with 816 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
4 changes: 3 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
# reified when subclassed to decorate a resource.
module ActiveModel
class Serializer
extend ActiveSupport::Autoload
include Configuration
include Associations
include Attributes
include Caching
include Links
include Meta
include Type
autoload :Adapter

# @param resource [ActiveRecord::Base, ActiveModelSerializers::Model]
# @return [ActiveModel::Serializer]
Expand All @@ -44,7 +46,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
44 changes: 44 additions & 0 deletions lib/active_model/serializer/adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module ActiveModel
class Serializer
# @deprecated Use ActiveModelSerializers::Adapter instead
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

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

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

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

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

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

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
14 changes: 14 additions & 0 deletions lib/active_model/serializer/adapter/attributes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_model_serializers/adapter/attributes'

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
16 changes: 16 additions & 0 deletions lib/active_model/serializer/adapter/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module ActiveModel
class Serializer
module Adapter
class Base < DelegateClass(ActiveModelSerializers::Adapter::Base)
def self.inherited(base)
warn "Inheriting deprecated ActiveModel::Serializer::Adater::Base in #{caller[0..2].join(', ')}. Please use ActiveModelSerializers::Adapter::Base"
super
end

def initialize(serializer, options = {})
super(ActiveModelSerializers::Adapter::Base.new(serializer, options))
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_model_serializers/adapter/json'

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
14 changes: 14 additions & 0 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_model_serializers/adapter/json_api'

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.

14 changes: 14 additions & 0 deletions lib/active_model/serializer/adapter/null.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'active_model_serializers/adapter/null'

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
1 change: 1 addition & 0 deletions lib/active_model_serializers/adapter/.#json_api.rb
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 0f789e4

Please sign in to comment.