Skip to content

Rename Serializer subclasses to Serialization #1137

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

Closed
wants to merge 1 commit into from
Closed
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
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _Windows Build Status -_ [![Build status](https://ci.appveyor.com/api/projects/s
ActiveModel::Serializer brings convention over configuration to your JSON generation.

AMS does this through two components: **serializers** and **adapters**.
Serializers describe _which_ attributes and relationships should be serialized.
Serializations describe _which_ attributes and relationships should be serialized.
Adapters describe _how_ attributes and relationships should be serialized.

By default AMS will use the **Flatten Json Adapter**. But we strongly advise you to use **JsonApi Adapter** that follows 1.0 of the format specified in [jsonapi.org/format](http://jsonapi.org/format).
Expand All @@ -31,7 +31,7 @@ Given two models, a `Post(title: string, body: text)` and a
serializers:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
cache key: 'posts', expires_in: 3.hours
attributes :title, :body

Expand All @@ -42,7 +42,7 @@ end
and

```ruby
class CommentSerializer < ActiveModel::Serializer
class CommentSerialization < ActiveModel::Serializer
attributes :name, :body

belongs_to :post
Expand Down Expand Up @@ -75,7 +75,7 @@ ActiveModel::Serializer.config.adapter = :json
If you would like the key in the outputted JSON to be different from its name in ActiveRecord, you can use the :key option to customize it:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
attributes :id, :body

# look up :subject on the model, but use +title+ in the JSON
Expand All @@ -97,7 +97,7 @@ class PostsController < ApplicationController
end
```

In this case, Rails will look for a serializer named `PostSerializer`, and if
In this case, Rails will look for a serializer named `PostSerialization`, and if
it exists, use it to serialize the `Post`.

### Specify a serializer
Expand All @@ -107,18 +107,18 @@ If you wish to use a serializer other than the default, you can explicitly pass
#### 1. For a resource:

```ruby
render json: @post, serializer: PostPreviewSerializer
render json: @post, serializer: PostPreviewSerialization
```

#### 2. For an array resource:

```ruby
# Use the default `ArraySerializer`, which will use `each_serializer` to
# serialize each element
render json: @posts, each_serializer: PostPreviewSerializer
render json: @posts, each_serializer: PostPreviewSerialization

# Or, you can explicitly provide the collection serializer as well
render json: @posts, serializer: CollectionSerializer, each_serializer: PostPreviewSerializer
render json: @posts, serializer: CollectionSerialization, each_serializer: PostPreviewSerialization
```

### Meta
Expand Down Expand Up @@ -160,7 +160,7 @@ end
If you want to override any association, you can use:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
attributes :id, :body

has_many :comments
Expand All @@ -176,7 +176,7 @@ end
If you want to override any attribute, you can use:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
attributes :id, :body

has_many :comments
Expand Down Expand Up @@ -226,7 +226,7 @@ And then execute:
$ bundle
```

## Creating a Serializer
## Creating a Serialization

The easiest way to create a new serializer is to generate a new resource, which
will generate a serializer at the same time:
Expand All @@ -247,7 +247,7 @@ The generated seralizer will contain basic `attributes` and
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
attributes :title, :body

has_many :comments
Expand All @@ -258,7 +258,7 @@ end
and

```ruby
class CommentSerializer < ActiveModel::Serializer
class CommentSerialization < ActiveModel::Serializer
attributes :name, :body

belongs_to :post_id
Expand All @@ -274,7 +274,7 @@ as well.
You may also use the `:serializer` option to specify a custom serializer class, for example:

```ruby
has_many :comments, serializer: CommentPreviewSerializer
has_many :comments, serializer: CommentPreviewSerialization
```

And you can change the JSON key that the serializer should use for a particular association:
Expand Down Expand Up @@ -309,7 +309,7 @@ cache(options = nil) # options: ```{key, expires_in, compress, force, race_condi
Take the example bellow:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
cache key: 'post', expires_in: 3.hours
attributes :title, :body

Expand All @@ -332,7 +332,7 @@ You can define the attribute by using ```only``` or ```except``` option on cache
Example:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
cache key: 'post', expires_in: 3.hours, only: [:title]
attributes :title, :body

Expand Down
2 changes: 1 addition & 1 deletion docs/general/adapters.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Adapters

AMS does this through two components: **serializers** and **adapters**.
Serializers describe _which_ attributes and relationships should be serialized.
Serializations describe _which_ attributes and relationships should be serialized.
Adapters describe _how_ attributes and relationships should be serialized.
You can use one of the built-in adapters (```FlattenJSON``` is the default one) or create one by yourself, but you won't need to implement an adapter unless you wish to use a new format or media type with AMS.

Expand Down
6 changes: 3 additions & 3 deletions docs/general/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ And then execute:
$ bundle
```

## Creating a Serializer
## Creating a Serialization

The easiest way to create a new serializer is to generate a new resource, which
will generate a serializer at the same time:
Expand All @@ -37,7 +37,7 @@ The generated seralizer will contain basic `attributes` and
`has_many`/`has_one`/`belongs_to` declarations, based on the model. For example:

```ruby
class PostSerializer < ActiveModel::Serializer
class PostSerialization < ActiveModel::Serializer
attributes :title, :body

has_many :comments
Expand All @@ -49,7 +49,7 @@ end
and

```ruby
class CommentSerializer < ActiveModel::Serializer
class CommentSerialization < ActiveModel::Serializer
attributes :name, :body

belongs_to :post_id
Expand Down
4 changes: 2 additions & 2 deletions docs/howto/add_pagination_links.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ If you are using `JSON` adapter, pagination links will not be included automatic

In your action specify a custom serializer.
```ruby
render json: @posts, serializer: PaginatedSerializer, each_serializer: PostPreviewSerializer
render json: @posts, serializer: PaginatedSerialization, each_serializer: PostPreviewSerialization
```

And then, you could do something like the following class.
```ruby
class PaginatedSerializer < ActiveModel::Serializer::ArraySerializer
class PaginatedSerialization < ActiveModel::Serializer::ArraySerializer
def initialize(object, options={})
meta_key = options[:meta_key] || :meta
options[meta_key] ||= {}
Expand Down
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def self.digest_caller_file(caller_line)

def self.get_serializer_for(klass)
serializers_cache.fetch_or_store(klass) do
serializer_class_name = "#{klass.name}Serializer"
serializer_class_name = "#{klass.name}Serialization"
Copy link
Member Author

Choose a reason for hiding this comment

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

hmm

serializer_class = serializer_class_name.safe_constantize

if serializer_class
Expand Down
8 changes: 4 additions & 4 deletions lib/active_model/serializer/adapter/fragment_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ def cached_attributes(klass, serializers)
cached_attributes.each do |attribute|
options = serializer.class._attributes_keys[attribute]
options ||= {}
# Add cached attributes to cached Serializer
# Add cached attributes to cached Serialization
serializers[:cached].constantize.attribute(attribute, options)
end

non_cached_attributes.each do |attribute|
options = serializer.class._attributes_keys[attribute]
options ||= {}
# Add non-cached attributes to non-cached Serializer
# Add non-cached attributes to non-cached Serialization
serializers[:non_cached].constantize.attribute(attribute, options)
end
end

def fragment_serializer(name, klass)
cached = "#{to_valid_const_name(name)}CachedSerializer"
non_cached = "#{to_valid_const_name(name)}NonCachedSerializer"
cached = "#{to_valid_const_name(name)}CachedSerialization"
non_cached = "#{to_valid_const_name(name)}NonCachedSerialization"

Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached)
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_model/serializer/array_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module ActiveModel
class Serializer
class ArraySerializer
NoSerializerError = Class.new(StandardError)
NoSerializationError = Class.new(StandardError)
include Enumerable
delegate :each, to: :@serializers

Expand All @@ -16,7 +16,7 @@ def initialize(resources, options = {})
}

if serializer_class.nil?
fail NoSerializerError, "No serializer found for resource: #{resource.inspect}"
fail NoSerializationError, "No serialization found for resource: #{resource.inspect}"
else
serializer_class.new(resource, options.except(:serializer))
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/serializer/serializer_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rails
module Generators
class SerializerGenerator < NamedBase
source_root File.expand_path('../templates', __FILE__)
check_class_collision :suffix => 'Serializer'
check_class_collision :suffix => 'Serialization'

argument :attributes, :type => :array, :default => [], :banner => 'field:type field:type'

Expand Down
2 changes: 1 addition & 1 deletion lib/generators/serializer/templates/serializer.rb.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% module_namespacing do -%>
class <%= class_name %>Serializer < <%= parent_class_name %>
class <%= class_name %>Serialization < <%= parent_class_name %>
attributes <%= attributes_names.map(&:inspect).join(", ") %>
<% association_names.each do |attribute| -%>
has_one :<%= attribute %>
Expand Down
20 changes: 10 additions & 10 deletions test/action_controller/explicit_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

module ActionController
module Serialization
class ExplicitSerializerTest < ActionController::TestCase
class ExplicitSerializerTestController < ActionController::Base
class ExplicitSerializationTest < ActionController::TestCase
class ExplicitSerializationTestController < ActionController::Base
def render_using_explicit_serializer
@profile = Profile.new(name: 'Name 1',
description: 'Description 1',
comments: 'Comments 1')
render json: @profile, serializer: ProfilePreviewSerializer
render json: @profile, serializer: ProfilePreviewSerialization
end

def render_array_using_explicit_serializer
Expand All @@ -21,8 +21,8 @@ def render_array_using_explicit_serializer
comments: 'Comments 2')
]
render json: array,
serializer: PaginatedSerializer,
each_serializer: ProfilePreviewSerializer
serializer: PaginatedSerialization,
each_serializer: ProfilePreviewSerialization
end

