mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Update README.markdown
This commit is contained in:
parent
e122675373
commit
0123e80dd3
@ -39,7 +39,7 @@ $ rails g resource post title:string body:string
|
|||||||
|
|
||||||
This will generate a serializer in `app/serializers/post_serializer.rb` for
|
This will generate a serializer in `app/serializers/post_serializer.rb` for
|
||||||
your new model. You can also generate a serializer for an existing model with
|
your new model. You can also generate a serializer for an existing model with
|
||||||
the `serializer generator`:
|
the serializer generator:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ rails g serializer post
|
$ rails g serializer post
|
||||||
@ -66,7 +66,7 @@ end
|
|||||||
In this case, Rails will look for a serializer named `PostSerializer`, and if
|
In this case, Rails will look for a serializer named `PostSerializer`, and if
|
||||||
it exists, use it to serialize the `Post`.
|
it exists, use it to serialize the `Post`.
|
||||||
|
|
||||||
This also works with `render_with`, which uses `to_json` under the hood. Also
|
This also works with `respond_with`, which uses `to_json` under the hood. Also
|
||||||
note that any options passed to `render :json` will be passed to your
|
note that any options passed to `render :json` will be passed to your
|
||||||
serializer and available as `@options` inside.
|
serializer and available as `@options` inside.
|
||||||
|
|
||||||
@ -110,31 +110,35 @@ end
|
|||||||
## Custom Attributes
|
## Custom Attributes
|
||||||
|
|
||||||
If you would like customize your JSON to include things beyond the simple
|
If you would like customize your JSON to include things beyond the simple
|
||||||
attributes of the model, you can override its `serializable_hash` method
|
attributes of the model, you can override its `attributes` method
|
||||||
to return anything you need. For example:
|
to return anything you need.
|
||||||
|
|
||||||
|
The most common scenario to use this feature is when an attribute
|
||||||
|
depends on a serialization scope. By default, the current user of your
|
||||||
|
application will be available in your serializer under the method
|
||||||
|
`scope`. This allows you to check for permissions before adding
|
||||||
|
an attribute. For example:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class Person < ActiveRecord::Base
|
class Person < ActiveRecord::Base
|
||||||
|
|
||||||
def full_name
|
def full_name
|
||||||
"#{first_name} #{last_name}"
|
"#{first_name} #{last_name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash options = nil
|
def attributes
|
||||||
hash = super(options)
|
hash = super(options)
|
||||||
hash["full_name"] = full_name
|
hash["full_name"] = full_name if scope.admin?
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
The attributes returned by `serializable_hash` are then available in your serializer
|
The serialization scope can be customized in your controller by
|
||||||
as usual:
|
calling `serialization_scope`:
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
class PersonSerializer < ActiveModel::Serializer
|
class ApplicationController < ActionController::Base
|
||||||
attributes :first_name, :last_name, :full_name
|
serialization_scope :current_admin
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user