Mongosteen
An easy way to add restful actions
Mongosteen is a library that helps to easily add restful actions to mongoid models and adds support of search, pagination, scopes, json config and history.
Mongosteen is based on inherited_resources gem, get yourself familiar on how it works and setup using it's documentation.
Installation
- Add Mongosteen to your Gemfile:
gem 'mongosteen'
- Create controller for the model (e.g.
Post
):
class PostsController < ApplicationController
mongosteen
end
- Connect new controller in
routes.rb
:
resources :posts
Mongosteen API
Index
Index supports search, page, perPage and scope parameters:
/posts.json?search=Mongosteen&perPage=10&page=2&scope=gems
Check out external gems documentation to see what options they provide:
- mongoid_search — search for mongoid models;
- kaminari — generic Rails pagination solution;
- has_scope — easy way to access mongoid model scopes via controller;
Versions
Get specific resource version:
/posts/54f4216f4f6c65e414000000.json?version=2
No how to add history support for mongoid model, check out:
- mongoid-history — track changes to mongoid document;
JSON config for model
Some times there is a need to configure json output for the model, for example to add model method to output or to exclude some internal fields. Mongosteen provides an easy and isolated way to do that in models controller using json_config
method:
class PostsController < ApplicationController
mongosteen
json_config({ methods: [ :published_at ] })
end
json_config
accepts configuration hash and passes it to as_json method when render output.
Sorted Relations
In Mongoid, the HABTM relations return docs in the wrong order. This workaround gives your document the ability to retrieve it's relations in the same order it was placed in. This is refactored version of an original mongoid-sorted-relations gem.
For more details about this issue, see #1548.
Usage example:
class Post
include Mongoid::Document
include Mongoid::SortedRelations
has_and_belongs_to_many :authors
end
post = Book.new title: 'Restful actions with Mongosteen'
post.authors << Author.create name: "Alexander Kravets"
post.authors << Author.create name: "Roman Brazhnyk"
post.authors << Author.create name: "Maxim Melnyk"
post.sorted_authors.map(&:name)
#=> ['Alexander Kravets', 'Roman Brazhnyk', 'Maxim Melnyk']
Check out orignal gem documentation for more usage examples.
Permitted Parameters
For easiness of prototyping, Mongosteen has a workaround that allows all input parameters for create
and update
methods. This default behaviour can be overriden by using permitted_params
method inside of models controller, e.g.:
class Admin::PostsController < Admin::BaseController
mongosteen
protected
def permitted_params
params.permit(:post => [:title, :body])
end
end
Serializable Model Id
By default mongoid model serializes document id into hash, to override that add Mongoid::SerializedId
to model class:
class Post
include Mongoid::Document
include Mongoid::SortedRelations
include Mongoid::SerializedId
end
The Mongosteen family
- Character: A simple and lightweight javascript library for building data management web apps
Credits
Mongosteen is maintained and funded by Slate Studio, LLC. Tweet your questions or suggestions to @slatestudio and while you’re at it follow us too.
License
Copyright © 2015 Slate Studio, LLC. Mongosteen is free software, and may be redistributed under the terms specified in the license.