RoarRepresenter
📗 Use roar
representers with respond_with
in a way that you can use easily multiple representers for the same model and for the same controller but without being mandatory.
It contains the bare minimum to easily render roar representers as responses in Rails. It is a much simpler gem than roar-rails
. But it also supports MongoID.
I'm ensuring that it renders well for JSON but I have not checked it for XML.
Another limitation is that I've only made it work for class representers and not for module representers.
⚠️ The main branch is used for development. It is not a stable branch for usage. Please, use a release instead. Preferably, the latest.
Installation
Add this line to your application's Gemfile:
gem 'roar_responder'
And then execute:
bundle install
Dependencies
This gem requires to be used inside Rails alongside the gems roar and responders.
Usage
Include the code in the controller(s)
Include the concern (module) in the controller(s) you want with this line:
include ::RoarResponder::ControllerRespondable
If you want to use this gem inside a Rails::Engine
, require the gem by adding the following line in lib/{YOUR_ENGINE}/engine.rb
.
require 'roar_responder'
Respond in the controller(s)
There are 3 possible ways to render the representer. They are the following, by order of what takes first in case that multiple are defined.
- At the controller action (instance level). Define it like
respond_with entity, representer_class: EntityRepresenter
. - At the controller class level. Define it like
self.represents = { entity: EntityRepresenter, collection: CollectionRepresenter }
. - Inferred. This means that if you do not specify a representer in any of the 2 previous ways, it uses the representer inside
app/representers
with the file name ofmodel_name
with_representer.rb
. If this file does not exist, it follows the default serialization of the entity/object itself.
You can render any object that responds to to_model
and model_name
. This works for any entity and collection of ActiveRecord, Mongoid, and other bare ruby objects that use ActiveModel features.
Development/Test
I usually test like this:
docker-compose up -d
cd test/dummy/ && ./bin/setup && cd ../..
bundle exec rake test