pax_global_header 0000666 0000000 0000000 00000000064 12460006062 0014506 g ustar 00root root 0000000 0000000 52 comment=02c6ac96b04c0242b03dae3c0d288951dc14fe05 active_model_serializers-0.9.3/ 0000775 0000000 0000000 00000000000 12460006062 0016566 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/.gitignore 0000664 0000000 0000000 00000000250 12460006062 0020553 0 ustar 00root root 0000000 0000000 *.gem *.rbc .bundle .config .yardoc *.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp *.swp .ruby-version active_model_serializers-0.9.3/.travis.yml 0000664 0000000 0000000 00000000454 12460006062 0020702 0 ustar 00root root 0000000 0000000 language: ruby rvm: - 1.9.3 - 2.0.0 - 2.1.1 - ruby-head - jruby-19mode - rbx-2 sudo: false env: - "RAILS_VERSION=3.2.17" - "RAILS_VERSION=4.0.3" - "RAILS_VERSION=4.1.0" - "RAILS_VERSION=master" matrix: allow_failures: - rvm: ruby-head - env: "RAILS_VERSION=master" active_model_serializers-0.9.3/CHANGELOG.md 0000664 0000000 0000000 00000006142 12460006062 0020402 0 ustar 00root root 0000000 0000000 # VERSION 0.9.0.pre * The following methods were removed - Model#active\_model\_serializer - Serializer#include! - Serializer#include? - Serializer#attr\_disabled= - Serializer#cache - Serializer#perform\_caching - Serializer#schema (needs more discussion) - Serializer#attribute - Serializer#include\_#{name}? (filter method added) - Serializer#attributes (took a hash) * The following things were added - Serializer#filter method - CONFIG object * Remove support for ruby 1.8 versions. * Require rails >= 3.2. * Serializers for associations are being looked up in a parent serializer's namespace first. Same with controllers' namespaces. * Added a "prefix" option in case you want to use a different version of serializer. * Serializers default namespace can be set in `default_serializer_options` and inherited by associations. # VERSION 0.8.1 * Fix bug whereby a serializer using 'options' would blow up. # VERSION 0.8.0 * Attributes can now have optional types. * A new DefaultSerializer ensures that POROs behave the same way as ActiveModels. * If you wish to override ActiveRecord::Base#to\_Json, you can now require 'active\_record/serializer\_override'. We don't recommend you do this, but many users do, so we've left it optional. * Fixed a bug where ActionController wouldn't always have MimeResponds. * An optional caching feature allows you to cache JSON & hashes that AMS uses. Adding 'cached true' to your Serializers will turn on this cache. * URL helpers used inside of Engines now work properly. * Serializers now can filter attributes with `only` and `except`: ``` UserSerializer.new(user, only: [:first_name, :last_name]) UserSerializer.new(user, except: :first_name) ``` * Basic Mongoid support. We now include our mixins in the right place. * On Ruby 1.8, we now generate an `id` method that properly serializes `id` columns. See issue #127 for more. * Add an alias for `scope` method to be the name of the context. By default this is `current_user`. The name is automatically set when using `serialization_scope` in the controller. * Pass through serialization options (such as `:include`) when a model has no serializer defined. # VERSION 0.7.0 * ```embed_key``` option to allow embedding by attributes other than IDs * Fix rendering nil with custom serializer * Fix global ```self.root = false``` * Add support for specifying the serializer for an association as a String * Able to specify keys on the attributes method * Serializer Reloading via ActiveSupport::DescendantsTracker * Reduce double map to once; Fixes datamapper eager loading. # VERSION 0.6.0 * Serialize sets properly * Add root option to ArraySerializer * Support polymorphic associations * Support :each_serializer in ArraySerializer * Add `scope` method to easily access the scope in the serializer * Fix regression with Rails 3.2.6; add Rails 4 support * Allow serialization_scope to be disabled with serialization_scope nil * Array serializer should support pure ruby objects besides serializers # VERSION 0.5.0 * First tagged version * Changes generators to always generate an ApplicationSerializer active_model_serializers-0.9.3/CONTRIBUTING.md 0000664 0000000 0000000 00000001123 12460006062 0021014 0 ustar 00root root 0000000 0000000 Contributing to AMS =================== First of all, **thank you**! Now, for the details: Please file issues on the [GitHub Issues list](https://github.com/rails-api/active_model_serializers/issues). Please discuss new features or ask for feedback about a new feature [on rails-api-core](https://groups.google.com/forum/#!forum/rails-api-core). If you want a feature implemented, the best way to get it done is to submit a pull request that implements it. Tests and docs would be nice. Please include a CHANGELOG with all entries that change behavior. :heart: :sparkling_heart: :heart: active_model_serializers-0.9.3/DESIGN.textile 0000664 0000000 0000000 00000046103 12460006062 0021143 0 ustar 00root root 0000000 0000000 This was the original design document for serializers. It is useful mostly for historical purposes as the public API has changed. h2. Rails Serializers This guide describes how to use Active Model serializers to build non-trivial JSON services in Rails. By reading this guide, you will learn: * When to use the built-in Active Model serialization * When to use a custom serializer for your models * How to use serializers to encapsulate authorization concerns * How to create serializer templates to describe the application-wide structure of your serialized JSON * How to build resources not backed by a single database table for use with JSON services This guide covers an intermediate topic and assumes familiarity with Rails conventions. It is suitable for applications that expose a JSON API that may return different results based on the authorization status of the user. h3. Serialization By default, Active Record objects can serialize themselves into JSON by using the `to_json` method. This method takes a series of additional parameter to control which properties and associations Rails should include in the serialized output. When building a web application that uses JavaScript to retrieve JSON data from the server, this mechanism has historically been the primary way that Rails developers prepared their responses. This works great for simple cases, as the logic for serializing an Active Record object is neatly encapsulated in Active Record itself. However, this solution quickly falls apart in the face of serialization requirements based on authorization. For instance, a web service may choose to expose additional information about a resource only if the user is entitled to access it. In addition, a JavaScript front-end may want information that is not neatly described in terms of serializing a single Active Record object, or in a different format than. In addition, neither the controller nor the model seems like the correct place for logic that describes how to serialize an model object *for the current user*. Serializers solve these problems by encapsulating serialization in an object designed for this purpose. If the default +to_json+ semantics, with at most a few configuration options serve your needs, by all means continue to use the built-in +to_json+. If you find yourself doing hash-driven-development in your controllers, juggling authorization logic and other concerns, serializers are for you! h3. The Most Basic Serializer A basic serializer is a simple Ruby object named after the model class it is serializing.
class PostSerializer
def initialize(post, scope)
@post, @scope = post, scope
end
def as_json
{ post: { title: @post.name, body: @post.body } }
end
end
A serializer is initialized with two parameters: the model object it should serialize and an authorization scope. By default, the
authorization scope is the current user (+current_user+) but you can use a different object if you want. The serializer also
implements an +as_json+ method, which returns a Hash that will be sent to the JSON encoder.
Rails will transparently use your serializer when you use +render :json+ in your controller.
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
render json: @post
end
end
Because +respond_with+ uses +render :json+ under the hood for JSON requests, Rails will automatically use your serializer when
you use +respond_with+ as well.
h4. +serializable_hash+
In general, you will want to implement +serializable_hash+ and +as_json+ to allow serializers to embed associated content
directly. The easiest way to implement these two methods is to have +as_json+ call +serializable_hash+ and insert the root.
class PostSerializer
def initialize(post, scope)
@post, @scope = post, scope
end
def serializable_hash
{ title: @post.name, body: @post.body }
end
def as_json
{ post: serializable_hash }
end
end
h4. Authorization
Let's update our serializer to include the email address of the author of the post, but only if the current user has superuser
access.
class PostSerializer
def initialize(post, scope)
@post, @scope = post, scope
end
def as_json
{ post: serializable_hash }
end
def serializable_hash
hash = post
hash.merge!(super_data) if super?
hash
end
private
def post
{ title: @post.name, body: @post.body }
end
def super_data
{ email: @post.email }
end
def super?
@scope.superuser?
end
end
h4. Testing
One benefit of encapsulating our objects this way is that it becomes extremely straight-forward to test the serialization
logic in isolation.
require "ostruct"
class PostSerializerTest < ActiveSupport::TestCase
# For now, we use a very simple authorization structure. These tests will need
# refactoring if we change that.
plebe = OpenStruct.new(super?: false)
god = OpenStruct.new(super?: true)
post = OpenStruct.new(title: "Welcome to my blog!", body: "Blah blah blah", email: "tenderlove@gmail.com")
test "a regular user sees just the title and body" do
json = PostSerializer.new(post, plebe).to_json
hash = JSON.parse(json)
assert_equal post.title, hash.delete("title")
assert_equal post.body, hash.delete("body")
assert_empty hash
end
test "a superuser sees the title, body and email" do
json = PostSerializer.new(post, god).to_json
hash = JSON.parse(json)
assert_equal post.title, hash.delete("title")
assert_equal post.body, hash.delete("body")
assert_equal post.email, hash.delete("email")
assert_empty hash
end
end
It's important to note that serializer objects define a clear interface specifically for serializing an existing object.
In this case, the serializer expects to receive a post object with +name+, +body+ and +email+ attributes and an authorization
scope with a +super?+ method.
By defining a clear interface, it's must easier to ensure that your authorization logic is behaving correctly. In this case,
the serializer doesn't need to concern itself with how the authorization scope decides whether to set the +super?+ flag, just
whether it is set. In general, you should document these requirements in your serializer files and programatically via tests.
The documentation library +YARD+ provides excellent tools for describing this kind of requirement:
class PostSerializer
# @param [~body, ~title, ~email] post the post to serialize
# @param [~super] scope the authorization scope for this serializer
def initialize(post, scope)
@post, @scope = post, scope
end
# ...
end
h3. Attribute Sugar
To simplify this process for a number of common cases, Rails provides a default superclass named +ActiveModel::Serializer+
that you can use to implement your serializers.
For example, you will sometimes want to simply include a number of existing attributes from the source model into the outputted
JSON. In the above example, the +title+ and +body+ attributes were always included in the JSON. Let's see how to use
+ActiveModel::Serializer+ to simplify our post serializer.
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
def serializable_hash
hash = attributes
hash.merge!(super_data) if super?
hash
end
private
def super_data
{ email: @post.email }
end
def super?
@scope.superuser?
end
end
First, we specified the list of included attributes at the top of the class. This will create an instance method called
+attributes+ that extracts those attributes from the post model.
NOTE: Internally, +ActiveModel::Serializer+ uses +read_attribute_for_serialization+, which defaults to +read_attribute+, which defaults to +send+. So if you're rolling your own models for use with the serializer, you can use simple Ruby accessors for your attributes if you like.
Next, we use the attributes method in our +serializable_hash+ method, which allowed us to eliminate the +post+ method we hand-rolled
earlier. We could also eliminate the +as_json+ method, as +ActiveModel::Serializer+ provides a default +as_json+ method for
us that calls our +serializable_hash+ method and inserts a root. But we can go a step further!
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
private
def attributes
hash = super
hash.merge!(email: post.email) if super?
hash
end
def super?
@scope.superuser?
end
end
The superclass provides a default +initialize+ method as well as a default +serializable_hash+ method, which uses
+attributes+. We can call +super+ to get the hash based on the attributes we declared, and then add in any additional
attributes we want to use.
NOTE: +ActiveModel::Serializer+ will create an accessor matching the name of the current class for the resource you pass in. In this case, because we have defined a PostSerializer, we can access the resource with the +post+ accessor.
h3. Associations
In most JSON APIs, you will want to include associated objects with your serialized object. In this case, let's include
the comments with the current post.
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
private
def attributes
hash = super
hash.merge!(email: post.email) if super?
hash
end
def super?
@scope.superuser?
end
end
The default +serializable_hash+ method will include the comments as embedded objects inside the post.
{
post: {
title: "Hello Blog!",
body: "This is my first post. Isn't it fabulous!",
comments: [
{
title: "Awesome",
body: "Your first post is great"
}
]
}
}
Rails uses the same logic to generate embedded serializations as it does when you use +render :json+. In this case,
because you didn't define a +CommentSerializer+, Rails used the default +as_json+ on your comment object.
If you define a serializer, Rails will automatically instantiate it with the existing authorization scope.
class CommentSerializer
def initialize(comment, scope)
@comment, @scope = comment, scope
end
def serializable_hash
{ title: @comment.title }
end
def as_json
{ comment: serializable_hash }
end
end
If we define the above comment serializer, the outputted JSON will change to:
{
post: {
title: "Hello Blog!",
body: "This is my first post. Isn't it fabulous!",
comments: [{ title: "Awesome" }]
}
}
Let's imagine that our comment system allows an administrator to kill a comment, and we only want to allow
users to see the comments they're entitled to see. By default, +has_many :comments+ will simply use the
+comments+ accessor on the post object. We can override the +comments+ accessor to limit the comments used
to just the comments we want to allow for the current user.
class PostSerializer < ActiveModel::Serializer
attributes :title. :body
has_many :comments
private
def attributes
hash = super
hash.merge!(email: post.email) if super?
hash
end
def comments
post.comments_for(scope)
end
def super?
@scope.superuser?
end
end
+ActiveModel::Serializer+ will still embed the comments, but this time it will use just the comments
for the current user.
NOTE: The logic for deciding which comments a user should see still belongs in the model layer. In general, you should encapsulate concerns that require making direct Active Record queries in scopes or public methods on your models.
h4. Modifying Associations
You can also rename associations if required. Say for example you have an association that
makes sense to be named one thing in your code, but another when data is serialized.
You can use the
class UserSerializer < ActiveModel::Serializer
has_many :followed_posts, key: :posts
has_one :owned_account, key: :account
end
Using the :key
without a :serializer
option will use implicit detection
to determine a serializer. In this example, you'd have to define two classes: PostSerializer
and AccountSerializer
. You can also add the :serializer
option
to set it explicitly:
class UserSerializer < ActiveModel::Serializer
has_many :followed_posts, key: :posts, serializer: CustomPostSerializer
has_one :owne_account, key: :account, serializer: PrivateAccountSerializer
end
h3. Customizing Associations
Not all front-ends expect embedded documents in the same form. In these cases, you can override the
default +serializable_hash+, and use conveniences provided by +ActiveModel::Serializer+ to avoid having to
build up the hash manually.
For example, let's say our front-end expects the posts and comments in the following format:
{
post: {
id: 1
title: "Hello Blog!",
body: "This is my first post. Isn't it fabulous!",
comments: [1,2]
},
comments: [
{
id: 1
title: "Awesome",
body: "Your first post is great"
},
{
id: 2
title: "Not so awesome",
body: "Why is it so short!"
}
]
}
We could achieve this with a custom +as_json+ method. We will also need to define a serializer for comments.
class CommentSerializer < ActiveModel::Serializer
attributes :id, :title, :body
# define any logic for dealing with authorization-based attributes here
end
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
def as_json
{ post: serializable_hash }.merge!(associations)
end
def serializable_hash
post_hash = attributes
post_hash.merge!(association_ids)
post_hash
end
private
def attributes
hash = super
hash.merge!(email: post.email) if super?
hash
end
def comments
post.comments_for(scope)
end
def super?
@scope.superuser?
end
end
Here, we used two convenience methods: +associations+ and +association_ids+. The first,
+associations+, creates a hash of all of the define associations, using their defined
serializers. The second, +association_ids+, generates a hash whose key is the association
name and whose value is an Array of the association's keys.
The +association_ids+ helper will use the overridden version of the association, so in
this case, +association_ids+ will only include the ids of the comments provided by the
+comments+ method.
h3. Special Association Serializers
So far, associations defined in serializers use either the +as_json+ method on the model
or the defined serializer for the association type. Sometimes, you may want to serialize
associated models differently when they are requested as part of another resource than
when they are requested on their own.
For instance, we might want to provide the full comment when it is requested directly,
but only its title when requested as part of the post. To achieve this, you can define
a serializer for associated objects nested inside the main serializer.
class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer
attributes :id, :title
end
# same as before
# ...
end
In other words, if a +PostSerializer+ is trying to serialize comments, it will first
look for +PostSerializer::CommentSerializer+ before falling back to +CommentSerializer+
and finally +comment.as_json+.
h3. Overriding the Defaults
h4. Authorization Scope
By default, the authorization scope for serializers is +:current_user+. This means
that when you call +render json: @post+, the controller will automatically call
its +current_user+ method and pass that along to the serializer's initializer.
If you want to change that behavior, simply use the +serialization_scope+ class
method.
class PostsController < ApplicationController
serialization_scope :current_app
end
You can also implement an instance method called (no surprise) +serialization_scope+,
which allows you to define a dynamic authorization scope based on the current request.
WARNING: If you use different objects as authorization scopes, make sure that they all implement whatever interface you use in your serializers to control what the outputted JSON looks like.
h3. Using Serializers Outside of a Request
The serialization API encapsulates the concern of generating a JSON representation of
a particular model for a particular user. As a result, you should be able to easily use
serializers, whether you define them yourself or whether you use +ActiveModel::Serializer+
outside a request.
For instance, if you want to generate the JSON representation of a post for a user outside
of a request:
user = get_user # some logic to get the user in question
PostSerializer.new(post, user).to_json # reliably generate JSON output
If you want to generate JSON for an anonymous user, you should be able to use whatever
technique you use in your application to generate anonymous users outside of a request.
Typically, that means creating a new user and not saving it to the database:
user = User.new # create a new anonymous user
PostSerializer.new(post, user).to_json
In general, the better you encapsulate your authorization logic, the more easily you
will be able to use the serializer outside of the context of a request. For instance,
if you use an authorization library like Cancan, which uses a uniform +user.can?(action, model)+,
the authorization interface can very easily be replaced by a plain Ruby object for
testing or usage outside the context of a request.
h3. Collections
So far, we've talked about serializing individual model objects. By default, Rails
will serialize collections, including when using the +associations+ helper, by
looping over each element of the collection, calling +serializable_hash+ on the element,
and then grouping them by their type (using the plural version of their class name
as the root).
For example, an Array of post objects would serialize as:
{
posts: [
{
title: "FIRST POST!",
body: "It's my first pooooost"
},
{ title: "Second post!",
body: "Zomg I made it to my second post"
}
]
}
If you want to change the behavior of serialized Arrays, you need to create
a custom Array serializer.
class ArraySerializer < ActiveModel::ArraySerializer
def serializable_array
serializers.map do |serializer|
serializer.serializable_hash
end
end
def as_json
hash = { root => serializable_array }
hash.merge!(associations)
hash
end
end
When generating embedded associations using the +associations+ helper inside a
regular serializer, it will create a new ArraySerializer
with the
associated content and call its +serializable_array+ method. In this case, those
embedded associations will not recursively include associations.
When generating an Array using +render json: posts+, the controller will invoke
the +as_json+ method, which will include its associations and its root.
active_model_serializers-0.9.3/Gemfile 0000664 0000000 0000000 00000001000 12460006062 0020050 0 ustar 00root root 0000000 0000000 source 'https://rubygems.org'
gemspec
platforms :ruby do
# sqlite3 1.3.9 does not work with rubinius 2.2.5:
# https://github.com/sparklemotion/sqlite3-ruby/issues/122
gem 'sqlite3', '1.3.8'
end
platforms :jruby do
gem 'activerecord-jdbcsqlite3-adapter'
end
version = ENV["RAILS_VERSION"] || "4.0.2"
rails = case version
when "master"
{:github => "rails/rails"}
else
"~> #{version}"
end
gem "rails", rails
if version < "4"
gem "minitest", "~> 4.7.5"
end
active_model_serializers-0.9.3/MIT-LICENSE 0000664 0000000 0000000 00000002063 12460006062 0020223 0 ustar 00root root 0000000 0000000 Copyright (c) 2011-2012 José Valim & Yehuda Katz
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
active_model_serializers-0.9.3/README.md 0000664 0000000 0000000 00000056711 12460006062 0020057 0 ustar 00root root 0000000 0000000 [](https://travis-ci.org/rails-api/active_model_serializers)
[](https://codeclimate.com/github/rails-api/active_model_serializers)
# ActiveModel::Serializers
## Purpose
`ActiveModel::Serializers` encapsulates the JSON serialization of objects.
Objects that respond to read\_attribute\_for\_serialization
(including `ActiveModel` and `ActiveRecord` objects) are supported.
Serializers know about both a model and the `current_user`, so you can
customize serialization based upon whether a user is authorized to see the
content.
In short, **serializers replace hash-driven development with object-oriented
development.**
# Installing
The easiest way to install `ActiveModel::Serializers` is to add it to your
`Gemfile`:
```ruby
gem "active_model_serializers"
```
Then, install it on the command line:
```
$ bundle install
```
#### Ruby 1.8 is no longer supported!
If you must use a ruby 1.8 version (MRI 1.8.7, REE, Rubinius 1.8, or JRuby 1.8), you need to use version 0.8.x.
Versions after 0.9.0 do not support ruby 1.8. To specify version 0.8, include this in your Gemfile:
```ruby
gem "active_model_serializers", "~> 0.8.0"
```
# Creating a Serializer
The easiest way to create a new serializer is to generate a new resource, which
will generate a serializer at the same time:
```
$ rails g resource post title:string body:string
```
This will generate a serializer in `app/serializers/post_serializer.rb` for
your new model. You can also generate a serializer for an existing model with
the serializer generator:
```
$ rails g serializer post
```
### Support for POROs
The PORO should include ActiveModel::SerializerSupport. That's all you need to
do to have your POROs supported.
For Rails versions before Rails 4 ActiveModel::Serializers expects objects to
implement `read_attribute_for_serialization`.
# render :json
In your controllers, when you use `render :json`, Rails will now first search
for a serializer for the object and use it if available.
```ruby
class PostsController < ApplicationController
def show
@post = Post.find(params[:id])
render json: @post
end
end
```
In this case, Rails will look for a serializer named `PostSerializer`, and if
it exists, use it to serialize the `Post`.
This also works with `respond_with`, which uses `to_json` under the hood. Also
note that any options passed to `render :json` will be passed to your
serializer and available as `@options` inside.
To specify a custom serializer for an object, you can specify the
serializer when you render the object:
```ruby
render json: @post, serializer: FancyPostSerializer
```
### Use serialization outside of ActionController::Base
When controller does not inherit from ActionController::Base,
include Serialization module manually:
```ruby
class ApplicationController < ActionController::API
include ActionController::Serialization
end
```
## Arrays
In your controllers, when you use `render :json` for an array of objects, AMS will
use `ActiveModel::ArraySerializer` (included in this project) as the base serializer,
and the individual `Serializer` for the objects contained in that array.
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
end
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
end
```
Given the example above, the index action will return
```json
{
"posts":
[
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
}
```
By default, the root element is the name of the controller. For example, `PostsController`
generates a root element "posts". To change it:
```ruby
render json: @posts, root: "some_posts"
```
You may disable the root element for arrays at the top level, which will result in
more concise json. See the next section for ways on how to do this. Disabling the
root element of the array with any of those methods will produce
```json
[
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
```
To specify a custom serializer for the items within an array:
```ruby
render json: @posts, each_serializer: FancyPostSerializer
```
## Render independently
By default the setting of serializer is in controller as described above which is the
recommended way. However, there may be cases you need to render the json object elsewhere
say in a helper or a view when controller is only for main object.
Then you can render the serialized JSON independently.
```ruby
def current_user_as_json_helper
CurrentUserSerializer.new(current_user).to_json
end
```
You can also render an array of objects using ArraySerializer.
```ruby
def users_array_as_json_helper(users)
ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
end
```
## Disabling the root element
You have 4 options to disable the root element, each with a slightly different scope:
#### 1. Disable root globally for all, or per class
In an initializer:
```ruby
# Disable for all serializers (except ArraySerializer)
ActiveModel::Serializer.root = false
# Disable for ArraySerializer
ActiveModel::ArraySerializer.root = false
```
#### 2. Disable root per render call in your controller
```ruby
render json: @posts, root: false
```
#### 3. Subclass the serializer, and specify using it
```ruby
class CustomArraySerializer < ActiveModel::ArraySerializer
self.root = false
end
# controller:
render json: @posts, serializer: CustomArraySerializer
```
#### 4. Define default_serializer_options in your controller
If you define `default_serializer_options` method in your controller,
all serializers in actions of this controller and it's children will use them.
One of the options may be `root: false`
```ruby
def default_serializer_options
{
root: false
}
end
```
## Changing the Key Format
You can specify that serializers use the lower-camel key format at the config, class or instance level.
```ruby
ActiveModel::Serializer.setup do |config|
config.key_format = :lower_camel
end
class BlogLowerCamelSerializer < ActiveModel::Serializer
format_keys :lower_camel
end
BlogSerializer.new(object, key_format: :lower_camel)
```
## Changing the default association key type
You can specify that serializers use unsuffixed names as association keys by default.
`````ruby
ActiveModel::Serializer.setup do |config|
config.default_key_type = :name
end
````
This will build association keys like `comments` or `author` instead of `comment_ids` or `author_id`.
## Getting the old version
If you find that your project is already relying on the old rails to_json
change `render :json` to `render json: @your_object.to_json`.
# Attributes and Associations
Once you have a serializer, you can specify which attributes and associations
you would like to include in the serialized form.
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
end
```
## Attributes
For specified attributes, a serializer will look up the attribute on the
object you passed to `render :json`. It uses
`read_attribute_for_serialization`, which `ActiveRecord` objects implement as a
regular attribute lookup.
Before looking up the attribute on the object, a serializer will check for the
presence of a method with the name of the attribute. This allows serializers to
include properties beyond the simple attributes of the model. For example:
```ruby
class PersonSerializer < ActiveModel::Serializer
attributes :first_name, :last_name, :full_name
def full_name
"#{object.first_name} #{object.last_name}"
end
end
```
Within a serializer's methods, you can access the object being
serialized as `object`.
Since this shadows any attribute named `object`, you can include them through `object.object`. For example:
```ruby
class VersionSerializer < ActiveModel::Serializer
attributes :version_object
def version_object
object.object
end
end
```
You can also access the `scope` method, which provides an
authorization context to your serializer. By default, the context
is the current user of your application, but this
[can be customized](#customizing-scope).
Serializers provide a method named `filter`, which should return an array
used to determine what attributes and associations should be included in the output.
This is typically used to customize output based on `current_user`. For example:
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body, :author
def filter(keys)
if scope.admin?
keys
else
keys - [:author]
end
end
end
```
And it's also safe to mutate keys argument by doing keys.delete(:author)
in case you want to avoid creating two extra arrays. Note that if you do an
in-place modification, you still need to return the modified array.
If you would like the key in the outputted JSON to be different from its name
in ActiveRecord, you can declare the attribute with the different name
and redefine that method:
```ruby
class PostSerializer < ActiveModel::Serializer
# look up subject on the model, but use title in the JSON
def title
object.subject
end
attributes :id, :body, :title
has_many :comments
end
```
If you would like to add meta information to the outputted JSON, use the `:meta`
option:
```ruby
render json: @posts, serializer: CustomArraySerializer, meta: {total: 10}
```
The above usage of `:meta` will produce the following:
```json
{
"meta": { "total": 10 },
"posts": [
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
}
```
If you would like to change the meta key name you can use the `:meta_key` option:
```ruby
render json: @posts, serializer: CustomArraySerializer, meta_object: {total: 10}, meta_key: 'meta_object'
```
The above usage of `:meta_key` will produce the following:
```json
{
"meta_object": { "total": 10 },
"posts": [
{ "title": "Post 1", "body": "Hello!" },
{ "title": "Post 2", "body": "Goodbye!" }
]
}
```
When using meta information, your serializer cannot have the `{ root: false }` option, as this would lead to
invalid JSON. If you do not have a root key, the meta information will be ignored.
If you would like direct, low-level control of attribute serialization, you can
completely override the `attributes` method to return the hash you need:
```ruby
class PersonSerializer < ActiveModel::Serializer
attributes :first_name, :last_name
def attributes
hash = super
if scope.admin?
hash["ssn"] = object.ssn
hash["secret"] = object.mothers_maiden_name
end
hash
end
end
```
## Associations
For specified associations, the serializer will look up the association and
then serialize each element of the association. For instance, a `has_many
:comments` association will create a new `CommentSerializer` for each comment
and use it to serialize the comment.
By default, serializers simply look up the association on the original object.
You can customize this behavior by implementing a method with the name of the
association and returning a different Array. Often, you will do this to
customize the objects returned based on the current user (scope).
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
# only let the user see comments he created.
def comments
object.comments.where(created_by: scope)
end
end
```
As with attributes, you can change the JSON key that the serializer should
use for a particular association.
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
# look up comments, but use +my_comments+ as the key in JSON
has_many :comments, root: :my_comments
end
```
Also, as with attributes, serializers will execute a filter method to
determine which associations should be included in the output. For
example:
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments
def filter(keys)
keys.delete :comments if object.comments_disabled?
keys
end
end
```
Or ...
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_one :author
has_many :comments
def filter(keys)
keys.delete :author unless scope.admin?
keys.delete :comments if object.comments_disabled?
keys
end
end
```
You may also use the `:serializer` option to specify a custom serializer class and the `:polymorphic` option to specify an association that is polymorphic (STI), e.g.:
```ruby
has_many :comments, serializer: CommentShortSerializer
has_one :reviewer, polymorphic: true
```
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
## Embedding Associations
By default, associations will be embedded inside the serialized object. So if
you have a post, the outputted JSON will look like:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comments": [
{ "id": 1, "body": "what a dumb post" }
]
}
}
```
This is convenient for simple use-cases, but for more complex clients, it is
better to supply an Array of IDs for the association. This makes your API more
flexible from a performance standpoint and avoids wasteful duplication.
To embed IDs instead of associations, simply use the `embed` class method:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :title, :body
has_many :comments
end
```
Now, any associations will be supplied as an Array of IDs:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ 1, 2, 3 ]
}
}
```
You may also choose to embed the IDs by the association's name underneath a
`key` for the resource. For example, say we want to change `comment_ids`
to `comments` underneath a `links` key:
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments, embed: :ids, key: :comments, embed_namespace: :links
end
```
The JSON will look like this:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"links": {
"comments": [ 1, 2, 3 ]
}
}
}
```
Alternatively, you can choose to embed only the ids or the associated objects per association:
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :body
has_many :comments, embed: :objects
has_many :tags, embed: :ids
end
```
The JSON will look like this:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comments": [
{ "id": 1, "body": "what a dumb post" }
],
"tag_ids": [ 1, 2, 3 ]
}
}
```
In addition to supplying an Array of IDs, you may want to side-load the data
alongside the main object. This makes it easier to process the entire package
of data without having to recursively scan the tree looking for embedded
information. It also ensures that associations that are shared between several
objects (like tags), are only delivered once for the entire payload.
You can specify that the data be included like this:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id, :title, :body
has_many :comments
end
```
Assuming that the comments also `has_many :tags`, you will get a JSON like
this:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ 1, 2 ]
},
"comments": [
{ "id": 1, "body": "what a dumb post", "tag_ids": [ 1, 2 ] },
{ "id": 2, "body": "i liked it", "tag_ids": [ 1, 3 ] },
],
"tags": [
{ "id": 1, "name": "short" },
{ "id": 2, "name": "whiny" },
{ "id": 3, "name": "happy" }
]
}
```
If you would like to namespace association JSON underneath a certain key in
the root document (say, `linked`), you can specify an `embed_in_root_key`:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids, include: true, embed_in_root_key: :linked
attributes: :id, :title, :body
has_many :comments, :tags
end
```
The above would yield the following JSON document:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ 1, 2 ]
},
"linked": {
"comments": [
{ "id": 1, "body": "what a dumb post", "tag_ids": [ 1, 2 ] },
{ "id": 2, "body": "i liked it", "tag_ids": [ 1, 3 ] },
],
"tags": [
{ "id": 1, "name": "short" },
{ "id": 2, "name": "whiny" },
{ "id": 3, "name": "happy" }
]
}
}
```
When side-loading data, your serializer cannot have the `{ root: false }` option,
as this would lead to invalid JSON. If you do not have a root key, the `include`
instruction will be ignored
You can also specify a different root for the embedded objects than the key
used to reference them:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id, :title, :body
has_many :comments, key: :comment_ids, root: :comment_objects
end
```
This would generate JSON that would look like this:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ 1 ]
},
"comment_objects": [
{ "id": 1, "body": "what a dumb post" }
]
}
```
You can also specify a different attribute to use rather than the ID of the
objects:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids, include: true
attributes :id, :title, :body
has_many :comments, key: :external_id
end
```
This would generate JSON that would look like this:
```json
{
"post": {
"id": 1,
"title": "New post",
"body": "A body!",
"comment_ids": [ "COMM001" ]
},
"comments": [
{ "id": 1, "external_id": "COMM001", "body": "what a dumb post" }
]
}
```
**NOTE**: The `embed :ids` mechanism is primary useful for clients that process
data in bulk and load it into a local store. For these clients, the ability to
easily see all of the data per type, rather than having to recursively scan the
data looking for information, is extremely useful.
If you are mostly working with the data in simple scenarios and manually making
Ajax requests, you probably just want to use the default embedded behavior.
## Embedding Polymorphic Associations
Because we need both the id and the type to be able to identify a polymorphic associated model, these are serialized in a slightly different format than common ones.
When embedding entire objects:
```ruby
class PostSerializer < ActiveModel::Serializer
attributes :id, :title
has_many :attachments, polymorphic: true
end
```
```json
{
"post": {
"id": 1,
"title": "New post",
"attachments": [
{
"type": "image"
"image": {
"id": 3
"name": "logo"
"url": "http://images.com/logo.jpg"
}
},
{
"type": "video"
"video": {
"id": 12
"uid": "XCSSMDFWW"
"source": "youtube"
}
}
]
}
}
```
When embedding ids:
```ruby
class PostSerializer < ActiveModel::Serializer
embed :ids
attributes :id, :title
has_many :attachments, polymorphic: true
end
```
```json
{
"post": {
"id": 1,
"title": "New post",
"attachment_ids": [
{
"type": "image"
"id": 12
},
{
"type": "video"
"id": 3
}
]
}
}
```
## Customizing Scope
In a serializer, `current_user` is the current authorization scope which the controller
provides to the serializer when you call `render :json`. By default, this is
`current_user`, but can be customized in your controller by calling
`serialization_scope`:
```ruby
class ApplicationController < ActionController::Base
serialization_scope :current_admin
end
```
The above example will also change the scope from `current_user` to
`current_admin`.
Please note that, until now, `serialization_scope` doesn't accept a second
object with options for specifying which actions should or should not take a
given scope in consideration.
To be clear, it's not possible, yet, to do something like this:
```ruby
class SomeController < ApplicationController
serialization_scope :current_admin, except: [:index, :show]
end
```
So, in order to have a fine grained control of what each action should take in
consideration for its scope, you may use something like this:
```ruby
class CitiesController < ApplicationController
serialization_scope nil
def index
@cities = City.all
render json: @cities, each_serializer: CitySerializer
end
def show
@city = City.find(params[:id])
render json: @city, scope: current_admin
end
end
```
Assuming that the `current_admin` method needs to make a query in the database
for the current user, the advantage of this approach is that, by setting
`serialization_scope` to `nil`, the `index` action no longer will need to make
that query, only the `show` action will.
## Testing
In order to test a Serializer, you can just call `.new` on it, passing the object to serialize:
### MiniTest
```ruby
class TestPostSerializer < Minitest::Test
def setup
@serializer = PostSerializer.new Post.new(id: 123, title: 'some title', body: 'some text')
end
def test_special_json_for_api
assert_equal '{"post":{"id":123,"title":"some title","body":"some text"}}', @serializer.to_json
end
```
### RSpec
```ruby
describe PostSerializer do
it "creates special JSON for the API" do
serializer = PostSerializer.new Post.new(id: 123, title: 'some title', body: 'some text')
expect(serializer.to_json).to eql('{"post":{"id":123,"title":"some title","body":"some text"}}')
end
end
```
## Caching
NOTE: This functionality was removed from AMS and it's in the TODO list.
We need to re-think and re-design the caching strategy for the next
version of AMS.
To cache a serializer, call `cached` and define a `cache_key` method:
```ruby
class PostSerializer < ActiveModel::Serializer
cached # enables caching for this serializer
attributes :title, :body
def cache_key
[object, scope]
end
end
```
The caching interface uses `Rails.cache` under the hood.
# ApplicationSerializer
By default, new serializers descend from ActiveModel::Serializer. However, if you wish to share behaviour across your serializers you can create an ApplicationSerializer at ```app/serializers/application_serializer.rb```:
```ruby
class ApplicationSerializer < ActiveModel::Serializer
end
```
Any newly generated serializers will automatically descend from ApplicationSerializer.
```
$ rails g serializer post
```
now generates:
```ruby
class PostSerializer < ApplicationSerializer
attributes :id
end
````
# Design and Implementation Guidelines
## Keep it Simple
`ActiveModel::Serializers` is capable of producing complex JSON views/large object
trees, and it may be tempting to design in this way so that your client can make
fewer requests to get data and so that related querying can be optimized.
However, keeping things simple in your serializers and controllers may
significantly reduce complexity and maintenance over the long-term development
of your application. Please consider reducing the complexity of the JSON views
you provide via the serializers as you build out your application, so that
controllers/services can be more easily reused without a lot of complexity
later.
## Performance
As you develop your controllers or other code that utilizes serializers, try to
avoid n+1 queries by ensuring that data loads in an optimal fashion, e.g. if you
are using ActiveRecord, you might want to use query includes or joins as needed
to make the data available that the serializer(s) need.
active_model_serializers-0.9.3/Rakefile 0000664 0000000 0000000 00000000744 12460006062 0020240 0 ustar 00root root 0000000 0000000 #!/usr/bin/env rake
require "bundler/gem_tasks"
require "rake/testtask"
desc 'Run tests'
test_task = Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
task default: :test
desc 'Run tests in isolated processes'
namespace :test do
task :isolated do
Dir[test_task.pattern].each do |file|
cmd = ['ruby']
test_task.libs.each { |l| cmd << '-I' << l }
cmd << file
sh cmd.join(' ')
end
end
end
active_model_serializers-0.9.3/active_model_serializers.gemspec 0000664 0000000 0000000 00000002104 12460006062 0025177 0 ustar 00root root 0000000 0000000 # -*- encoding: utf-8 -*-
$:.unshift File.expand_path("../lib", __FILE__)
require "active_model/serializer/version"
Gem::Specification.new do |gem|
gem.authors = ["José Valim", "Yehuda Katz", "Santiago Pastorino"]
gem.email = ["jose.valim@gmail.com", "wycats@gmail.com", "santiago@wyeworks.com"]
gem.description = %q{Making it easy to serialize models for client-side use}
gem.summary = %q{Bringing consistency and object orientation to model serialization. Works great for client-side MVC frameworks!}
gem.homepage = "https://github.com/rails-api/active_model_serializers"
gem.license = 'MIT'
gem.files = Dir['README.md', 'CHANGELOG.md', 'CONTRIBUTING.md', 'DESIGN.textile', 'MIT-LICENSE', 'lib/**/*', 'test/**/*']
gem.test_files = Dir['test/**/*']
gem.name = "active_model_serializers"
gem.require_paths = ["lib"]
gem.version = ActiveModel::Serializer::VERSION
gem.required_ruby_version = ">= 1.9.3"
gem.add_dependency "activemodel", ">= 3.2"
gem.add_development_dependency "rails", ">= 3.2"
end
active_model_serializers-0.9.3/lib/ 0000775 0000000 0000000 00000000000 12460006062 0017334 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/action_controller/ 0000775 0000000 0000000 00000000000 12460006062 0023054 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/action_controller/serialization.rb 0000664 0000000 0000000 00000005506 12460006062 0026264 0 ustar 00root root 0000000 0000000 require 'active_support/core_ext/class/attribute'
module ActionController
# Action Controller Serialization
#
# Overrides render :json to check if the given object implements +active_model_serializer+
# as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
#
# This module also provides a serialization_scope method that allows you to configure the
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
# to the current user:
#
# class ApplicationController < ActionController::Base
# serialization_scope :current_user
# end
#
# If you need more complex scope rules, you can simply override the serialization_scope:
#
# class ApplicationController < ActionController::Base
# private
#
# def serialization_scope
# current_user
# end
# end
#
module Serialization
extend ActiveSupport::Concern
include ActionController::Renderers
class << self
attr_accessor :enabled
end
self.enabled = true
included do
class_attribute :_serialization_scope
self._serialization_scope = :current_user
end
module ClassMethods
def serialization_scope(scope)
self._serialization_scope = scope
end
end
[:_render_option_json, :_render_with_renderer_json].each do |renderer_method|
define_method renderer_method do |resource, options|
serializer = build_json_serializer(resource, options)
if serializer
super(serializer, options)
else
super(resource, options)
end
end
end
private
def namespace_for_serializer
@namespace_for_serializer ||= self.class.parent unless self.class.parent == Object
end
def default_serializer(resource)
options = {}.tap do |o|
o[:namespace] = namespace_for_serializer if namespace_for_serializer
end
ActiveModel::Serializer.serializer_for(resource, options)
end
def default_serializer_options
{}
end
def serialization_scope
_serialization_scope = self.class._serialization_scope
send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
end
def build_json_serializer(resource, options = {})
options = default_serializer_options.merge(options)
@namespace_for_serializer = options.fetch(:namespace, nil)
if serializer = options.fetch(:serializer, default_serializer(resource))
options[:scope] = serialization_scope unless options.has_key?(:scope)
if resource.respond_to?(:to_ary)
options[:resource_name] = controller_name
options[:namespace] = namespace_for_serializer if namespace_for_serializer
end
serializer.new(resource, options)
end
end
end
end
active_model_serializers-0.9.3/lib/action_controller/serialization_test_case.rb 0000664 0000000 0000000 00000005571 12460006062 0030320 0 ustar 00root root 0000000 0000000 module ActionController
module SerializationAssertions
extend ActiveSupport::Concern
included do
setup :setup_serialization_subscriptions
teardown :teardown_serialization_subscriptions
end
def setup_serialization_subscriptions
@serializers = Hash.new(0)
ActiveSupport::Notifications.subscribe("!serialize.active_model_serializers") do |name, start, finish, id, payload|
serializer = payload[:serializer]
@serializers[serializer] += 1
end
end
def teardown_serialization_subscriptions
ActiveSupport::Notifications.unsubscribe("!serialize.active_model_serializers")
end
def process(*args)
@serializers = Hash.new(0)
super
end
# Asserts that the request was rendered with the appropriate serializers.
#
# # assert that the "PostSerializer" serializer was rendered
# assert_serializer "PostSerializer"
#
# # assert that the instance of PostSerializer was rendered
# assert_serializer PostSerializer
#
# # assert that the "PostSerializer" serializer was rendered
# assert_serializer :post_serializer
#
# # assert that the rendered serializer starts with "Post"
# assert_serializer %r{\APost.+\Z}
#
# # assert that no serializer was rendered
# assert_serializer nil
#
#
def assert_serializer(options = {}, message = nil)
# Force body to be read in case the template is being streamed.
response.body
rendered = @serializers
msg = message || "expecting <#{options.inspect}> but rendering with <#{rendered.keys}>"
matches_serializer = case options
when lambda { |options| options.kind_of?(Class) && options < ActiveModel::Serializer }
rendered.any? do |serializer, count|
options.name == serializer
end
when Symbol
options = options.to_s.camelize
rendered.any? do |serializer, count|
serializer == options
end
when String
!options.empty? && rendered.any? do |serializer, count|
serializer == options
end
when Regexp
rendered.any? do |serializer, count|
serializer.match(options)
end
when NilClass
rendered.blank?
else
raise ArgumentError, "assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil"
end
assert matches_serializer, msg
end
end
end
active_model_serializers-0.9.3/lib/active_model/ 0000775 0000000 0000000 00000000000 12460006062 0021767 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/array_serializer.rb 0000664 0000000 0000000 00000004627 12460006062 0025674 0 ustar 00root root 0000000 0000000 require 'active_model/default_serializer'
require 'active_model/serializable'
module ActiveModel
class ArraySerializer
include Serializable
class << self
attr_accessor :_root
alias root _root=
alias root= _root=
end
def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@polymorphic = options.fetch(:polymorphic, false)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@each_serializer = options[:each_serializer]
@resource_name = options[:resource_name]
@only = options[:only] ? Array(options[:only]) : nil
@except = options[:except] ? Array(options[:except]) : nil
@namespace = options[:namespace]
@key_format = options[:key_format] || options[:each_serializer].try(:key_format)
end
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format
def json_key
key = root.nil? ? @resource_name : root
key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
end
def serializer_for(item)
serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
end
def serializable_object(options={})
@object.map do |item|
serializer_for(item).serializable_object_with_notification(options)
end
end
alias_method :serializable_array, :serializable_object
def embedded_in_root_associations
@object.each_with_object({}) do |item, hash|
serializer_for(item).embedded_in_root_associations.each_pair do |type, objects|
next if !objects || objects.flatten.empty?
if hash.has_key?(type)
case hash[type] when Hash
hash[type].deep_merge!(objects){ |key, old, new| (Array(old) + Array(new)).uniq }
else
hash[type].concat(objects).uniq!
end
else
hash[type] = objects
end
end
end
end
private
def instrumentation_keys
[:object, :scope, :root, :meta_key, :meta, :each_serializer, :resource_name, :key_format]
end
end
end
active_model_serializers-0.9.3/lib/active_model/default_serializer.rb 0000664 0000000 0000000 00000001303 12460006062 0026166 0 ustar 00root root 0000000 0000000 require 'active_model/serializable'
module ActiveModel
# DefaultSerializer
#
# Provides a constant interface for all items
class DefaultSerializer
include ActiveModel::Serializable
attr_reader :object
def initialize(object, options={})
@object = object
@wrap_in_array = options[:_wrap_in_array]
end
def as_json(options={})
instrument('!serialize') do
return [] if @object.nil? && @wrap_in_array
hash = @object.as_json
@wrap_in_array ? [hash] : hash
end
end
alias serializable_hash as_json
alias serializable_object as_json
private
def instrumentation_keys
[:object, :wrap_in_array]
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializable.rb 0000664 0000000 0000000 00000002772 12460006062 0024772 0 ustar 00root root 0000000 0000000 require 'active_model/serializable/utils'
module ActiveModel
module Serializable
def self.included(base)
base.extend Utils
end
def as_json(options={})
instrument('!serialize') do
if root = options.fetch(:root, json_key)
hash = { root => serializable_object(options) }
hash.merge!(serializable_data)
hash
else
serializable_object(options)
end
end
end
def serializable_object_with_notification(options={})
instrument('!serialize') do
serializable_object(options)
end
end
def serializable_data
embedded_in_root_associations.tap do |hash|
if respond_to?(:meta) && meta
hash[meta_key] = meta
end
end
end
def namespace
get_namespace && Utils._const_get(get_namespace)
end
def embedded_in_root_associations
{}
end
private
def get_namespace
modules = self.class.name.split('::')
modules[0..-2].join('::') if modules.size > 1
end
def instrument(action, &block)
payload = instrumentation_keys.inject({ serializer: self.class.name }) do |payload, key|
payload[:payload] = self.instance_variable_get(:"@#{key}")
payload
end
ActiveSupport::Notifications.instrument("#{action}.active_model_serializers", payload, &block)
end
def instrumentation_keys
[:object, :scope, :root, :meta_key, :meta, :wrap_in_array, :only, :except, :key_format]
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializable/ 0000775 0000000 0000000 00000000000 12460006062 0024435 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializable/utils.rb 0000664 0000000 0000000 00000000511 12460006062 0026117 0 ustar 00root root 0000000 0000000 module ActiveModel
module Serializable
module Utils
extend self
def _const_get(const)
begin
method = RUBY_VERSION >= '2.0' ? :const_get : :qualified_const_get
Object.send method, const
rescue NameError
const.safe_constantize
end
end
end
end
end active_model_serializers-0.9.3/lib/active_model/serializer.rb 0000664 0000000 0000000 00000021130 12460006062 0024462 0 ustar 00root root 0000000 0000000 require 'active_model/array_serializer'
require 'active_model/serializable'
require 'active_model/serializer/association'
require 'active_model/serializer/config'
require 'thread'
module ActiveModel
class Serializer
include Serializable
@mutex = Mutex.new
class << self
def inherited(base)
base._root = _root
base._attributes = (_attributes || []).dup
base._associations = (_associations || {}).dup
end
def setup
@mutex.synchronize do
yield CONFIG
end
end
EMBED_IN_ROOT_OPTIONS = [
:include,
:embed_in_root,
:embed_in_root_key,
:embed_namespace
].freeze
def embed(type, options={})
CONFIG.embed = type
if EMBED_IN_ROOT_OPTIONS.any? { |opt| options[opt].present? }
CONFIG.embed_in_root = true
end
if options[:embed_in_root_key].present?
CONFIG.embed_in_root_key = options[:embed_in_root_key]
end
ActiveSupport::Deprecation.warn <<-WARN
** Notice: embed is deprecated. **
The use of .embed method on a Serializer will be soon removed, as this should have a global scope and not a class scope.
Please use the global .setup method instead:
ActiveModel::Serializer.setup do |config|
config.embed = :#{type}
config.embed_in_root = #{CONFIG.embed_in_root || false}
end
WARN
end
def format_keys(format)
@key_format = format
end
attr_reader :key_format
def serializer_for(resource, options = {})
if resource.respond_to?(:to_ary)
if Object.constants.include?(:ArraySerializer)
::ArraySerializer
else
ArraySerializer
end
else
_const_get build_serializer_class(resource, options)
end
end
attr_accessor :_root, :_attributes, :_associations
alias root _root=
alias root= _root=
def root_name
if name
root_name = name.demodulize.underscore.sub(/_serializer$/, '')
CONFIG.plural_default_root ? root_name.pluralize : root_name
end
end
def attributes(*attrs)
attrs.each do |attr|
striped_attr = strip_attribute attr
@_attributes << striped_attr
define_method striped_attr do
object.read_attribute_for_serialization attr
end unless method_defined?(attr)
end
end
def has_one(*attrs)
associate(Association::HasOne, *attrs)
end
def has_many(*attrs)
associate(Association::HasMany, *attrs)
end
private
def strip_attribute(attr)
symbolized = attr.is_a?(Symbol)
attr = attr.to_s.gsub(/\?\Z/, '')
attr = attr.to_sym if symbolized
attr
end
def build_serializer_class(resource, options)
"".tap do |klass_name|
klass_name << "#{options[:namespace]}::" if options[:namespace]
klass_name << options[:prefix].to_s.classify if options[:prefix]
klass_name << "#{resource.class.name}Serializer"
end
end
def associate(klass, *attrs)
options = attrs.extract_options!
attrs.each do |attr|
define_method attr do
object.send attr
end unless method_defined?(attr)
@_associations[attr] = klass.new(attr, options)
end
end
end
def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@polymorphic = options.fetch(:polymorphic, false)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@wrap_in_array = options[:_wrap_in_array]
@only = options[:only] ? Array(options[:only]) : nil
@except = options[:except] ? Array(options[:except]) : nil
@key_format = options[:key_format]
@context = options[:context]
@namespace = options[:namespace]
end
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
def json_key
key = if root == true || root.nil?
self.class.root_name
else
root
end
key_format == :lower_camel && key.present? ? key.camelize(:lower) : key
end
def attributes
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
hash[name] = send(name)
end
end
def associations
associations = self.class._associations
included_associations = filter(associations.keys)
associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name
if association.embed_ids?
ids = serialize_ids association
if association.embed_namespace?
hash = hash[association.embed_namespace] ||= {}
hash[association.key] = ids
else
hash[association.key] = ids
end
elsif association.embed_objects?
if association.embed_namespace?
hash = hash[association.embed_namespace] ||= {}
end
hash[association.embedded_key] = serialize association
end
end
end
end
def filter(keys)
if @only
keys & @only
elsif @except
keys - @except
else
keys
end
end
def embedded_in_root_associations
associations = self.class._associations
included_associations = filter(associations.keys)
associations.each_with_object({}) do |(name, association), hash|
if included_associations.include? name
association_serializer = build_serializer(association)
# we must do this always because even if the current association is not
# embeded in root, it might have its own associations that are embeded in root
hash.merge!(association_serializer.embedded_in_root_associations) {|key, oldval, newval| [newval, oldval].flatten }
if association.embed_in_root?
if association.embed_in_root_key?
hash = hash[association.embed_in_root_key] ||= {}
end
serialized_data = association_serializer.serializable_object
key = association.root_key
if hash.has_key?(key)
hash[key].concat(serialized_data).uniq!
else
hash[key] = serialized_data
end
end
end
end
end
def build_serializer(association)
object = send(association.name)
association.build_serializer(object, association_options_for_serializer(association))
end
def association_options_for_serializer(association)
prefix = association.options[:prefix]
namespace = association.options[:namespace] || @namespace || self.namespace
{ scope: scope }.tap do |opts|
opts[:namespace] = namespace if namespace
opts[:prefix] = prefix if prefix
end
end
def serialize(association)
build_serializer(association).serializable_object
end
def serialize_ids(association)
associated_data = send(association.name)
if associated_data.respond_to?(:to_ary)
associated_data.map { |elem| serialize_id(elem, association) }
else
serialize_id(associated_data, association) if associated_data
end
end
def key_format
@key_format || self.class.key_format || CONFIG.key_format
end
def format_key(key)
if key_format == :lower_camel
key.to_s.camelize(:lower)
else
key
end
end
def convert_keys(hash)
Hash[hash.map do |k,v|
key = if k.is_a?(Symbol)
format_key(k).to_sym
else
format_key(k)
end
[key ,v]
end]
end
attr_writer :serialization_options
def serialization_options
@serialization_options || {}
end
def serializable_object(options={})
self.serialization_options = options
return @wrap_in_array ? [] : nil if @object.nil?
hash = attributes
hash.merge! associations
hash = convert_keys(hash) if key_format.present?
hash = { :type => type_name(@object), type_name(@object) => hash } if @polymorphic
@wrap_in_array ? [hash] : hash
end
alias_method :serializable_hash, :serializable_object
def serialize_id(elem, association)
id = elem.read_attribute_for_serialization(association.embed_key)
association.polymorphic? ? { id: id, type: type_name(elem) } : id
end
def type_name(elem)
elem.class.to_s.demodulize.underscore.to_sym
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/ 0000775 0000000 0000000 00000000000 12460006062 0024140 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/association.rb 0000664 0000000 0000000 00000004276 12460006062 0027012 0 ustar 00root root 0000000 0000000 require 'active_model/default_serializer'
require 'active_model/serializer/association/has_one'
require 'active_model/serializer/association/has_many'
module ActiveModel
class Serializer
class Association
def initialize(name, options={})
if options.has_key?(:include)
ActiveSupport::Deprecation.warn <<-WARN
** Notice: include was renamed to embed_in_root. **
WARN
end
@name = name.to_s
@options = options
self.embed = options.fetch(:embed) { CONFIG.embed }
@polymorphic = options.fetch(:polymorphic, false)
@embed_in_root = options.fetch(:embed_in_root) { options.fetch(:include) { CONFIG.embed_in_root } }
@key_format = options.fetch(:key_format) { CONFIG.key_format }
@embed_key = options[:embed_key] || :id
@key = options[:key]
@embedded_key = options[:root] || name
@embed_in_root_key = options.fetch(:embed_in_root_key) { CONFIG.embed_in_root_key }
@embed_namespace = options.fetch(:embed_namespace) { CONFIG.embed_namespace }
serializer = @options[:serializer]
@serializer_from_options = serializer.is_a?(String) ? serializer.constantize : serializer
end
attr_reader :name, :embed_ids, :embed_objects, :polymorphic
attr_accessor :embed_in_root, :embed_key, :key, :embedded_key, :root_key, :serializer_from_options, :options, :key_format, :embed_in_root_key, :embed_namespace
alias embed_ids? embed_ids
alias embed_objects? embed_objects
alias embed_in_root? embed_in_root
alias embed_in_root_key? embed_in_root_key
alias embed_namespace? embed_namespace
alias polymorphic? polymorphic
def embed=(embed)
@embed_ids = embed == :id || embed == :ids
@embed_objects = embed == :object || embed == :objects
end
def serializer_from_object(object, options = {})
Serializer.serializer_for(object, options)
end
def default_serializer
DefaultSerializer
end
def build_serializer(object, options = {})
serializer_class(object, options).new(object, options.merge(self.options))
end
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/association/ 0000775 0000000 0000000 00000000000 12460006062 0026454 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/association/has_many.rb 0000664 0000000 0000000 00000001652 12460006062 0030604 0 ustar 00root root 0000000 0000000 module ActiveModel
class Serializer
class Association
class HasMany < Association
def initialize(name, *args)
super
@root_key = @embedded_key.to_s
@key ||= case CONFIG.default_key_type
when :name then name.to_s.pluralize
else "#{name.to_s.singularize}_ids"
end
end
def serializer_class(object, _)
if use_array_serializer?
ArraySerializer
else
serializer_from_options
end
end
def options
if use_array_serializer?
{ each_serializer: serializer_from_options }.merge! super
else
super
end
end
private
def use_array_serializer?
!serializer_from_options ||
serializer_from_options && !(serializer_from_options <= ArraySerializer)
end
end
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/association/has_one.rb 0000664 0000000 0000000 00000001240 12460006062 0030412 0 ustar 00root root 0000000 0000000 module ActiveModel
class Serializer
class Association
class HasOne < Association
def initialize(name, *args)
super
@root_key = @embedded_key.to_s.pluralize
@key ||= case CONFIG.default_key_type
when :name then name.to_s.singularize
else "#{name}_id"
end
end
def serializer_class(object, options = {})
serializer_from_options || serializer_from_object(object, options) || default_serializer
end
def build_serializer(object, options = {})
options[:_wrap_in_array] = embed_in_root?
super
end
end
end
end
end active_model_serializers-0.9.3/lib/active_model/serializer/config.rb 0000664 0000000 0000000 00000001137 12460006062 0025734 0 ustar 00root root 0000000 0000000 module ActiveModel
class Serializer
class Config
def initialize(data = {})
@data = data
end
def each(&block)
@data.each(&block)
end
def clear
@data.clear
end
def method_missing(name, *args)
name = name.to_s
return @data[name] if @data.include?(name)
match = name.match(/\A(.*?)([?=]?)\Z/)
case match[2]
when "="
@data[match[1]] = args.first
when "?"
!!@data[match[1]]
end
end
end
CONFIG = Config.new('embed' => :objects) # :nodoc:
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/generators/ 0000775 0000000 0000000 00000000000 12460006062 0026311 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/resource_override.rb 0000664 0000000 0000000 00000000350 12460006062 0032362 0 ustar 00root root 0000000 0000000 require 'rails/generators'
require 'rails/generators/rails/resource/resource_generator'
module Rails
module Generators
class ResourceGenerator
def add_serializer
invoke 'serializer'
end
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer/ 0000775 0000000 0000000 00000000000 12460006062 0030462 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer/USAGE 0000664 0000000 0000000 00000000440 12460006062 0031247 0 ustar 00root root 0000000 0000000 Description:
Generates a serializer for the given resource with tests.
Example:
`rails generate serializer Account name created_at`
For TestUnit it creates:
Serializer: app/serializers/account_serializer.rb
TestUnit: test/unit/account_serializer_test.rb
scaffold_controller_generator.rb 0000664 0000000 0000000 00000000542 12460006062 0037023 0 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer require 'rails/generators'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class ScaffoldControllerGenerator
if Rails::VERSION::MAJOR >= 4
source_root File.expand_path('../templates', __FILE__)
hook_for :serializer, default: true
end
end
end
end
serializer_generator.rb 0000664 0000000 0000000 00000002200 12460006062 0035141 0 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer module Rails
module Generators
class SerializerGenerator < NamedBase
source_root File.expand_path('../templates', __FILE__)
check_class_collision suffix: 'Serializer'
argument :attributes, type: :array, default: [], banner: 'field:type field:type'
class_option :parent, type: :string, desc: 'The parent class for the generated serializer'
def create_serializer_file
template 'serializer.rb', File.join('app/serializers', class_path, "#{file_name}_serializer.rb")
end
private
def attributes_names
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
end
def association_names
attributes.select { |attr| attr.reference? }.map { |a| a.name.to_sym }
end
def parent_class_name
if options[:parent]
options[:parent]
elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) ||
(Object.const_get(:ApplicationSerializer) rescue nil)
'ApplicationSerializer'
else
'ActiveModel::Serializer'
end
end
end
end
end
active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer/templates/ 0000775 0000000 0000000 00000000000 12460006062 0032460 5 ustar 00root root 0000000 0000000 controller.rb 0000664 0000000 0000000 00000005647 12460006062 0035125 0 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer/templates <% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
# GET <%= route_url %>
# GET <%= route_url %>.json
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
respond_to do |format|
format.html # index.html.erb
format.json { render json: <%= "@#{plural_table_name}" %> }
end
end
# GET <%= route_url %>/1
# GET <%= route_url %>/1.json
def show
respond_to do |format|
format.html # show.html.erb
format.json { render json: <%= "@#{singular_table_name}" %> }
end
end
# GET <%= route_url %>/new
def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
end
# GET <%= route_url %>/1/edit
def edit
end
# POST <%= route_url %>
# POST <%= route_url %>.json
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
respond_to do |format|
if @<%= orm_instance.save %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> }
format.json { render json: <%= "@#{singular_table_name}" %>, status: :created }
else
format.html { render action: 'new' }
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
end
end
end
# PATCH/PUT <%= route_url %>/1
# PATCH/PUT <%= route_url %>/1.json
def update
respond_to do |format|
if @<%= orm_instance.update("#{singular_table_name}_params") %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity }
end
end
end
# DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.json
def destroy
@<%= orm_instance.destroy %>
respond_to do |format|
format.html { redirect_to <%= index_helper %>_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_<%= singular_table_name %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end
# Never trust parameters from the scary internet, only allow the white list through.
def <%= "#{singular_table_name}_params" %>
<%- if attributes_names.empty? -%>
params[<%= ":#{singular_table_name}" %>]
<%- else -%>
params.require(<%= ":#{singular_table_name}" %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
<%- end -%>
end
end
<% end -%>
serializer.rb 0000664 0000000 0000000 00000000374 12460006062 0035103 0 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/lib/active_model/serializer/generators/serializer/templates <% module_namespacing do -%>
class <%= class_name %>Serializer < <%= parent_class_name %>
attributes <%= attributes_names.map(&:inspect).join(", ") %>
<% association_names.each do |attribute| -%>
has_one :<%= attribute %>
<% end -%>
end
<% end -%>
active_model_serializers-0.9.3/lib/active_model/serializer/railtie.rb 0000664 0000000 0000000 00000001202 12460006062 0026111 0 ustar 00root root 0000000 0000000 module ActiveModel
class Railtie < Rails::Railtie
initializer 'generators' do |app|
app.load_generators
require 'active_model/serializer/generators/serializer/serializer_generator'
require 'active_model/serializer/generators/serializer/scaffold_controller_generator'
require 'active_model/serializer/generators/resource_override'
end
initializer 'include_routes.active_model_serializer' do |app|
ActiveSupport.on_load(:active_model_serializers) do
include app.routes.url_helpers
end
end
end
end
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
active_model_serializers-0.9.3/lib/active_model/serializer/version.rb 0000664 0000000 0000000 00000000106 12460006062 0026147 0 ustar 00root root 0000000 0000000 module ActiveModel
class Serializer
VERSION = '0.9.3'
end
end
active_model_serializers-0.9.3/lib/active_model/serializer_support.rb 0000664 0000000 0000000 00000000150 12460006062 0026255 0 ustar 00root root 0000000 0000000 module ActiveModel
module SerializerSupport
alias read_attribute_for_serialization send
end
end
active_model_serializers-0.9.3/lib/active_model_serializers.rb 0000664 0000000 0000000 00000001245 12460006062 0024732 0 ustar 00root root 0000000 0000000 require 'active_model'
require 'active_model/serializer'
require 'active_model/serializer_support'
require 'active_model/serializer/version'
require 'active_model/serializer/railtie' if defined?(Rails)
begin
require 'action_controller'
require 'action_controller/serialization'
require 'action_controller/serialization_test_case'
ActiveSupport.on_load(:action_controller) do
if ::ActionController::Serialization.enabled
ActionController::Base.send(:include, ::ActionController::Serialization)
ActionController::TestCase.send(:include, ::ActionController::SerializationAssertions)
end
end
rescue LoadError
# rails not installed, continuing
end
active_model_serializers-0.9.3/test/ 0000775 0000000 0000000 00000000000 12460006062 0017545 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/fixtures/ 0000775 0000000 0000000 00000000000 12460006062 0021416 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/fixtures/active_record.rb 0000664 0000000 0000000 00000004222 12460006062 0024554 0 ustar 00root root 0000000 0000000 require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)
ActiveRecord::Schema.define do
create_table :ar_posts, force: true do |t|
t.string :title
t.text :body
t.belongs_to :ar_section, index: true
t.timestamps
end
create_table :ar_comments, force: true do |t|
t.text :body
t.belongs_to :ar_post, index: true
t.timestamps
end
create_table :ar_tags, force: true do |t|
t.string :name
end
create_table :ar_sections, force: true do |t|
t.string :name
end
create_table :ar_posts_tags, force: true do |t|
t.references :ar_post, :ar_tag, index: true
end
create_table :ar_comments_tags, force: true do |t|
t.references :ar_comment, :ar_tag, index: true
end
end
class ARPost < ActiveRecord::Base
has_many :ar_comments, class_name: 'ARComment'
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_posts_tags
belongs_to :ar_section, class_name: 'ARSection'
end
class ARComment < ActiveRecord::Base
belongs_to :ar_post, class_name: 'ARPost'
has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_comments_tags
end
class ARTag < ActiveRecord::Base
end
class ARSection < ActiveRecord::Base
end
class ARPostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :ar_comments, :ar_tags
has_one :ar_section
end
class ARCommentSerializer < ActiveModel::Serializer
attributes :body
has_many :ar_tags
end
class ARTagSerializer < ActiveModel::Serializer
attributes :name
end
class ARSectionSerializer < ActiveModel::Serializer
attributes 'name'
end
ARPost.create(title: 'New post',
body: 'A body!!!',
ar_section: ARSection.create(name: 'ruby')).tap do |post|
short_tag = post.ar_tags.create(name: 'short')
whiny_tag = post.ar_tags.create(name: 'whiny')
happy_tag = ARTag.create(name: 'happy')
post.ar_comments.create(body: 'what a dumb post').tap do |comment|
comment.ar_tags.concat happy_tag, whiny_tag
end
post.ar_comments.create(body: 'i liked it').tap do |comment|
comment.ar_tags.concat happy_tag, short_tag
end
end
active_model_serializers-0.9.3/test/fixtures/poro.rb 0000664 0000000 0000000 00000006337 12460006062 0022733 0 ustar 00root root 0000000 0000000 class Model
def initialize(hash = {})
@attributes = hash
end
def read_attribute_for_serialization(name)
if name == :id || name == 'id'
object_id
elsif respond_to?(name)
send name
else
@attributes[name]
end
end
end
###
## Models
###
class User < Model
def profile
@profile ||= Profile.new(name: 'N1', description: 'D1')
end
end
class UserInfo < Model
def user
@user ||= User.new(name: 'N1', email: 'E1')
end
end
class Profile < Model
end
class Category < Model
def posts
@posts ||= [Post.new(title: 'T1', body: 'B1'),
Post.new(title: 'T2', body: 'B2')]
end
end
class Post < Model
def comments
@comments ||= [Comment.new(content: 'C1'),
Comment.new(content: 'C2')]
end
end
class SpecialPost < Post
def special_comment
@speical_comment ||= Comment.new(content: 'special')
end
end
class Comment < Model
end
class WebLog < Model
end
class Interview < Model
def attachment
@attachment ||= Image.new(url: 'U1')
end
end
class Mail < Model
def attachments
@attachments ||= [Image.new(url: 'U1'),
Video.new(html: 'H1')]
end
end
class Image < Model
end
class Video < Model
end
###
## Serializers
###
class UserSerializer < ActiveModel::Serializer
attributes :name, :email
has_one :profile
end
class UserInfoSerializer < ActiveModel::Serializer
has_one :user
end
class ProfileSerializer < ActiveModel::Serializer
def description
description = object.read_attribute_for_serialization(:description)
scope ? "#{description} - #{scope}" : description
end
attributes :name, :description
end
class CategorySerializer < ActiveModel::Serializer
attributes :name
has_many :posts
end
class PostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
end
class SpecialPostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments, root: :comments, embed_in_root: true, embed: :ids
has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
end
class CommentSerializer < ActiveModel::Serializer
attributes :content
end
class WebLogSerializer < ActiveModel::Serializer
attributes :name, :display_name
end
class WebLogLowerCamelSerializer < WebLogSerializer
format_keys :lower_camel
end
class InterviewSerializer < ActiveModel::Serializer
attributes :text
has_one :attachment, polymorphic: true
end
class MailSerializer < ActiveModel::Serializer
attributes :body
has_many :attachments, polymorphic: true
end
class ImageSerializer < ActiveModel::Serializer
attributes :url
end
class VideoSerializer < ActiveModel::Serializer
attributes :html
end
class ShortProfileSerializer < ::ProfileSerializer; end
module TestNamespace
class ProfileSerializer < ::ProfileSerializer; end
class UserSerializer < ::UserSerializer; end
end
ActiveModel::Serializer.setup do |config|
config.default_key_type = :name
end
class NameKeyUserSerializer < ActiveModel::Serializer
attributes :name, :email
has_one :profile
end
class NameKeyPostSerializer < ActiveModel::Serializer
attributes :title, :body
has_many :comments
end
ActiveModel::Serializer.setup do |config|
config.default_key_type = nil
end
active_model_serializers-0.9.3/test/fixtures/template.html.erb 0000664 0000000 0000000 00000000016 12460006062 0024663 0 ustar 00root root 0000000 0000000 Hello.
active_model_serializers-0.9.3/test/integration/ 0000775 0000000 0000000 00000000000 12460006062 0022070 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/integration/action_controller/ 0000775 0000000 0000000 00000000000 12460006062 0025610 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/integration/action_controller/namespaced_serialization_test.rb 0000664 0000000 0000000 00000005276 12460006062 0034243 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActionController module Serialization class NamespacedSerializationTest < ActionController::TestCase class TestNamespace::MyController < ActionController::Base def render_profile_with_namespace render json: Profile.new({ name: 'Name 1', description: 'Description 1'}) end def render_profiles_with_namespace render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})] end def render_comment render json: Comment.new(content: 'Comment 1') end def render_comments render json: [Comment.new(content: 'Comment 1')] end end tests TestNamespace::MyController def test_render_profile_with_namespace get :render_profile_with_namespace assert_serializer TestNamespace::ProfileSerializer end def test_render_profiles_with_namespace get :render_profiles_with_namespace assert_serializer TestNamespace::ProfileSerializer end def test_fallback_to_a_version_without_namespace get :render_comment assert_serializer CommentSerializer end def test_array_fallback_to_a_version_without_namespace get :render_comments assert_serializer CommentSerializer end end class OptionNamespacedSerializationTest < ActionController::TestCase class MyController < ActionController::Base def default_serializer_options { namespace: TestNamespace } end def render_profile_with_namespace_option render json: Profile.new({ name: 'Name 1', description: 'Description 1'}) end def render_profiles_with_namespace_option render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})] end def render_comment render json: Comment.new(content: 'Comment 1') end def render_comments render json: [Comment.new(content: 'Comment 1')] end end tests MyController def test_render_profile_with_namespace_option get :render_profile_with_namespace_option assert_serializer TestNamespace::ProfileSerializer end def test_render_profiles_with_namespace_option get :render_profiles_with_namespace_option assert_serializer TestNamespace::ProfileSerializer end def test_fallback_to_a_version_without_namespace get :render_comment assert_serializer CommentSerializer end def test_array_fallback_to_a_version_without_namespace get :render_comments assert_serializer CommentSerializer end end end end active_model_serializers-0.9.3/test/integration/action_controller/serialization_test.rb 0000664 0000000 0000000 00000022421 12460006062 0032052 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActionController module Serialization class ImplicitSerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_using_implicit_serializer render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end end tests MyController def test_render_using_implicit_serializer get :render_using_implicit_serializer assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body end end class ImplicitSerializerScopeTest < ActionController::TestCase class MyController < ActionController::Base def render_using_implicit_serializer_and_scope render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end private def current_user 'current_user' end end tests MyController def test_render_using_implicit_serializer_and_scope get :render_using_implicit_serializer_and_scope assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body end end class DefaultOptionsForSerializerScopeTest < ActionController::TestCase class MyController < ActionController::Base def default_serializer_options { scope: current_admin } end def render_using_scope_set_in_default_serializer_options render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end private def current_user 'current_user' end def current_admin 'current_admin' end end tests MyController def test_render_using_scope_set_in_default_serializer_options get :render_using_scope_set_in_default_serializer_options assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body end end class ExplicitSerializerScopeTest < ActionController::TestCase class MyController < ActionController::Base def render_using_implicit_serializer_and_explicit_scope render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin end private def current_user 'current_user' end def current_admin 'current_admin' end end tests MyController def test_render_using_implicit_serializer_and_explicit_scope get :render_using_implicit_serializer_and_explicit_scope assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body end end class OverridingSerializationScopeTest < ActionController::TestCase class MyController < ActionController::Base def render_overriding_serialization_scope render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end private def current_user 'current_user' end def serialization_scope 'current_admin' end end tests MyController def test_render_overriding_serialization_scope get :render_overriding_serialization_scope assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body end end class CallingSerializationScopeTest < ActionController::TestCase class MyController < ActionController::Base def render_calling_serialization_scope render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end private def current_user 'current_user' end serialization_scope :current_user end tests MyController def test_render_calling_serialization_scope get :render_calling_serialization_scope assert_equal 'application/json', @response.content_type assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body end end class JSONDumpSerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_using_json_dump render json: JSON.dump(hello: 'world') end end tests MyController def test_render_using_json_dump get :render_using_json_dump assert_equal 'application/json', @response.content_type assert_equal '{"hello":"world"}', @response.body end end class RailsSerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_using_rails_behavior render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })], serializer: false end end tests MyController def test_render_using_rails_behavior get :render_using_rails_behavior assert_equal 'application/json', @response.content_type assert_equal '[{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}]', @response.body end end class ArraySerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_array render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })] end end tests MyController def test_render_array get :render_array assert_equal 'application/json', @response.content_type assert_equal '{"my":[{"name":"Name 1","description":"Description 1"}]}', @response.body end end class LowerCamelArraySerializerTest < ActionController::TestCase class WebLogController < ActionController::Base def render_array render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}), WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})], each_serializer: WebLogLowerCamelSerializer end end tests WebLogController def test_render_array get :render_array assert_equal 'application/json', @response.content_type assert_equal '{"webLog":[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]}', @response.body end end class LowerCamelWoRootSerializerTest < ActionController::TestCase class WebLogController < ActionController::Base def render_without_root render json: WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}), root: false, serializer: WebLogLowerCamelSerializer end end tests WebLogController def test_render_without_root get :render_without_root assert_equal 'application/json', @response.content_type assert_equal '{"name":"Name 1","displayName":"Display Name 1"}', @response.body end end class LowerCamelArrayWoRootSerializerTest < ActionController::TestCase class WebLogController < ActionController::Base def render_array_without_root render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}), WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})], root: false, each_serializer: WebLogLowerCamelSerializer end end tests WebLogController def test_render_array_without_root get :render_array_without_root assert_equal 'application/json', @response.content_type assert_equal '[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]', @response.body end end class ArrayEmbedingSerializerTest < ActionController::TestCase def setup super @association = UserSerializer._associations[:profile] @old_association = @association.dup end def teardown super UserSerializer._associations[:profile] = @old_association end class MyController < ActionController::Base def initialize(*) super @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' }) end attr_reader :user def render_array_embeding_in_root render json: [@user] end end tests MyController def test_render_array_embeding_in_root @association.embed = :ids @association.embed_in_root = true get :render_array_embeding_in_root assert_equal 'application/json', @response.content_type assert_equal("{\"my\":[{\"name\":\"Name 1\",\"email\":\"mail@server.com\",\"profile_id\":#{@controller.user.profile.object_id}}],\"profiles\":[{\"name\":\"N1\",\"description\":\"D1\"}]}", @response.body) end end end end active_model_serializers-0.9.3/test/integration/action_controller/serialization_test_case_test.rb 0000664 0000000 0000000 00000004164 12460006062 0034110 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActionController module SerializationsAssertions class RenderSerializerTest < ActionController::TestCase class MyController < ActionController::Base def render_using_serializer render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end def render_text render text: 'ok' end def render_template prepend_view_path "./test/fixtures" render template: "template" end end tests MyController def test_supports_specifying_serializers_with_a_serializer_class get :render_using_serializer assert_serializer ProfileSerializer end def test_supports_specifying_serializers_with_a_regexp get :render_using_serializer assert_serializer %r{\AProfile.+\Z} end def test_supports_specifying_serializers_with_a_string get :render_using_serializer assert_serializer 'ProfileSerializer' end def test_supports_specifying_serializers_with_a_symbol get :render_using_serializer assert_serializer :profile_serializer end def test_supports_specifying_serializers_with_a_nil get :render_text assert_serializer nil end def test_raises_descriptive_error_message_when_serializer_was_not_rendered get :render_using_serializer e = assert_raise ActiveSupport::TestCase::Assertion do assert_serializer 'PostSerializer' end assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message end def test_raises_argument_error_when_asserting_with_invalid_object get :render_using_serializer e = assert_raise ArgumentError do assert_serializer Hash end assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message end def test_does_not_overwrite_notification_subscriptions get :render_template assert_template "template" end end end end active_model_serializers-0.9.3/test/integration/active_record/ 0000775 0000000 0000000 00000000000 12460006062 0024701 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/integration/active_record/active_record_test.rb 0000664 0000000 0000000 00000005005 12460006062 0031076 0 ustar 00root root 0000000 0000000 require 'test_helper' require 'fixtures/active_record' module ActiveModel class Serializer class ActiveRecordTest < Minitest::Test def setup @post = ARPost.first end def test_serialization_embedding_objects post_serializer = ARPostSerializer.new(@post) assert_equal({ 'ar_post' => { title: 'New post', body: 'A body!!!', ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] }, { body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }], ar_tags: [{ name: 'short' }, { name: 'whiny' }], ar_section: { 'name' => 'ruby' } } }, post_serializer.as_json) end def test_serialization_embedding_ids post_serializer = ARPostSerializer.new(@post) embed(ARPostSerializer, embed: :ids) do assert_equal({ 'ar_post' => { title: 'New post', body: 'A body!!!', 'ar_comment_ids' => [1, 2], 'ar_tag_ids' => [1, 2], 'ar_section_id' => 1 } }, post_serializer.as_json) end end def test_serialization_embedding_ids_including_in_root post_serializer = ARPostSerializer.new(@post) embed(ARPostSerializer, embed: :ids, embed_in_root: true) do embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do assert_equal({ 'ar_post' => { title: 'New post', body: 'A body!!!', 'ar_comment_ids' => [1, 2], 'ar_tag_ids' => [1, 2], 'ar_section_id' => 1 }, 'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] }, { body: 'i liked it', 'ar_tag_ids' => [3, 1] }], 'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }], 'ar_sections' => [{ 'name' => 'ruby' }] }, post_serializer.as_json) end end end private def embed(serializer_class, options = {}) old_assocs = Hash[serializer_class._associations.to_a.map { |(name, association)| [name, association.dup] }] serializer_class._associations.each_value do |association| association.embed = options[:embed] association.embed_in_root = options[:embed_in_root] end yield ensure serializer_class._associations = old_assocs end end end end active_model_serializers-0.9.3/test/integration/generators/ 0000775 0000000 0000000 00000000000 12460006062 0024241 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/integration/generators/resource_generator_test.rb 0000664 0000000 0000000 00000001320 12460006062 0031516 0 ustar 00root root 0000000 0000000 require 'test_helper' require 'rails' require 'active_model/serializer/railtie' require 'test_app' class ResourceGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../../../tmp', __FILE__) setup :prepare_destination, :copy_routes tests Rails::Generators::ResourceGenerator arguments %w(account) def test_serializer_file_is_generated run_generator assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ end private def copy_routes config_dir = File.join(destination_root, 'config') FileUtils.mkdir_p(config_dir) File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }') end end active_model_serializers-0.9.3/test/integration/generators/scaffold_controller_generator_test.rb 0000664 0000000 0000000 00000004701 12460006062 0033721 0 ustar 00root root 0000000 0000000 require 'test_helper' require 'rails' require 'active_model/serializer/railtie' require 'test_app' class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../../../tmp', __FILE__) setup :prepare_destination tests Rails::Generators::ScaffoldControllerGenerator arguments %w(account name:string description:text business:references) def test_generated_controller return true if Rails::VERSION::MAJOR < 4 run_generator assert_file 'app/controllers/accounts_controller.rb' do |content| assert_instance_method :index, content do |m| assert_match /@accounts = Account\.all/, m assert_match /format.html/, m assert_match /format.json \{ render json: @accounts \}/, m end assert_instance_method :show, content do |m| assert_match /format.html/, m assert_match /format.json \{ render json: @account \}/, m end assert_instance_method :new, content do |m| assert_match /@account = Account\.new/, m end assert_instance_method :edit, content do |m| assert m.blank? end assert_instance_method :create, content do |m| assert_match /@account = Account\.new\(account_params\)/, m assert_match /@account\.save/, m assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m assert_match /format\.json \{ render json: @account, status: :created \}/, m assert_match /format\.html \{ render action: 'new' \}/, m assert_match /format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m end assert_instance_method :update, content do |m| assert_match /format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m assert_match /format\.json \{ head :no_content \}/, m assert_match /format\.html \{ render action: 'edit' \}/, m assert_match /format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m end assert_instance_method :destroy, content do |m| assert_match /@account\.destroy/, m assert_match /format\.html { redirect_to accounts_url \}/, m assert_match /format\.json \{ head :no_content \}/, m end assert_match(/def account_params/, content) assert_match(/params\.require\(:account\)\.permit\(:name, :description, :business_id\)/, content) end end end active_model_serializers-0.9.3/test/integration/generators/serializer_generator_test.rb 0000664 0000000 0000000 00000003004 12460006062 0032041 0 ustar 00root root 0000000 0000000 require 'test_helper' require 'rails' require 'active_model/serializer/railtie' require 'test_app' class SerializerGeneratorTest < Rails::Generators::TestCase destination File.expand_path('../../../tmp', __FILE__) setup :prepare_destination tests Rails::Generators::SerializerGenerator arguments %w(account name:string description:text business:references) def test_generates_a_serializer_with_attributes_and_associations run_generator assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ do |serializer| assert_match(/attributes :id, :name, :description/, serializer) assert_match(/has_one :business/, serializer) end end def test_generates_a_namespaced_serializer run_generator ['admin/account'] assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/ end def test_uses_application_serializer_if_one_exists Object.const_set(:ApplicationSerializer, Class.new) run_generator assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/ ensure Object.send :remove_const, :ApplicationSerializer end def test_uses_given_parent Object.const_set(:ApplicationSerializer, Class.new) run_generator ['Account', '--parent=MySerializer'] assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/ ensure Object.send :remove_const, :ApplicationSerializer end end active_model_serializers-0.9.3/test/test_app.rb 0000664 0000000 0000000 00000000607 12460006062 0021714 0 ustar 00root root 0000000 0000000 class TestApp < Rails::Application if Rails.version.to_s.first >= '4' config.eager_load = false config.secret_key_base = 'abc123' end config.after_initialize do Rails.application.routes.default_url_options = { host: 'http://example.com' } end # Set up a logger to avoid creating a log directory on every run. config.logger = Logger.new(nil) end TestApp.initialize! active_model_serializers-0.9.3/test/test_helper.rb 0000664 0000000 0000000 00000001163 12460006062 0022411 0 ustar 00root root 0000000 0000000 require 'bundler/setup' require 'minitest/autorun' require 'active_model_serializers' require 'fixtures/poro' # Ensure backward compatibility with Minitest 4 Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) module TestHelper Routes = ActionDispatch::Routing::RouteSet.new Routes.draw do get ':controller(/:action(/:id))' get ':controller(/:action)' end ActionController::Base.send :include, Routes.url_helpers ActionController::Base.send :include, ActionController::Serialization end ActionController::TestCase.class_eval do def setup @routes = TestHelper::Routes end end active_model_serializers-0.9.3/test/unit/ 0000775 0000000 0000000 00000000000 12460006062 0020524 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/unit/active_model/ 0000775 0000000 0000000 00000000000 12460006062 0023157 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/unit/active_model/array_serializer/ 0000775 0000000 0000000 00000000000 12460006062 0026526 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/unit/active_model/array_serializer/except_test.rb 0000664 0000000 0000000 00000001154 12460006062 0031403 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class ExceptTest < Minitest::Test def test_array_serializer_pass_except_to_items_serializers array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })] serializer = ArraySerializer.new(array, except: [:description]) expected = [{ name: 'Name 1' }, { name: 'Name 2' }] assert_equal expected, serializer.serializable_array end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/key_format_test.rb 0000664 0000000 0000000 00000001200 12460006062 0032243 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class KeyFormatTest < Minitest::Test def test_array_serializer_pass_options_to_items_serializers array = [WebLog.new({ name: 'Name 1', display_name: 'Display Name 1'}), WebLog.new({ name: 'Name 2', display_name: 'Display Name 2'})] serializer = ArraySerializer.new(array, key_format: :lower_camel) expected = [{ name: 'Name 1', displayName: 'Display Name 1' }, { name: 'Name 2', displayName: 'Display Name 2' }] assert_equal expected, serializer.serializable_array end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/meta_test.rb 0000664 0000000 0000000 00000002516 12460006062 0031044 0 ustar 00root root 0000000 0000000 require 'test_helper' require 'active_model/serializer' module ActiveModel class ArraySerializer class MetaTest < Minitest::Test def setup @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }) @serializer = ArraySerializer.new([@profile1, @profile2], root: 'profiles') end def test_meta @serializer.meta = { total: 10 } assert_equal({ 'profiles' => [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ], meta: { total: 10 } }, @serializer.as_json) end def test_meta_using_meta_key @serializer.meta_key = :my_meta @serializer.meta = { total: 10 } assert_equal({ 'profiles' => [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ], my_meta: { total: 10 } }, @serializer.as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/only_test.rb 0000664 0000000 0000000 00000001137 12460006062 0031075 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class OnlyTest < Minitest::Test def test_array_serializer_pass_only_to_items_serializers array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })] serializer = ArraySerializer.new(array, only: [:name]) expected = [{ name: 'Name 1' }, { name: 'Name 2' }] assert_equal expected, serializer.serializable_array end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/root_test.rb 0000664 0000000 0000000 00000006573 12460006062 0031110 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class RootAsOptionTest < Minitest::Test def setup @old_root = ArraySerializer._root @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }) @serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize) end def teardown ArraySerializer._root = @old_root end def test_root_is_not_displayed_using_serializable_array assert_equal([ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ], @serializer.serializable_array) end def test_root_using_as_json assert_equal({ initialize: [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ] }, @serializer.as_json) end def test_root_as_argument_takes_precedence assert_equal({ argument: [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ] }, @serializer.as_json(root: :argument)) end def test_using_false_root_in_initialize_takes_precedence ArraySerializer._root = 'root' @serializer = ArraySerializer.new([@profile1, @profile2], root: false) assert_equal([ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ], @serializer.as_json) end end class RootInSerializerTest < Minitest::Test def setup @old_root = ArraySerializer._root ArraySerializer._root = :in_serializer @profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }) @serializer = ArraySerializer.new([@profile1, @profile2]) @rooted_serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize) end def teardown ArraySerializer._root = @old_root end def test_root_is_not_displayed_using_serializable_hash assert_equal([ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ], @serializer.serializable_array) end def test_root_using_as_json assert_equal({ in_serializer: [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ] }, @serializer.as_json) end def test_root_in_initializer_takes_precedence assert_equal({ initialize: [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ] }, @rooted_serializer.as_json) end def test_root_as_argument_takes_precedence assert_equal({ argument: [ { name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' } ] }, @rooted_serializer.as_json(root: :argument)) end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/scope_test.rb 0000664 0000000 0000000 00000001363 12460006062 0031226 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class ScopeTest < Minitest::Test def test_array_serializer_pass_options_to_items_serializers array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })] serializer = ArraySerializer.new(array, scope: current_user) expected = [{ name: 'Name 1', description: 'Description 1 - user' }, { name: 'Name 2', description: 'Description 2 - user' }] assert_equal expected, serializer.serializable_array end private def current_user 'user' end end end end active_model_serializers-0.9.3/test/unit/active_model/array_serializer/serialization_test.rb 0000664 0000000 0000000 00000015045 12460006062 0032774 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class ArraySerializer class BasicObjectsSerializationTest < Minitest::Test def setup array = [1, 2, 3] @serializer = Serializer.serializer_for(array).new(array) end def test_serializer_for_array_returns_appropriate_type assert_kind_of ActiveModel::ArraySerializer, @serializer end def test_array_serializer_serializes_simple_objects assert_equal [1, 2, 3], @serializer.serializable_array assert_equal [1, 2, 3], @serializer.as_json end end class CustomArraySerializerSupport < Minitest::Test def setup Object.const_set(:ArraySerializer, Class.new) array = [1, 2, 3] @serializer_class = Serializer.serializer_for(array) end def teardown Object.send(:remove_const, :ArraySerializer) end def test_serializer_for_array_returns_appropriate_type assert_equal ::ArraySerializer, @serializer_class end end class ModelSerializationTest < Minitest::Test def test_array_serializer_serializes_models array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })] serializer = ArraySerializer.new(array) expected = [{ name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' }] assert_equal expected, serializer.serializable_array assert_equal expected, serializer.as_json end def test_array_serializers_each_serializer array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })] serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer) expected = [{ name: 'Name 1', description: 'Description 1' }, { name: 'Name 2', description: 'Description 2' }] assert_equal expected, serializer.serializable_array assert_equal expected, serializer.as_json end def test_associated_objects_of_multiple_instances_embedded_in_root @association = PostSerializer._associations[:comments] @old_association = @association.dup @association.embed = :ids @association.embed_in_root = true @post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' }) @post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' }) class << @post2 attr_writer :comments end @post2.comments = [ Comment.new(content: 'C3'), Comment.new(content: 'C4') ] @serializer = ArraySerializer.new([@post1, @post2], root: :posts) assert_equal({ posts: [ {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) }, {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) } ], 'comments' => [ {content: "C1"}, {content: "C2"}, {content: "C3"}, {content: "C4"} ] }, @serializer.as_json) ensure PostSerializer._associations[:comments] = @old_association end def test_embed_object_for_has_one_association_with_nil_value @association = UserSerializer._associations[:profile] @old_association = @association.dup @association.embed = :objects @user1 = User.new({ name: 'User 1', email: 'email1@server.com' }) @user2 = User.new({ name: 'User 2', email: 'email2@server.com' }) class << @user1 def profile nil end end class << @user2 def profile @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1') end end @serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts) assert_equal([ { name: "User 1", email: "email1@server.com", profile: nil }, { name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } } ], @serializer.as_json) ensure UserSerializer._associations[:profile] = @old_association end def test_embed_object_in_root_for_has_one_association_with_nil_value @association = UserSerializer._associations[:profile] @old_association = @association.dup @association.embed = :ids @association.embed_in_root = true @user1 = User.new({ name: 'User 1', email: 'email1@server.com' }) @user2 = User.new({ name: 'User 2', email: 'email2@server.com' }) class << @user1 def profile nil end end class << @user2 def profile @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1') end end @serializer = ArraySerializer.new([@user1, @user2], root: :users) assert_equal({ users: [ { name: "User 1", email: "email1@server.com", 'profile_id' => nil }, { name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id } ], 'profiles' => [ { name: 'Name 1', description: 'Desc 1' } ] }, @serializer.as_json) ensure UserSerializer._associations[:profile] = @old_association end def test_embed_object_in_root_for_has_one_association_with_all_nil_values @association = UserSerializer._associations[:profile] @old_association = @association.dup @association.embed = :ids @association.embed_in_root = true @user1 = User.new({ name: 'User 1', email: 'email1@server.com' }) @user2 = User.new({ name: 'User 2', email: 'email2@server.com' }) class << @user1 def profile nil end end class << @user2 def profile nil end end @serializer = ArraySerializer.new([@user1, @user2], root: :users) assert_equal({ users: [ { name: "User 1", email: "email1@server.com", 'profile_id' => nil }, { name: "User 2", email: "email2@server.com", 'profile_id' => nil } ] }, @serializer.as_json) ensure UserSerializer._associations[:profile] = @old_association end end end end active_model_serializers-0.9.3/test/unit/active_model/default_serializer_test.rb 0000664 0000000 0000000 00000000575 12460006062 0030427 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class DefaultSerializer class Test < Minitest::Test def test_serialize_objects assert_equal(nil, DefaultSerializer.new(nil).serializable_object) assert_equal(1, DefaultSerializer.new(1).serializable_object) assert_equal('hi', DefaultSerializer.new('hi').serializable_object) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/ 0000775 0000000 0000000 00000000000 12460006062 0025330 5 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/unit/active_model/serializer/associations/ 0000775 0000000 0000000 00000000000 12460006062 0030027 5 ustar 00root root 0000000 0000000 build_serializer_test.rb 0000664 0000000 0000000 00000002224 12460006062 0034664 0 ustar 00root root 0000000 0000000 active_model_serializers-0.9.3/test/unit/active_model/serializer/associations require 'test_helper' module ActiveModel class Serializer class Association class BuildSerializerTest < Minitest::Test def setup @association = Association::HasOne.new('post', serializer: PostSerializer) @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' }) @user = User.new end def test_build_serializer_for_array_called_twice 2.times do serializer = @association.build_serializer(@post) assert_instance_of(PostSerializer, serializer) end end def test_build_serializer_from_in_a_namespace assoc = Association::HasOne.new('profile') serializer = TestNamespace::UserSerializer.new(@user).build_serializer(assoc) assert_instance_of(TestNamespace::ProfileSerializer, serializer) end def test_build_serializer_with_prefix assoc = Association::HasOne.new('profile', prefix: :short) serializer = UserSerializer.new(@user).build_serializer(assoc) assert_instance_of(ShortProfileSerializer, serializer) end end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/associations_test.rb 0000664 0000000 0000000 00000001071 12460006062 0031412 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class AssociationsTest < Minitest::Test def test_associations_inheritance inherited_serializer_klass = Class.new(PostSerializer) do has_many :users end another_inherited_serializer_klass = Class.new(PostSerializer) assert_equal([:comments, :users], inherited_serializer_klass._associations.keys) assert_equal([:comments], another_inherited_serializer_klass._associations.keys) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/attributes_test.rb 0000664 0000000 0000000 00000003244 12460006062 0031105 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class AttributesTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile_serializer = ProfileSerializer.new(@profile) end def test_attributes_definition assert_equal([:name, :description], @profile_serializer.class._attributes) end def test_attributes_serialization_using_serializable_hash assert_equal({ name: 'Name 1', description: 'Description 1' }, @profile_serializer.serializable_hash) end def test_attributes_serialization_using_as_json assert_equal({ 'profile' => { name: 'Name 1', description: 'Description 1' } }, @profile_serializer.as_json) end def test_attributes_inheritance inherited_serializer_klass = Class.new(ProfileSerializer) do attributes :comments end another_inherited_serializer_klass = Class.new(ProfileSerializer) assert_equal([:name, :description, :comments], inherited_serializer_klass._attributes) assert_equal([:name, :description], another_inherited_serializer_klass._attributes) end def tests_query_attributes_strip_question_mark model = Class.new(::Model) do def strip? true end end serializer = Class.new(ActiveModel::Serializer) do attributes :strip? end actual = serializer.new(model.new).as_json assert_equal({ strip: true }, actual) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/config_test.rb 0000664 0000000 0000000 00000004477 12460006062 0030175 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class Config class Test < Minitest::Test def test_config_const_is_an_instance_of_config assert_kind_of Config, CONFIG end def test_config_instance config = Config.new config.setting1 = 'value1' assert_equal 'value1', config.setting1 end def test_each_config config = Config.new config.setting1 = 'value1' config.setting2 = 'value2' actual = {} config.each do |k, v| actual[k] = v end assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual) end end class ConfigTest < Minitest::Test def test_setup Serializer.setup do |config| config.a = 'v1' config.b = 'v2' end assert_equal 'v1', CONFIG.a assert_equal 'v2', CONFIG.b ensure CONFIG.clear end def test_config_accessors Serializer.setup do |config| config.foo = 'v1' config.bar = 'v2' end assert_equal 'v1', CONFIG.foo assert_equal 'v2', CONFIG.bar ensure CONFIG.clear end def test_acessor_when_nil assert_nil CONFIG.foo CONFIG.foo = 1 assert_equal 1, CONFIG.foo assert_nil CONFIG.bar end end class ApplyConfigTest < Minitest::Test def test_apply_config_to_associations CONFIG.embed = :ids CONFIG.embed_in_root = true CONFIG.key_format = :lower_camel association = PostSerializer._associations[:comments] old_association = association.dup association.send :initialize, association.name, association.options assert association.embed_ids? assert !association.embed_objects? assert association.embed_in_root assert_equal :lower_camel, association.key_format assert_equal 'post', PostSerializer.root_name CONFIG.plural_default_root = true assert_equal 'posts', PostSerializer.root_name ensure PostSerializer._associations[:comments] = old_association CONFIG.clear end end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/filter_test.rb 0000664 0000000 0000000 00000004027 12460006062 0030204 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class FilterOptionsTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end def test_only_option @profile_serializer = ProfileSerializer.new(@profile, only: :name) assert_equal({ 'profile' => { name: 'Name 1' } }, @profile_serializer.as_json) end def test_except_option @profile_serializer = ProfileSerializer.new(@profile, except: :comments) assert_equal({ 'profile' => { name: 'Name 1', description: 'Description 1' } }, @profile_serializer.as_json) end end class FilterAttributesTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile_serializer = ProfileSerializer.new(@profile) @profile_serializer.instance_eval do def filter(keys) keys - [:description] end end end def test_filtered_attributes_serialization assert_equal({ 'profile' => { name: 'Name 1' } }, @profile_serializer.as_json) end end class FilterAssociationsTest < Minitest::Test def setup @association = PostSerializer._associations[:comments] @old_association = @association.dup @association.embed = :ids @association.embed_in_root = true @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' }) @post_serializer = PostSerializer.new(@post) @post_serializer.instance_eval do def filter(keys) keys - [:body, :comments] end end end def teardown PostSerializer._associations[:comments] = @old_association end def test_filtered_associations_serialization assert_equal({ 'post' => { title: 'Title 1' } }, @post_serializer.as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/has_many_polymorphic_test.rb 0000664 0000000 0000000 00000013036 12460006062 0033143 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class HasManyPolymorphicTest < ActiveModel::TestCase def setup @association = MailSerializer._associations[:attachments] @old_association = @association.dup @mail = Mail.new({ body: 'Body 1' }) @mail_serializer = MailSerializer.new(@mail) end def teardown MailSerializer._associations[:attachments] = @old_association end def model_name(object) object.class.to_s.demodulize.underscore.to_sym end def test_associations_definition assert_equal 1, MailSerializer._associations.length assert_kind_of Association::HasMany, @association assert_equal true, @association.polymorphic assert_equal 'attachments', @association.name end def test_associations_embedding_ids_serialization_using_serializable_hash @association.embed = :ids assert_equal({ body: 'Body 1', 'attachment_ids' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end }, @mail_serializer.serializable_hash) end def test_associations_embedding_ids_serialization_using_as_json @association.embed = :ids assert_equal({ 'mail' => { :body => 'Body 1', 'attachment_ids' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end } }, @mail_serializer.as_json) end def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options @association.embed = :ids @association.key = 'key' assert_equal({ body: 'Body 1', 'key' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end }, @mail_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_serializable_hash @association.embed = :objects assert_equal({ body: 'Body 1', :attachments => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] }, @mail_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_as_json @association.embed = :objects assert_equal({ 'mail' => { body: 'Body 1', attachments: [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] } }, @mail_serializer.as_json) end def test_associations_embedding_nil_objects_serialization_using_as_json @association.embed = :objects @mail.instance_eval do def attachments [nil] end end assert_equal({ 'mail' => { :body => 'Body 1', :attachments => [nil] } }, @mail_serializer.as_json) end def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options @association.embed = :objects @association.embedded_key = 'root' assert_equal({ body: 'Body 1', 'root' => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] }, @mail_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash @association.embed = :ids @association.embed_in_root = true assert_equal({ body: 'Body 1', 'attachment_ids' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end }, @mail_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_as_json @association.embed = :ids @association.embed_in_root = true assert_equal({ 'mail' => { body: 'Body 1', 'attachment_ids' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end, }, 'attachments' => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] }, @mail_serializer.as_json) end def test_associations_embedding_nothing_including_objects_serialization_using_as_json @association.embed = nil @association.embed_in_root = true assert_equal({ 'mail' => { body: 'Body 1' }, 'attachments' => [ { type: :image, image: { url: 'U1' }}, { type: :video, video: { html: 'H1' }} ] }, @mail_serializer.as_json) end def test_associations_using_a_given_serializer @association.embed = :ids @association.embed_in_root = true @association.serializer_from_options = Class.new(ActiveModel::Serializer) do def fake 'fake' end attributes :fake end assert_equal({ 'mail' => { body: 'Body 1', 'attachment_ids' => @mail.attachments.map do |c| { id: c.object_id, type: model_name(c) } end }, 'attachments' => [ { type: :image, image: { fake: 'fake' }}, { type: :video, video: { fake: 'fake' }} ] }, @mail_serializer.as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/has_many_test.rb 0000664 0000000 0000000 00000022146 12460006062 0030520 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class HasManyTest < Minitest::Test def setup @association = PostSerializer._associations[:comments] @old_association = @association.dup @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' }) @post_serializer = PostSerializer.new(@post) end def teardown PostSerializer._associations[:comments] = @old_association end def test_associations_definition assert_equal 1, PostSerializer._associations.length assert_kind_of Association::HasMany, @association assert_equal 'comments', @association.name end def test_associations_inheritance inherited_serializer_klass = Class.new(PostSerializer) do has_many :some_associations end another_inherited_serializer_klass = Class.new(PostSerializer) assert_equal(PostSerializer._associations.length + 1, inherited_serializer_klass._associations.length) assert_equal(PostSerializer._associations.length, another_inherited_serializer_klass._associations.length) end def test_associations_embedding_ids_serialization_using_serializable_hash @association.embed = :ids assert_equal({ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end def test_associations_embedding_ids_serialization_using_as_json @association.embed = :ids assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } } }, @post_serializer.as_json) end def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options @association.embed = :ids @association.key = 'key' assert_equal({ title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_serializable_hash @association.embed = :objects assert_equal({ title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_as_json @association.embed = :objects assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] } }, @post_serializer.as_json) end def test_associations_embedding_nil_objects_serialization_using_as_json @association.embed = :objects @post.instance_eval do def comments [nil] end end assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] } }, @post_serializer.as_json) end def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options @association.embed = :objects @association.embedded_key = 'root' assert_equal({ title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash @association.embed = :ids @association.embed_in_root = true assert_equal({ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, @post_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_as_json @association.embed = :ids @association.embed_in_root = true assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, 'comments' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer @association.embed = :ids @association.embed_in_root = true category = Category.new(name: 'Name 1') category.instance_variable_set(:@posts, [@post]) category_serializer = CategorySerializer.new(category) assert_equal({ 'category' => { name: 'Name 1', posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }] }, "comments" => [{ content: 'C1' }, { content: 'C2' }] }, category_serializer.as_json) end def test_associations_embedding_nothing_including_objects_serialization_using_as_json @association.embed = nil @association.embed_in_root = true assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1' }, 'comments' => [{ content: 'C1' }, { content: 'C2' }] }, @post_serializer.as_json) end def test_associations_using_a_given_serializer @association.embed = :ids @association.embed_in_root = true @association.serializer_from_options = Class.new(Serializer) do def content object.read_attribute_for_serialization(:content) + '!' end attributes :content end assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, 'comments' => [{ content: 'C1!' }, { content: 'C2!' }] }, @post_serializer.as_json) end def test_associations_embedding_ids_using_a_given_array_serializer @association.embed = :ids @association.embed_in_root = true @association.serializer_from_options = Class.new(ArraySerializer) do def serializable_object { my_content: ['fake'] } end end assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }, 'comments' => { my_content: ['fake'] } }, @post_serializer.as_json) end def test_associations_embedding_objects_using_a_given_array_serializer @association.serializer_from_options = Class.new(ArraySerializer) do def serializable_object { my_content: ['fake'] } end end assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } } }, @post_serializer.as_json) end def test_associations_embedding_ids_including_objects_serialization_with_embed_in_root_key @association.embed_in_root = true @association.embed_in_root_key = :linked @association.embed = :ids assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map(&:object_id) }, linked: { 'comments' => [ { content: 'C1' }, { content: 'C2' } ] }, }, @post_serializer.as_json) end def test_associations_embedding_ids_using_embed_namespace_including_object_serialization_with_embed_in_root_key @association.embed_in_root = true @association.embed_in_root_key = :linked @association.embed = :ids @association.embed_namespace = :links @association.key = :comments assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', links: { comments: @post.comments.map(&:object_id) } }, linked: { 'comments' => [ { content: 'C1' }, { content: 'C2' } ] }, }, @post_serializer.as_json) end def test_associations_embedding_objects_using_embed_namespace @association.embed = :objects @association.embed_namespace = :links assert_equal({ 'post' => { title: 'Title 1', body: 'Body 1', links: { comments: [ { content: 'C1' }, { content: 'C2' } ] } } }, @post_serializer.as_json) end def test_associations_name_key_embedding_ids_serialization_using_serializable_hash @association = NameKeyPostSerializer._associations[:comments] @association.embed = :ids assert_equal({ title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }, NameKeyPostSerializer.new(@post).serializable_hash) end def test_associations_name_key_embedding_ids_serialization_using_as_json @association = NameKeyPostSerializer._associations[:comments] @association.embed = :ids assert_equal({ 'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } } }, NameKeyPostSerializer.new(@post).as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/has_one_and_has_many_test.rb 0000664 0000000 0000000 00000001400 12460006062 0033024 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class HasOneAndHasManyTest < Minitest::Test def setup @post = SpecialPost.new({ title: 'T1', body: 'B1'}) @post_serializer = SpecialPostSerializer.new(@post) end def teardown end def test_side_load_has_one_and_has_many_in_same_array assert_equal({ "post" => { title: 'T1', body: 'B1', 'comment_ids' => @post.comments.map { |c| c.object_id }, 'special_comment_id' => @post_serializer.special_comment.object_id, }, "comments" => [{ content: 'C1' }, { content: 'C2' }, { content: 'special' }] }, @post_serializer.as_json(root: 'post')) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/has_one_polymorphic_test.rb 0000664 0000000 0000000 00000013404 12460006062 0032757 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class HasOnePolymorphicTest < ActiveModel::TestCase def setup @association = InterviewSerializer._associations[:attachment] @old_association = @association.dup @interview = Interview.new({ text: 'Text 1' }) @interview_serializer = InterviewSerializer.new(@interview) end def teardown InterviewSerializer._associations[:attachment] = @old_association end def model_name(object) object.class.to_s.demodulize.underscore.to_sym end def test_associations_definition assert_equal 1, InterviewSerializer._associations.length assert_kind_of Association::HasOne, @association assert_equal true, @association.polymorphic assert_equal 'attachment', @association.name end def test_associations_embedding_ids_serialization_using_serializable_hash @association.embed = :ids assert_equal({ text: 'Text 1', 'attachment_id' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } }, @interview_serializer.serializable_hash) end def test_associations_embedding_ids_serialization_using_as_json @association.embed = :ids assert_equal({ 'interview' => { text: 'Text 1', 'attachment_id' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } } }, @interview_serializer.as_json) end def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options @association.embed = :ids @association.key = 'key' assert_equal({ text: 'Text 1', 'key' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } }, @interview_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_serializable_hash @association.embed = :objects assert_equal({ text: 'Text 1', attachment: { type: model_name(@interview.attachment), model_name(@interview.attachment) => { url: 'U1'} } }, @interview_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_as_json @association.embed = :objects assert_equal({ 'interview' => { text: 'Text 1', attachment: { type: model_name(@interview.attachment), model_name(@interview.attachment) => { url: 'U1'} } } }, @interview_serializer.as_json) end def test_associations_embedding_nil_ids_serialization_using_as_json @association.embed = :ids @interview.instance_eval do def attachment nil end end assert_equal({ 'interview' => { text: 'Text 1', 'attachment_id' => nil } }, @interview_serializer.as_json) end def test_associations_embedding_nil_objects_serialization_using_as_json @association.embed = :objects @interview.instance_eval do def attachment nil end end assert_equal({ 'interview' => { text: 'Text 1', attachment: nil } }, @interview_serializer.as_json) end def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options @association.embed = :objects @association.embedded_key = 'root' assert_equal({ text: 'Text 1', 'root' => { type: model_name(@interview.attachment), model_name(@interview.attachment) => { url: 'U1'} } }, @interview_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash @association.embed = :ids @association.embed_in_root = true assert_equal({ text: 'Text 1', 'attachment_id' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } }, @interview_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_as_json @association.embed = :ids @association.embed_in_root = true assert_equal({ 'interview' => { text: 'Text 1', 'attachment_id' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } }, "attachments" => [{ type: model_name(@interview.attachment), model_name(@interview.attachment) => { url: 'U1' } }] }, @interview_serializer.as_json) end def test_associations_using_a_given_serializer @association.embed = :ids @association.embed_in_root = true @association.serializer_from_options = Class.new(ActiveModel::Serializer) do def name 'fake' end attributes :name end assert_equal({ 'interview' => { text: 'Text 1', 'attachment_id' => { type: model_name(@interview.attachment), id: @interview.attachment.object_id } }, "attachments" => [{ type: model_name(@interview.attachment), model_name(@interview.attachment) => { name: 'fake' } }] }, @interview_serializer.as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/has_one_test.rb 0000664 0000000 0000000 00000017646 12460006062 0030346 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class HasOneTest < Minitest::Test def setup @association = UserSerializer._associations[:profile] @old_association = @association.dup @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' }) @user_serializer = UserSerializer.new(@user) end def teardown UserSerializer._associations[:profile] = @old_association end def test_associations_definition assert_equal 1, UserSerializer._associations.length assert_kind_of Association::HasOne, @association assert_equal 'profile', @association.name end def test_associations_embedding_ids_serialization_using_serializable_hash @association.embed = :ids assert_equal({ name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, @user_serializer.serializable_hash) end def test_associations_embedding_ids_serialization_using_as_json @association.embed = :ids assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id } }, @user_serializer.as_json) end def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options @association.embed = :ids @association.key = 'key' assert_equal({ name: 'Name 1', email: 'mail@server.com', 'key' => @user.profile.object_id }, @user_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_serializable_hash @association.embed = :objects assert_equal({ name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } }, @user_serializer.serializable_hash) end def test_associations_embedding_objects_serialization_using_as_json @association.embed = :objects assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'N1', description: 'D1' } } }, @user_serializer.as_json) end def test_associations_embedding_nil_ids_serialization_using_as_json @association.embed = :ids @user.instance_eval do def profile nil end end assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil } }, @user_serializer.as_json) end def test_associations_embedding_nil_objects_serialization_using_as_json @association.embed = :objects @user.instance_eval do def profile nil end end assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: nil } }, @user_serializer.as_json) end def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options @association.embed = :objects @association.embedded_key = 'root' assert_equal({ name: 'Name 1', email: 'mail@server.com', 'root' => { name: 'N1', description: 'D1' } }, @user_serializer.serializable_hash) end def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash @association.embed = :ids @association.embed_in_root = true assert_equal({ name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, @user_serializer.serializable_hash) end def test_associations_embedding_nil_ids_including_objects_serialization_using_as_json @association.embed = :ids @association.embed_in_root = true @user.instance_eval do def profile nil end end assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil }, 'profiles' => [] }, @user_serializer.as_json) end def test_associations_embedding_ids_including_objects_serialization_using_as_json @association.embed = :ids @association.embed_in_root = true assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, 'profiles' => [{ name: 'N1', description: 'D1' }] }, @user_serializer.as_json) end def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer @association.embed = :ids @association.embed_in_root = true user_info = UserInfo.new user_info.instance_variable_set(:@user, @user) user_info_serializer = UserInfoSerializer.new(user_info) assert_equal({ 'user_info' => { user: { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id } }, 'profiles' => [{ name: 'N1', description: 'D1' }] }, user_info_serializer.as_json) end def test_associations_embedding_ids_using_a_given_serializer @association.embed = :ids @association.embed_in_root = true @association.serializer_from_options = Class.new(Serializer) do def name 'fake' end attributes :name end assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, 'profiles' => [{ name: 'fake' }] }, @user_serializer.as_json) end def test_associations_embedding_objects_using_a_given_serializer @association.serializer_from_options = Class.new(Serializer) do def name 'fake' end attributes :name end assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', profile: { name: 'fake' } } }, @user_serializer.as_json) end def test_associations_embedding_ids_using_embed_namespace @association.embed_namespace = :links @association.embed = :ids @association.key = :profile assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', links: { profile: @user.profile.object_id } } }, @user_serializer.as_json) end def test_asociations_embedding_objects_using_embed_namespace @association.embed_namespace = :links @association.embed = :objects assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', links: { profile: { name: 'N1', description: 'D1' } } } }, @user_serializer.as_json) end def test_associations_embedding_ids_using_embed_namespace_and_embed_in_root_key @association.embed_in_root = true @association.embed_in_root_key = :linked @association.embed = :ids assert_equal({ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => @user.profile.object_id }, linked: { 'profiles' => [ { name: 'N1', description: 'D1' } ] } }, @user_serializer.as_json) end def test_associations_name_key_embedding_ids_serialization_using_serializable_hash @association = NameKeyUserSerializer._associations[:profile] @association.embed = :ids assert_equal({ name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id }, NameKeyUserSerializer.new(@user).serializable_hash) end def test_associations_name_key_embedding_ids_serialization_using_as_json @association = NameKeyUserSerializer._associations[:profile] @association.embed = :ids assert_equal({ 'name_key_user' => { name: 'Name 1', email: 'mail@server.com', 'profile' => @user.profile.object_id } }, NameKeyUserSerializer.new(@user).as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/key_format_test.rb 0000664 0000000 0000000 00000001433 12460006062 0031055 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class KeyFormatTest < Minitest::Test def test_lower_camel_format_option object = WebLog.new({ name: 'Name 1', display_name: 'Display Name 1'}) serializer = WebLogSerializer.new(object, key_format: :lower_camel) expected = { name: 'Name 1', displayName: 'Display Name 1' } assert_equal expected, serializer.serializable_object end def test_lower_camel_format_serializer object = WebLog.new({ name: 'Name 1', display_name: 'Display Name 1'}) serializer = WebLogLowerCamelSerializer.new(object) expected = { name: 'Name 1', displayName: 'Display Name 1' } assert_equal expected, serializer.serializable_object end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/meta_test.rb 0000664 0000000 0000000 00000001737 12460006062 0027652 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class MetaTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) end def test_meta profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta: { total: 10 }) assert_equal({ 'profile' => { name: 'Name 1', description: 'Description 1' }, meta: { total: 10 } }, profile_serializer.as_json) end def test_meta_using_meta_key profile_serializer = ProfileSerializer.new(@profile, root: 'profile', meta_key: :my_meta, my_meta: { total: 10 }) assert_equal({ 'profile' => { name: 'Name 1', description: 'Description 1' }, my_meta: { total: 10 } }, profile_serializer.as_json) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/options_test.rb 0000664 0000000 0000000 00000002016 12460006062 0030406 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class OptionsTest < Minitest::Test def setup @serializer = ProfileSerializer.new(nil, context: {foo: :bar}) end def test_custom_options_are_accessible_from_serializer assert_equal({foo: :bar}, @serializer.context) end end class SerializationOptionsTest < Minitest::Test def setup @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @profile_serializer = ProfileSerializer.new(@profile) @profile_serializer.instance_eval do def description serialization_options[:force_the_description] end end end def test_filtered_attributes_serialization forced_description = "This is a test" assert_equal({ 'profile' => { name: 'Name 1', description: forced_description } }, @profile_serializer.as_json(force_the_description: forced_description)) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/root_test.rb 0000664 0000000 0000000 00000006571 12460006062 0027710 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class RootAsOptionTest < Minitest::Test def setup @old_root = ProfileSerializer._root @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @serializer = ProfileSerializer.new(@profile, root: :initialize) ProfileSerializer._root = true end def teardown ProfileSerializer._root = @old_root end def test_root_is_not_displayed_using_serializable_hash assert_equal({ name: 'Name 1', description: 'Description 1' }, @serializer.serializable_hash) end def test_root_using_as_json assert_equal({ initialize: { name: 'Name 1', description: 'Description 1' } }, @serializer.as_json) end def test_root_from_serializer_name @serializer = ProfileSerializer.new(@profile) assert_equal({ 'profile' => { name: 'Name 1', description: 'Description 1' } }, @serializer.as_json) end def test_root_as_argument_takes_precedence assert_equal({ argument: { name: 'Name 1', description: 'Description 1' } }, @serializer.as_json(root: :argument)) end def test_using_false_root_in_initializer_takes_precedence ProfileSerializer._root = 'root' @serializer = ProfileSerializer.new(@profile, root: false) assert_equal({ name: 'Name 1', description: 'Description 1' }, @serializer.as_json) end def test_root_inheritance ProfileSerializer._root = 'profile' inherited_serializer_klass = Class.new(ProfileSerializer) inherited_serializer_klass._root = 'inherited_profile' another_inherited_serializer_klass = Class.new(ProfileSerializer) assert_equal('inherited_profile', inherited_serializer_klass._root) assert_equal('profile', another_inherited_serializer_klass._root) end end class RootInSerializerTest < Minitest::Test def setup @old_root = ProfileSerializer._root ProfileSerializer._root = :in_serializer profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) @serializer = ProfileSerializer.new(profile) @rooted_serializer = ProfileSerializer.new(profile, root: :initialize) end def teardown ProfileSerializer._root = @old_root end def test_root_is_not_displayed_using_serializable_hash assert_equal({ name: 'Name 1', description: 'Description 1' }, @serializer.serializable_hash) end def test_root_using_as_json assert_equal({ in_serializer: { name: 'Name 1', description: 'Description 1' } }, @serializer.as_json) end def test_root_in_initializer_takes_precedence assert_equal({ initialize: { name: 'Name 1', description: 'Description 1' } }, @rooted_serializer.as_json) end def test_root_as_argument_takes_precedence assert_equal({ argument: { name: 'Name 1', description: 'Description 1' } }, @rooted_serializer.as_json(root: :argument)) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/scope_test.rb 0000664 0000000 0000000 00000002174 12460006062 0030031 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class ScopeTest < Minitest::Test def setup @serializer = ProfileSerializer.new(nil, scope: current_user) end def test_scope assert_equal('user', @serializer.scope) end private def current_user 'user' end end class NestedScopeTest < Minitest::Test def setup @association = UserSerializer._associations[:profile] @old_association = @association.dup @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' }) @user_serializer = UserSerializer.new(@user, scope: 'user') end def teardown UserSerializer._associations[:profile] = @old_association end def test_scope_passed_through @association.serializer_from_options = Class.new(Serializer) do def name scope end attributes :name end assert_equal({ name: 'Name 1', email: 'mail@server.com', profile: { name: 'user' } }, @user_serializer.serializable_hash) end end end end active_model_serializers-0.9.3/test/unit/active_model/serializer/url_helpers_test.rb 0000664 0000000 0000000 00000001545 12460006062 0031245 0 ustar 00root root 0000000 0000000 require 'test_helper' module ActiveModel class Serializer class UrlHelpersTest < Minitest::Test include Rails.application.routes.url_helpers def setup Object.const_set 'UserController', Class.new(ActionController::Base) do def show render text: 'profile' end end Rails.application.routes.draw do get '/profile/:id', as: :profile, controller: :user, action: :show end end def test_url_helpers_are_available serializer = Class.new(ActiveModel::Serializer) do attributes :url def url profile_url(id: object.object_id) end end profile = Profile.new assert_equal({ url: profile_url(id: profile.object_id) }, serializer.new(profile).as_json) end end end end