ActiveModel::Serializer implementation and Rails hooks
Go to file
Benjamin Fleischer 4cc454d49b Setup benchmarking structure
- Setup dummy app files in `test/dummy`
- Setup dummy test server `bin/serve_dummy
  - Note:  Serializer caching can be completely disabled by passing in
  `CACHE_ON=off bin/serve_dummy start` since Serializer#_cache is only
  set at boot.
- run with
  - ./bin/bench
  - `bin/bench` etc adapted from ruby-bench-suite
  - target files are `test/dummy/bm_*.rb`. Just add another to run it.
  - benchmark cache/no cache
  - remove rake dependency that loads unnecessary files
- remove git gem dependency
  - Running over revisions to be added in subsequent PR
2016-03-09 12:42:25 -06:00
.github Fix GitHub template formatting and spelling 2016-02-26 11:24:57 -07:00
bin Setup benchmarking structure 2016-03-09 12:42:25 -06:00
docs Provide Rails url_helpers via SerializationContext 2016-03-07 10:40:34 -07:00
lib Refactor fragment cache methods 2016-03-08 23:51:10 +01:00
test Setup benchmarking structure 2016-03-09 12:42:25 -06:00
.gitignore Adds support for top-level links to JsonApi adapter 2016-02-03 10:16:22 -02:00
.rubocop_todo.yml Update rubocop todos 2016-03-08 22:32:34 +01:00
.rubocop.yml Setup benchmarking structure 2016-03-09 12:42:25 -06:00
.simplecov Update SimpleCov; remove compatibility patch 2016-02-09 20:59:31 -06:00
.travis.yml Remove rbx from CI. Allowing failures is just wasting resources 2016-03-08 23:01:43 -06:00
active_model_serializers.gemspec Setup benchmarking structure 2016-03-09 12:42:25 -06:00
appveyor.yml add JRuby 9000 to appveyor.yml 2016-02-04 22:35:04 -06:00
CHANGELOG.md Info about codebeat badge in misc section. 2016-03-09 14:33:37 +01:00
CONTRIBUTING.md Simplify contributing.md; move details into GitHub templates 2016-02-26 10:43:52 -07:00
Gemfile Setup benchmarking structure 2016-03-09 12:42:25 -06:00
LICENSE.txt Generate a basic gem 2014-07-05 00:53:48 -04:00
Rakefile Setup benchmarking structure 2016-03-09 12:42:25 -06:00
README.md Organize the badges 2016-03-08 13:17:46 -05:00

ActiveModelSerializers

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

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

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