mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Tidied up readme
This commit is contained in:
parent
4cba68f095
commit
811b6ce583
1 changed files with 11 additions and 29 deletions
40
README.md
40
README.md
|
|
@ -44,7 +44,7 @@ See [active_model_serializers](https://github.com/rails-api/active_model_seriali
|
||||||
|
|
||||||
### Serializers are inferred by active_record model names
|
### Serializers are inferred by active_record model names
|
||||||
|
|
||||||
grape-active_model_serializers will search for serializers for the objects returned by your grape API.
|
`grape-active_model_serializers` will search for serializers for the objects returned by your grape API.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
namespace :users do
|
namespace :users do
|
||||||
|
|
@ -58,44 +58,27 @@ In this case, as User objects are being returned, grape-active_model_serializers
|
||||||
|
|
||||||
### Manually specifying serializer options
|
### Manually specifying serializer options
|
||||||
|
|
||||||
Serializers can be specified at a route level by with the serializer option. A serializer can be specified by passing the the serializer class or the serializer name. The following are equivalent:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
get "/home", :serializer => HomeSerializer
|
|
||||||
...
|
|
||||||
```
|
|
||||||
```ruby
|
|
||||||
get "/home", :serializer => "home"
|
|
||||||
...
|
|
||||||
```
|
|
||||||
```ruby
|
|
||||||
get "/home", :serializer => :home
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
You can also set a serializer at the namespace level. This serializer can/will be overriden if a serilizer is also specified on the route.
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
# Serializer options can be specified on routes or namespaces.
|
||||||
namespace 'foo', :serializer => :bar do
|
namespace 'foo', :serializer => :bar do
|
||||||
get "/" do
|
get "/" do
|
||||||
# will use "bar" serializer
|
# will use "bar" serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Options specified on a route or namespace override those of the containing namespace.
|
||||||
get "/home", :serializer => :home do
|
get "/home", :serializer => :home do
|
||||||
# will use "home" serializer
|
# will use "home" serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# All standard options for `ActiveModel::Serializers` are supported.
|
||||||
|
get "/fancy_homes", :root => 'world', :each_serializer => :fancy_homes
|
||||||
|
...
|
||||||
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
Other standard options for `ActiveModel::Serializers` can be provided at either the namespace or route level with the same overriding behavior.
|
|
||||||
|
|
||||||
```ruby
|
### Full Example
|
||||||
get "/home", :root => 'world', :each_serializer => :fancy_home
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
### Example
|
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
|
|
@ -116,7 +99,7 @@ API.new.get "/home" # => '{:user=>{:first_name=>"JR", :last_name=>"HE"}}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Rspec
|
## RSpec
|
||||||
|
|
||||||
See "Writing Tests" in https://github.com/intridea/grape.
|
See "Writing Tests" in https://github.com/intridea/grape.
|
||||||
|
|
||||||
|
|
@ -132,9 +115,8 @@ Enjoy :)
|
||||||
|
|
||||||
|
|
||||||
## Thanks to
|
## Thanks to
|
||||||
|
|
||||||
The developers and maintainers of:
|
The developers and maintainers of:
|
||||||
[active_model_serializers](https://github.com/rails-api/active_model_serializers)
|
[active_model_serializers](https://github.com/rails-api/active_model_serializers)
|
||||||
[Grape](https://github.com/intridea/grape)!
|
[Grape](https://github.com/intridea/grape)!
|
||||||
|
|
||||||
Structured and based upon [Grape](https://github.com/LTe/grape-rabl).
|
Structured and based upon [grape-rabl](https://github.com/LTe/grape-rabl).
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue