mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Merge pull request #462 from fab/master
Generator now properly detects ApplicationSerializer
This commit is contained in:
commit
d985e6424c
27
README.md
27
README.md
@ -73,10 +73,6 @@ Currently `ActiveModel::Serializers` expects objects to implement
|
|||||||
read\_attribute\_for\_serialization. That's all you need to do to have
|
read\_attribute\_for\_serialization. That's all you need to do to have
|
||||||
your POROs supported.
|
your POROs supported.
|
||||||
|
|
||||||
# ActiveModel::Serializer
|
|
||||||
|
|
||||||
All new serializers descend from ActiveModel::Serializer
|
|
||||||
|
|
||||||
# render :json
|
# render :json
|
||||||
|
|
||||||
In your controllers, when you use `render :json`, Rails will now first search
|
In your controllers, when you use `render :json`, Rails will now first search
|
||||||
@ -726,6 +722,29 @@ end
|
|||||||
|
|
||||||
The caching interface uses `Rails.cache` under the hood.
|
The caching interface uses `Rails.cache` under the hood.
|
||||||
|
|
||||||
|
# ApplicationSerializer
|
||||||
|
|
||||||
|
By default, new serializers descend from ActiveModel::Serializer. However, if you wish to share behaviour across your serializers you can create an ApplicationSerializer at ```app/serializers/application_serializer.rb```:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class ApplicationSerializer < ActiveModel::Serializer
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Any newly generated serializers will automatically descend from ApplicationSerializer.
|
||||||
|
|
||||||
|
```
|
||||||
|
$ rails g serializer post
|
||||||
|
```
|
||||||
|
|
||||||
|
now generates:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class PostSerializer < ApplicationSerializer
|
||||||
|
attributes :id
|
||||||
|
end
|
||||||
|
````
|
||||||
|
|
||||||
# Design and Implementation Guidelines
|
# Design and Implementation Guidelines
|
||||||
|
|
||||||
## Keep it Simple
|
## Keep it Simple
|
||||||
|
|||||||
@ -26,7 +26,7 @@ module Rails
|
|||||||
if options[:parent]
|
if options[:parent]
|
||||||
options[:parent]
|
options[:parent]
|
||||||
elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) ||
|
elsif (ns = Rails::Generators.namespace) && ns.const_defined?(:ApplicationSerializer) ||
|
||||||
defined?(::ApplicationSerializer)
|
(Object.const_get(:ApplicationSerializer) rescue nil)
|
||||||
'ApplicationSerializer'
|
'ApplicationSerializer'
|
||||||
else
|
else
|
||||||
'ActiveModel::Serializer'
|
'ActiveModel::Serializer'
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user