ActiveModel::Serializer implementation and Rails hooks
Go to file
Benjamin Fleischer 80af763d2e Make test attributes explicit
- Organize test poros with associations and by serializer
- Freeze derived attributes/associations against mutation
- Cleanup PORO fixtures
2016-12-04 19:33:39 -06:00
.github Fix GitHub template formatting and spelling 2016-02-26 11:24:57 -07:00
bin Merge pull request #1588 from bf4/benchmark_revision_runner 2016-03-27 11:24:07 +02:00
docs Use consistent capitalization for JSONAPI adapter references 2016-11-29 10:41:16 -05:00
lib Make test attributes explicit 2016-12-04 19:33:39 -06:00
test Make test attributes explicit 2016-12-04 19:33:39 -06:00
.gitignore adds mac files to .gitignore (#1728) 2016-05-17 13:51:26 -04:00
.rubocop.yml re: RuboCop - replace rocket style hashes 2016-06-21 22:08:27 +01:00
.simplecov Update SimpleCov; remove compatibility patch 2016-02-09 20:59:31 -06:00
.travis.yml Run tests by Ruby 2.2.6 and 2.3.3 2016-12-04 00:20:24 +09:00
active_model_serializers.gemspec Pin jsonapi version. (#1955) 2016-10-25 07:43:40 -04:00
appveyor.yml Fix appveyor setting for JRuby 2016-04-25 09:24:51 +09:00
CHANGELOG.md Better AMS Model attributes interface 2016-11-21 09:14:26 -06:00
CODE_OF_CONDUCT.md add a code of conduct 2016-08-31 23:03:28 +02:00
CONTRIBUTING.md Bump to v0.10.0 2016-05-17 12:49:37 -06:00
Gemfile added active record benchmark (#1919) 2016-09-24 16:39:29 -04:00
MIT-LICENSE Improvements from Rails plugin template 2016-04-01 05:39:03 -05:00
Rakefile re: RuboCop - Suppress of handling LoadError for optional dependencies 2016-06-20 22:15:58 +01:00
README.md Better AMS Model attributes interface 2016-11-21 09:14:26 -06:00

ActiveModelSerializers

Build Status Build Status Build status
Code Quality Code Quality codebeat Test Coverage
Issue Stats Pulse

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 (no JSON root). 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.

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

0.10.x is 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.

Installation

Add this line to your application's Gemfile:

gem 'active_model_serializers', '~> 0.10.0'

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!

Documentation

High-level behavior

Choose an adapter from adapters:

ActiveModelSerializers.config.adapter = :json_api # Default: `:attributes`

Given a serializable model:

# either
class SomeResource < ActiveRecord::Base
  # columns: title, body
end
# or
class SomeResource < ActiveModelSerializers::Model
  attributes :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 = ActiveModelSerializers::SerializableResource.new(resource, options)
serialization.to_json
serialization.as_json

SerializableResource delegates to the adapter, which it builds as:

adapter_options = {}
adapter = ActiveModelSerializers::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.

Semantic Versioning

This project adheres to semver

Contributing

See CONTRIBUTING.md