ActiveModel::Serializer implementation and Rails hooks
Go to file
2015-12-23 09:56:07 -06:00
docs Add original design doc from 0.8 2015-12-23 09:45:45 -06:00
lib Merge pull request #1383 from beauby/simplify-associations 2015-12-22 10:37:28 -06:00
test Merge pull request #1384 from bf4/fix_ci_failures 2015-12-21 17:23:04 -06:00
.gitignore Fix bundler caching in travis & Appveyor 2015-11-24 01:53:45 +00:00
.rubocop_todo.yml Rename ArraySerializer to CollectionSerializer for clarity 2015-10-21 16:53:26 -05:00
.rubocop.yml Enforce case requires else; allow else nil 2015-09-21 09:50:53 -05:00
.simplecov Disable coverage/warnings output when passing in dev 2015-09-21 00:39:56 -05:00
.travis.yml Drop JRuby 1.9 2015-12-22 20:23:29 -02:00
active_model_serializers.gemspec Merge pull request #1371 from bf4/documentation_updates 2015-12-20 22:30:44 -06:00
appveyor.yml Fix bundler caching in travis & Appveyor 2015-11-24 01:53:45 +00:00
CHANGELOG.md Add some pre-history [ci skip] 2015-12-23 09:56:07 -06:00
CONTRIBUTING.md Add CHANGELOG from 0.9 2015-12-23 09:45:45 -06:00
Gemfile Patch ActionController::TestCase#assigns for Rails5 2015-11-25 21:56:01 -06:00
LICENSE.txt Generate a basic gem 2014-07-05 00:53:48 -04:00
Rakefile Separate default rake from rake ci 2015-10-22 10:45:24 -05:00
README.md Add link to slack per discussion with duduribeiro [ci skip] 2015-12-15 22:06:13 -06:00

ActiveModelSerializers

Build Status (Windows: Build status) Code Quality Test Coverage

Documentation

About

ActiveModelSerializers brings convention over configuration to your JSON generation.

ActiveModelSerializers works through two components: serializers and adapters.

Serializers describe which attributes and relationships should be serialized.

Adapters describe how attributes and relationships should be serialized.

SerializableResource co-ordinates the resource, Adapter and Serializer to produce the resource serialization. The serialization has the #as_json, #to_json and #serializable_hash methods used by the Rails JSON Renderer. (SerializableResource actually delegates these methods to the adapter.)

By default ActiveModelSerializers will use the Attributes Adapter. But we strongly advise you to use JsonApi Adapter, which follows 1.0 of the format specified in jsonapi.org/format. Check how to change the adapter in the sections below.

RELEASE CANDIDATE, PLEASE READ

This is the master branch of ActiveModelSerializers.

It will become the 0.10.0 release when it's ready. Currently this is a release candidate.

0.10.x is not backward compatible with 0.9.x nor 0.8.x.

0.10.x will be based on the 0.8.0 code, but with a more flexible architecture. We'd love your help. Learn how you can help here.

It is generally safe and recommended to use the master branch.

For more information, see the post 'The future of AMS'.

Installation

Note: ActiveModelSerializers is already included on Rails >= 5

Add this line to your application's Gemfile:

gem 'active_model_serializers'

And then execute:

$ bundle

Getting Started

See Getting Started for the nuts and bolts.

More information is available in the Guides and High-level behavior.

Getting Help

If you find a bug, please report an Issue and see our contributing guide.

If you have a question, please post to Stack Overflow.

If you'd like to chat, we have a community slack.

Thanks!

High-level behavior

Given a serializable model:

# either
class SomeResource < ActiveRecord::Base
  # columns: title, body
end
# or
class SomeResource < ActiveModelSerializers::Model
  attr_accessor :title, :body
end

And initialized as:

resource = SomeResource.new(title: 'ActiveModelSerializers', body: 'Convention over configuration')

Given a serializer for the serializable model:

class SomeSerializer < ActiveModel::Serializer
  attribute :title, key: :name
  attributes :body
end

The model can be serialized as:

options = {}
serialization = SerializableResource.new(resource, options)
serialization.to_json
serialization.as_json

SerializableResource delegates to the adapter, which it builds as:

adapter_options = {}
adapter = Adapter.create(serializer, adapter_options)
adapter.to_json
adapter.as_json
adapter.serializable_hash

The adapter formats the serializer's attributes and associations (a.k.a. includes):

serializer_options = {}
serializer = SomeSerializer.new(resource, serializer_options)
serializer.attributes
serializer.associations

See ARCHITECTURE.md for more information.

Contributing

See CONTRIBUTING.md