https://github.com/foca/granola-rails
Rails wrapper for Granola
https://github.com/foca/granola-rails
granola json-serialization lesscode rails ruby
Last synced: 8 months ago
JSON representation
Rails wrapper for Granola
- Host: GitHub
- URL: https://github.com/foca/granola-rails
- Owner: foca
- License: mit
- Created: 2016-11-14T17:23:37.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-10T18:57:49.000Z (over 7 years ago)
- Last Synced: 2025-06-25T21:45:42.481Z (9 months ago)
- Topics: granola, json-serialization, lesscode, rails, ruby
- Language: Ruby
- Homepage: https://github.com/foca/granola
- Size: 24.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Granola::Rails [](https://travis-ci.org/foca/granola-rails) [](https://rubygems.org/gems/granola-rails)
Integrate [Granola](https://github.com/foca/granola) into Rails' rendering.
## Usage
Once you install it, you can just use `render` to use Granola serializers:
``` ruby
class UsersController < ApplicationController
def show
user = User.find(params[:id])
render json: user
end
end
```
This would infer a `UserSerializer` (in `app/serializers/user_serializer.rb`).
You can pass any options to this method that you could pass to
[`Granola::Rack#granola`][granola-rack]. For example, to use a different
serializer:
``` ruby
class UsersController < ApplicationController
def show
user = User.find(params[:id])
render json: user, with: DetailedUserSerializer
end
end
```
[granola-rack]: https://github.com/foca/granola/blob/master/lib/granola/rack.rb
### Serialization formats
Any serialization format you add to Granola via `Granola.render` will be
available to render through rails. For example:
``` ruby
# config/initializers/granola.rb
Granola.render :yaml, via: YAML.method(:dump), content_type: Mime[:yaml].to_s
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def show
user = User.find(params[:id])
respond_to do |format|
format.json { render json: user }
format.yaml { render yaml: user }
end
end
end
```
## Rails Generators
This library provides a `serializer` generator:
``` bash
$ rails generate serializer user
create app/serializers/user_serializer.rb
invoke test_unit
create test/serializers/user_serializer_test.rb
```
## Installation
Add this line to your application's Gemfile:
``` ruby
gem 'granola-rails'
```
And then execute:
``` sh
$ bundle
```
Or install it yourself as:
``` sh
$ gem install granola-rails
```
## License
The gem is available as open source under the terms of the MIT License. See the
[LICENSE](./LICENSE) for detjsonails.