https://github.com/outlawandy/attr_serializer
easy JSON serialization for ActiveRecord models
https://github.com/outlawandy/attr_serializer
activerecord activerecord-models json json-serialization rails rails-api serialization
Last synced: about 2 months ago
JSON representation
easy JSON serialization for ActiveRecord models
- Host: GitHub
- URL: https://github.com/outlawandy/attr_serializer
- Owner: OutlawAndy
- License: mit
- Created: 2017-05-30T17:15:34.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-09-25T20:45:43.000Z (almost 4 years ago)
- Last Synced: 2026-04-03T15:56:36.522Z (3 months ago)
- Topics: activerecord, activerecord-models, json, json-serialization, rails, rails-api, serialization
- Language: Ruby
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AttrSerializer [](https://badge.fury.io/rb/attr_serializer)
easy JSON serialization for ActiveRecord models
## Installation
Add this line to your application's Gemfile
```ruby
gem 'attr_serializer'
```
Or install it yourself
$ gem install attr_serializer
## Usage
in your ActiveRecord models
```ruby
class User < ActiveRecord::Base
attr_serializer :id, :name
end
```
Now, whenever `to_json` is called on an instance or collection of `User` objects, only `id` & `name` will be serialized
```ruby
class UsersController < ApplicationController
def show
render json: User.find(123)
end
end
```
```json
{"id": 123, "name": "Bob Jones"}
```
Any attribute or method name can be supplied to `attr_serializer`
```ruby
class User < ActiveRecord::Base
attr_serializer :id, :name, :some_data
def some_data
"not in database"
end
end
```
```json
{"id": 123, "name": "Bob Jones", "some_data": "not in database"}
```
You may also specify alternate keys for the resulting `json` by passing a hash as the last argument to `attr_serializer`
```ruby
class User < ActiveRecord::Base
attr_serializer :id, :name, someData: :some_data
def some_data
"not in database"
end
end
```
```json
{"id": 123, "name": "Bob Jones", "someData": "not in database"}
```
`attr_serializer` is also inherited accross STI models
```ruby
class Admin < User
attr_serializer :admin_stuff
def admin_stuff
"super important"
end
end
Admin.first.to_json
#=> "{\"id\": 124, \"name\": \"Admin Bob\", \"someData\": \"not in database\", \"admin_stuff\": \"super important\"}"
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/OutlawAndy/attr_serializer.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).