0.0
No release in over a year
Use roar representers with respond_with in a way that you can easily use multiple representers for the same model and for the same controller but without being mandatory. Support ActiveRecord, Mongoid, and any Ruby object that responds to `to_model` and `model_name`.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 1.15.0
>= 5.2, < 7.1
~> 1.1.0
 Project Readme

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.

  1. At the controller action (instance level). Define it like respond_with entity, representer_class: EntityRepresenter.
  2. At the controller class level. Define it like self.represents = { entity: EntityRepresenter, collection: CollectionRepresenter }.
  3. 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 of model_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