Mongosteen is a library that helps to easily add RESTful actions to mongoid models, providing support for search, pagination, scopes, json configuration and history.
Mongosteen is based on inherited_resources gem, get yourself familiar with how it works and what you can do with this great tool.
- 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
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;
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;
Sometimes there is a need to configure json output for the model, for example to add model method to output or exclude some internal fields. Mongosteen provides an easy and isolated way to do that in model controller using json_config
method:
class PostsController < ApplicationController
mongosteen
json_config methods: %w(published_at)
end
json_config
accepts configuration hash and passes it to as_json method while rendering document json.
If you need to define configuration specifically for methods, e.g. index might not need all document fields to make requests lighter, there is an actions
special key for that:
class PostsController < ApplicationController
mongosteen
json_config methods: %w(published_at),
actions: {
index: { methods: %w(published_at), exclude: %w(body_html) }
}
end
There are four default actions you can use here: index
, show
, create
, update
. Also you can specify your custom methods defined in controller and connected via routes.
For rapid prototyping, Mongosteen allows all input parameters for create
and update
methods. This default behaviour can be overriden by using permitted_params
method in controller, e.g:
class Admin::PostsController < Admin::BaseController
mongosteen
private
def post_params
params.require(:post).permit(:title, :body)
end
end
- Character: Powerful responsive javascript CMS for apps
- Mongosteen: An easy way to add RESTful actions for Mongoid models
- Inverter: An easy way to connect Rails templates content to Character CMS
- Loft: Media assets manager for Character CMS
Copyright © 2015 Slate Studio, LLC. Mongoosteen is free software, and may be redistributed under the terms specified in the license.
Mongoosteen is maintained and funded by Slate Studio, LLC. Tweet your questions or suggestions to @slatestudio and while you’re at it follow us too.