Skip to content

Commit d2917fd

Browse files
committed
Merge pull request #1673 from DrSayre/adding_poro_howto
Adding poro howto
2 parents a182618 + 05618ea commit d2917fd

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Fixes:
6464
- [#1488](https://github.com/rails-api/active_model_serializers/pull/1488) Require ActiveSupport's string inflections (@nate00)
6565

6666
Misc:
67+
- [#1673](https://github.com/rails-api/active_model_serializers/pull/1673) Adds "How to" guide on using AMS with POROs (@DrSayre)
6768
- [#1608](https://github.com/rails-api/active_model_serializers/pull/1608) Move SerializableResource to ActiveModelSerializers (@groyoh)
6869
- [#1602](https://github.com/rails-api/active_model_serializers/pull/1602) Add output examples to Adapters docs (@remear)
6970
- [#1557](https://github.com/rails-api/active_model_serializers/pull/1557) Update docs regarding overriding the root key (@Jwan622)

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This is the documentation of ActiveModelSerializers, it's focused on the **0.10.
2727
- [Using ActiveModelSerializers Outside Of Controllers](howto/outside_controller_use.md)
2828
- [Testing ActiveModelSerializers](howto/test.md)
2929
- [Passing Arbitrary Options](howto/passing_arbitrary_options.md)
30+
- [How to serialize a Plain-Old Ruby Object (PORO)](howto/serialize_poro.md)
3031

3132
## Integrations
3233

docs/howto/serialize_poro.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[Back to Guides](../README.md)
2+
3+
# How to serialize a Plain-Old Ruby Object (PORO)
4+
5+
When you are first getting started with ActiveModelSerializers, it may seem only `ActiveRecord::Base` objects can be serializable, but pretty much any object can be serializable with ActiveModelSerializers. Here is an example of a PORO that is serializable:
6+
```ruby
7+
# my_model.rb
8+
class MyModel
9+
alias :read_attribute_for_serialization :send
10+
attr_accessor :id, :name, :level
11+
12+
def initialize(attributes)
13+
@id = attributes[:id]
14+
@name = attributes[:name]
15+
@level = attributes[:level]
16+
end
17+
18+
def self.model_name
19+
@_model_name ||= ActiveModel::Name.new(self)
20+
end
21+
end
22+
```
23+
24+
Fortunately, ActiveModelSerializers provides a [`ActiveModelSerializers::Model`](https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model_serializers/model.rb) which you can use in production code that will make your PORO a lot cleaner. The above code now becomes:
25+
```ruby
26+
# my_model.rb
27+
class MyModel < ActiveModelSerializers::Model
28+
attr_accessor :id, :name, :level
29+
end
30+
```
31+
32+
The default serializer would be `MyModelSerializer`.

0 commit comments

Comments
 (0)