def render_array_using_implicit_serializer
Expand All @@ -35,7 +35,7 @@ def render_array_using_implicit_serializer
comments: 'Comments 2')
]
render json: array,
each_serializer: ProfilePreviewSerializer
each_serializer: ProfilePreviewSerialization
end

def render_array_using_explicit_serializer_and_custom_serializers
Expand All @@ -53,18 +53,18 @@ def render_array_using_explicit_serializer_and_custom_serializers
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post.blog = @blog

render json: [@post], each_serializer: PostPreviewSerializer
render json: [@post], each_serializer: PostPreviewSerialization
end

def render_using_explicit_each_serializer
location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])

render json: place, each_serializer: PlaceSerializer
render json: place, each_serializer: PlaceSerialization
end
end

tests ExplicitSerializerTestController
tests ExplicitSerializationTestController

def test_render_using_explicit_serializer
get :render_using_explicit_serializer
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_render_using_explicit_each_serializer
id: 42,
lat: '-23.550520',
lng: '-46.633309',
place: 'Nowhere' # is a virtual attribute on LocationSerializer
place: 'Nowhere' # is a virtual attribute on LocationSerialization
}
]
}
Expand Down
8 changes: 4 additions & 4 deletions test/action_controller/serialization_scope_name_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'pathname'

class DefaultScopeNameTest < ActionController::TestCase
class UserSerializer < ActiveModel::Serializer
class UserSerialization < ActiveModel::Serializer
attributes :admin?
ActiveModelSerializers.silence_warnings do
def admin?
Expand All @@ -21,7 +21,7 @@ def current_user
end

def render_new_user
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerialization, adapter: :json_api
end
end

Expand All @@ -34,7 +34,7 @@ def test_default_scope_name
end

class SerializationScopeNameTest < ActionController::TestCase
class AdminUserSerializer < ActiveModel::Serializer
class AdminUserSerialization < ActiveModel::Serializer
attributes :admin?
ActiveModelSerializers.silence_warnings do
def admin?
Expand All @@ -54,7 +54,7 @@ def current_admin
end

def render_new_user
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerialization, adapter: :json_api
end
end

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

module ActionController
module Serialization
class ImplicitSerializerTest < ActionController::TestCase
class ImplicitSerializationTest < ActionController::TestCase
include ActiveSupport::Testing::Stream
class ImplicitSerializationTestController < ActionController::Base
include SerializationTesting
Expand Down
8 changes: 4 additions & 4 deletions test/adapter/fragment_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def setup
@author = Author.new(name: 'Joao M. D. Moura')
@role = Role.new(name: 'Great Author', description: nil)
@role.author = [@author]
@role_serializer = RoleSerializer.new(@role)
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
@spam_hash = FragmentCache.new(Spam::UnrelatedLinkSerializer.adapter.new(@spam_serializer), @spam_serializer, {})
@role_serializer = RoleSerialization.new(@role)
@spam_serializer = Spam::UnrelatedLinkSerialization.new(@spam)
@role_hash = FragmentCache.new(RoleSerialization.adapter.new(@role_serializer), @role_serializer, {})
@spam_hash = FragmentCache.new(Spam::UnrelatedLinkSerialization.adapter.new(@spam_serializer), @spam_serializer, {})
end

def test_fragment_fetch_with_virtual_attributes
Expand Down
Loading