CollectionJson Rails
This gem will add Rails specific features to CollectionJson Serializer.
Features
Parsing of incoming templates:
It integrates with strong parameters. accept_template! accepts one argument:
the name of the model. That is what you would usually pass at the require()
method of strong parameters. This will parse the incoming template as params,
and return the result of params.require(:model). Then, you can just proceed as
usual.
class PostsController < ApplicationController
include CollectionJson::Rails::AcceptTemplate
private
def post_params
accept_template!(:post).permit(:title)
end
One step serialization:
You can respond with Collection+JSON witho no effort, using render.
class PostSerializer < CollectionJson::Serializer
items do
attributes :title, :body
end
end
class PostsController < ApplicationController
include CollectionJson::Rails::Render
def index
@posts = Post.all
render json: @posts, status: :ok
end
end
TO-DO Features
Generators:
$ rails g cj:serializer post
# => app/serializers/post_serializer.rb
Wonder: should be invocated by scaffold, models, etc. generators?
URLs helpers
No more fighting with routes generation:
class PostSerializer < CollectionJson::Serializer
href :posts_url
items do
href :post_url, :self
Installation
Add this line to your application's Gemfile:
gem 'collection_json_rails'
And then execute:
$ bundle
Or install it yourself as:
$ gem install collection_json_rails
Contributing
- Fork it ( https://github.com/[my-github-username]/collection_json_rails/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request