mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Improved readme
This commit is contained in:
parent
cbbe2e79d9
commit
d3db8672ab
2 changed files with 13 additions and 12 deletions
24
README.md
24
README.md
|
|
@ -47,7 +47,6 @@ See [active_model_serializers](https://github.com/rails-api/active_model_seriali
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
Serializer
|
|
||||||
```ruby
|
```ruby
|
||||||
namespace :users do
|
namespace :users do
|
||||||
get ":id" do
|
get ":id" do
|
||||||
|
|
@ -67,7 +66,7 @@ Grape::Formatter::ActiveModelSerializers.infer_serializers = false
|
||||||
|
|
||||||
### Manually specifying a serializer
|
### Manually specifying a serializer
|
||||||
|
|
||||||
Serializers can be specified at a route level by with the serializer option. A serializer can be specified with its class name or by name as string/symbol. The following are equivalent:
|
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
|
```ruby
|
||||||
get "/home", :serializer => HomeSerializer
|
get "/home", :serializer => HomeSerializer
|
||||||
|
|
@ -75,29 +74,32 @@ get "/home", :serializer => HomeSerializer
|
||||||
```
|
```
|
||||||
```ruby
|
```ruby
|
||||||
get "/home", :serializer => "home"
|
get "/home", :serializer => "home"
|
||||||
|
...
|
||||||
```
|
```
|
||||||
```ruby
|
```ruby
|
||||||
get "/home", :serializer => :home
|
get "/home", :serializer => :home
|
||||||
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
# user.rb
|
class User < ActiveRecord::Base
|
||||||
class User
|
attr_accessor :first_name, :last_name, :password, :email
|
||||||
include ActiveModel::SerializerSupport
|
|
||||||
|
|
||||||
attributes :first_name, :last_name, :email
|
|
||||||
end
|
end
|
||||||
```
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
# user_serializer.rb
|
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
attributes :first_name, :last_name
|
attributes :first_name, :last_name
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class API < Grape::API
|
||||||
|
get("/home") do
|
||||||
|
User.new({first_name: 'JR', last_name: 'HE', email: 'contact@jrhe.co.uk'})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
API.new.get "/home" # => '{:user=>{:first_name=>"JR", :last_name=>"HE"}}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ describe Grape::ActiveModelSerializers do
|
||||||
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
last_response.body.should == "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
it "should respond with proper content-type" do
|
it "should respond with proper content-type" do
|
||||||
subject.get("/home", :serializer => "user") do
|
subject.get("/home", :serializer => "user") do
|
||||||
{user: {first_name: "JR", last_name: "HE"}}
|
{user: {first_name: "JR", last_name: "HE"}}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue