Move SerializableResource to ActiveModelSerializers namespace

Ref. https://github.com/rails-api/active_model_serializers/pull/1310
This commit is contained in:
Yohan Robert
2016-03-19 06:13:32 +01:00
parent 874b8cab30
commit 21cb896802
28 changed files with 179 additions and 162 deletions

View File

@@ -14,7 +14,7 @@ post = Post.create(title: "Sample post", body: "I love Active Model Serializers!
options = {}
# Create a serializable resource instance
serializable_resource = ActiveModel::SerializableResource.new(post, options)
serializable_resource = ActiveModelSerializers::SerializableResource.new(post, options)
# Convert your resource into json
model_json = serializable_resource.as_json
@@ -38,20 +38,20 @@ serializer = ActiveModel::Serializer.serializer_for(post, options)
You could also retrieve the serializer via:
```ruby
ActiveModel::SerializableResource.new(post, options).serializer
ActiveModelSerializers::SerializableResource.new(post, options).serializer
```
Both approaches will return an instance, if any, of the resource's serializer.
## Serializing before controller render
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModel::SerializableResource` with
At times, you might want to use a serializer without rendering it to the view. For those cases, you can create an instance of `ActiveModelSerializers::SerializableResource` with
the resource you want to be serialized and call `.as_json`.
```ruby
def create
message = current_user.messages.create!(message_params)
message_json = ActiveModel::SerializableResource.new(message).as_json
message_json = ActiveModelSerializers::SerializableResource.new(message).as_json
MessageCreationWorker.perform(message_json)
head 204
end