https://github.com/oestrich/nagare
A simple serializer for your Rails API.
https://github.com/oestrich/nagare
rails-api ruby serializer
Last synced: about 2 months ago
JSON representation
A simple serializer for your Rails API.
- Host: GitHub
- URL: https://github.com/oestrich/nagare
- Owner: oestrich
- License: mit
- Created: 2016-02-25T02:03:45.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-07-20T14:45:12.000Z (almost 8 years ago)
- Last Synced: 2024-10-31T14:47:07.528Z (8 months ago)
- Topics: rails-api, ruby, serializer
- Language: Ruby
- Homepage:
- Size: 14.6 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nagare 流れ
[](https://travis-ci.org/oestrich/nagare)
Nagare is a simple serializer for your Rails API.
## Installation
```ruby
gem 'nagare', github: 'oestrich/nagare'
```## Usage
### Create your context
Create a specialized context for your application. Anything you place in here will be available inside of the serializers.
```ruby
class ApplicationController < ActionController::Base
private
def nagare_context
@nagare_context ||= Nagare::Context.new({
current_user: current_user,
})
end
end
```### Create your serializers
There are item serializers and collection serializers.
```ruby
class OrderSerializer < Nagare::Item
attributes :id, :user_id
end
```You can access the object being serialized by the `object` method.
```ruby
class OrdersSerializer < Nagare::Collection
attributes :count, :href
def count
collection.count
end
end
```You can access the collection being serialized by the `collection` method.
### Use your serializers
You can extend the context per route very easily by specifying a `context` key with a new hash. Hash keys will be available as methods inside of the serializer.
```ruby
class OrdersController < ApplicationController
def index
render({
json: orders,
serializers: { collection: OrdersSerializer, item: OrderSerializer },
context: { href: orders_url },
})
end
def show
render({
json: order,
serializer: { item: OrderSerializer },
context: { href: order_url(order) },
})
end
end
```