Simple gem to dry up your controllers and provide a uniform interface for serializing Ruby objects. It relys on you to select and install either the blueprinter or jsonapi-serializer gems as well as this one.
Add this line to your application's Gemfile:
gem 'dry_serialization'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install dry_serialization
-
Install with rails generator
-
currently supported serializers are jsonapi_serializer and blueprinter
rails g dry_serialization:<serializer_name>:install
-
-
This generator will assume you have an api controller, and create one if you do not. This is to further keep the api separate from the web controllers.
- The inclusion of the module corresponding to your chosen serializer gem will give you access to
#serialized_resource
.- The
#serialized_resource
method also takes an optional 'options' hash. Forjsonapi-serializer
, that can come in the form of theinclude
hash and/ormeta
or any of the other options available through that gem. Please see their documentation for all options. - Similarly for
blueprinter
it will also take an optional options hash. Again, please see the official documentation in their repo.
- The
- The inclusion of the module corresponding to your chosen serializer gem will give you access to
-
Conversly, if you choose to not use the install generator, you can simply add your desired serializer gem to the Gemfile and include the relevant modules where needed.
user = User.find(params[:id]) render json: serialized_resource(user, UserSerializer) # UserSerializer can be substituted with UserBlueprint if that is the gem you've chosen.
-
SerializationHelper
- This module provides success and error response helper methods.
- In your controller you can use them like so:
def create user = User.create(create_user_params) return error_response(user.errors) unless user.errors.empty? success_response(user, UserSerializer, :created) end
- Both methods take an optional argument for the status. You can either use the symbol representation or the number code.
After checking out the repo, run bin/setup
to install dependencies. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/mikeyduece/dry_serialization.
The gem is available as open source under the terms of the MIT License.