README.md in shreddies-0.2.0 vs README.md in shreddies-0.3.0
- old
+ new
@@ -63,10 +63,12 @@
```ruby
User.all.as_json
```
+### Collection and Single Modules
+
You may find that you don't want or need to return as much data in collections of objects, or may want to include differtent data. So if a serializer defines a `Collection` module, and a collection or array is being rendered, then that Collection module will automatically be included:
```ruby
ArticleSerializer < Shreddies::Json
module Collection
@@ -87,10 +89,14 @@
end
end
end
```
+### `before_render` callback
+
+You can define a `#before_render` private method in your serializers, which will act as a callback. It receives the object to be output, and expects you to return the object, which allows you to modify it before rendering.
+
### Options
Both `#as_json` and `.render` accepts an `options` hash, which will be forwarded to the serializer class, and available as `options`. This allows you to pass arbitrary options and use them in your serializer.
The following standard options are supported, and provide additional built-in functionality:
@@ -120,9 +126,13 @@
end
end
```
The `Collection` and `Single` modules can be defined and they will be automatically included. The Collection module will be included when rendering an array or ActiveRecord collection (`ActiveRecord::Relation`), and the Single module will be included when rendering a single obejct.
+
+#### `transform_keys` (default: true)
+
+If false, the returned keys will not be transformed. The default is to deeply transform all keys to camelCase.
#### `index_by`
Give this option a property of your serialized subject as a Symbol, and the returned collection will be a Hash keyed by that property.