mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Change to 1.9 Hash syntax in docs
This commit is contained in:
parent
88ff42ebc8
commit
54ce37b956
@ -358,8 +358,8 @@ Here is an example:
|
|||||||
|
|
||||||
<pre lang="ruby">
|
<pre lang="ruby">
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
has_many :followed_posts, :key => :posts
|
has_many :followed_posts, key: :posts
|
||||||
has_one :owned_account, :key => :account
|
has_one :owned_account, key: :account
|
||||||
end
|
end
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
@ -370,8 +370,8 @@ to set it explicitly:
|
|||||||
|
|
||||||
<pre lang="ruby">
|
<pre lang="ruby">
|
||||||
class UserSerializer < ActiveModel::Serializer
|
class UserSerializer < ActiveModel::Serializer
|
||||||
has_many :followed_posts, :key => :posts, :serializer => CustomPostSerializer
|
has_many :followed_posts, key: :posts, serializer: CustomPostSerializer
|
||||||
has_one :owne_account, :key => :account, :serializer => PrivateAccountSerializer
|
has_one :owne_account, key: :account, serializer: PrivateAccountSerializer
|
||||||
end
|
end
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
|
|||||||
34
README.md
34
README.md
@ -217,7 +217,7 @@ end
|
|||||||
## Getting the old version
|
## Getting the old version
|
||||||
|
|
||||||
If you find that your project is already relying on the old rails to_json
|
If you find that your project is already relying on the old rails to_json
|
||||||
change `render :json` to `render :json => @your_object.to_json`.
|
change `render :json` to `render json: @your_object.to_json`.
|
||||||
|
|
||||||
# Attributes and Associations
|
# Attributes and Associations
|
||||||
|
|
||||||
@ -293,7 +293,7 @@ type of a computed attribute:
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PersonSerializer < ActiveModel::Serializer
|
class PersonSerializer < ActiveModel::Serializer
|
||||||
attributes :first_name, :last_name, {:full_name => :string}
|
attributes :first_name, :last_name, {full_name: :string}
|
||||||
|
|
||||||
def full_name
|
def full_name
|
||||||
"#{object.first_name} #{object.last_name}"
|
"#{object.first_name} #{object.last_name}"
|
||||||
@ -309,7 +309,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :id, :body
|
attributes :id, :body
|
||||||
|
|
||||||
# look up :subject on the model, but use +title+ in the JSON
|
# look up :subject on the model, but use +title+ in the JSON
|
||||||
attribute :subject, :key => :title
|
attribute :subject, key: :title
|
||||||
has_many :comments
|
has_many :comments
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -318,7 +318,7 @@ If you would like to add meta information to the outputted JSON, use the `:meta`
|
|||||||
option:
|
option:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}
|
render json: @posts, serializer: CustomArraySerializer, meta: {total: 10}
|
||||||
```
|
```
|
||||||
|
|
||||||
The above usage of `:meta` will produce the following:
|
The above usage of `:meta` will produce the following:
|
||||||
@ -336,7 +336,7 @@ The above usage of `:meta` will produce the following:
|
|||||||
If you would like to change the meta key name you can use the `:meta_key` option:
|
If you would like to change the meta key name you can use the `:meta_key` option:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
render :json => @posts, :serializer => CustomArraySerializer, :meta => {:total => 10}, :meta_key => 'meta_object'
|
render json: @posts, serializer: CustomArraySerializer, meta: {total: 10}, meta_key: 'meta_object'
|
||||||
```
|
```
|
||||||
|
|
||||||
The above usage of `:meta_key` will produce the following:
|
The above usage of `:meta_key` will produce the following:
|
||||||
@ -388,7 +388,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
|
|
||||||
# only let the user see comments he created.
|
# only let the user see comments he created.
|
||||||
def comments
|
def comments
|
||||||
object.comments.where(:created_by => current_user)
|
object.comments.where(created_by: current_user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
@ -401,7 +401,7 @@ class PostSerializer < ActiveModel::Serializer
|
|||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
|
|
||||||
# look up comments, but use +my_comments+ as the key in JSON
|
# look up comments, but use +my_comments+ as the key in JSON
|
||||||
has_many :comments, :key => :my_comments
|
has_many :comments, key: :my_comments
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -439,8 +439,8 @@ end
|
|||||||
You may also use the `:serializer` option to specify a custom serializer class and the `:polymorphic` option to specify an association that is polymorphic (STI), e.g.:
|
You may also use the `:serializer` option to specify a custom serializer class and the `:polymorphic` option to specify an association that is polymorphic (STI), e.g.:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
has_many :comments, :serializer => CommentShortSerializer
|
has_many :comments, serializer: CommentShortSerializer
|
||||||
has_one :reviewer, :polymorphic => true
|
has_one :reviewer, polymorphic: true
|
||||||
```
|
```
|
||||||
|
|
||||||
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
Serializers are only concerned with multiplicity, and not ownership. `belongs_to` ActiveRecord associations can be included using `has_one` in your serializer.
|
||||||
@ -528,7 +528,7 @@ You can specify that the data be included like this:
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
embed :ids, :include => true
|
embed :ids, include: true
|
||||||
|
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
has_many :comments
|
has_many :comments
|
||||||
@ -563,10 +563,10 @@ used to reference them:
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
embed :ids, :include => true
|
embed :ids, include: true
|
||||||
|
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
has_many :comments, :key => :comment_ids, :root => :comment_objects
|
has_many :comments, key: :comment_ids, root: :comment_objects
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -591,10 +591,10 @@ objects:
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PostSerializer < ActiveModel::Serializer
|
class PostSerializer < ActiveModel::Serializer
|
||||||
embed :ids, :include => true
|
embed :ids, include: true
|
||||||
|
|
||||||
attributes :id, :title, :body
|
attributes :id, :title, :body
|
||||||
has_many :comments, :embed_key => :external_id
|
has_many :comments, embed_key: :external_id
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -646,7 +646,7 @@ To be clear, it's not possible, yet, to do something like this:
|
|||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class SomeController < ApplicationController
|
class SomeController < ApplicationController
|
||||||
serialization_scope :current_admin, :except => [:index, :show]
|
serialization_scope :current_admin, except: [:index, :show]
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -660,13 +660,13 @@ class CitiesController < ApplicationController
|
|||||||
def index
|
def index
|
||||||
@cities = City.all
|
@cities = City.all
|
||||||
|
|
||||||
render :json => @cities, :each_serializer => CitySerializer
|
render json: @cities, each_serializer: CitySerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@city = City.find(params[:id])
|
@city = City.find(params[:id])
|
||||||
|
|
||||||
render :json => @city, :scope => current_admin, :scope_name => :current_admin
|
render json: @city, scope: current_admin, scope_name: :current_admin
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user