Bento
Bento is a Rails account management engine.
Installation
Install the gem with bundler:
gem install bundler
add gem 'bento' to your Gemfile
bundle install
When you are done, you are ready to add Bento to any of your models using the generator:
rails generate bento MODEL
rake db:migrate
Replace MODEL by the class name you want to use as your account-like model, like Account, Site, etc. This will create a model (if one does not exist) and configure it with default Bento options. The generator will also create a migration file (if you’re running ActiveRecord) and configure your routes.
The migration files will by default set up the following database structure:
user has_many :accounts, :through => :bento_memberships
accounts has_many :users, :through => :bento_memberships
Getting started
Model
This is a walkthrough with all steps you need to setup a Bento resource, including model, migration and route files. Bento must be set up within the model (or models) you want to use. Bento routes must be created inside your config/routes.rb file. We’re assuming here you want a Account model with the default Bento options.
class Account < ActiveRecord::Base
bento
end
Bento doesn’t use attr_accessible or attr_protected inside its modules, so be sure to define attributes as accessible or protected in your model.
Routes
Configure your routes after setting up your model. Open your config/routes.rb file and add:
bento_for :accounts
This will use your Account model to create a set of needed routes (you can see them by running ‘rake routes`). You can nest other routes in bento_for just as you would with the standard resources routes method:
bento_for :accounts do
resources :projects
end
This will add bento_for :accounts to your routes file. If you want to add another model just rename or add the argument(s) to bento_for.
bento_for :accounts, :sites
Controllers
You can tell your controllers to scope your resource to the current account.
class AccountsController < Bento::AccountsController
scoped_to_account
end
If you’re defining your own controller you must generate views as described below. If your current_user respond to admin? All the actions will only be accessible by users that respond with true.
Views
Bento gives you a complete CRUD for managing accounts. To customize the views use:
rails generate bento:views
Helpers
Bento will create the following helpers to use inside your controllers and views:
current_account
Where account would be anything your model is named. The helper depends on a current_user-helper being defined (as is the case if you’re using Devise).
Override specific pieces
The default behavior is to redirect to the accounts show view after creation. This can be changed by overriding the after_create_url method:
class AccountsController < Bento::AccountsController
protected
def after_create_url
account_path(resource)
end
end
Navigating the source
The gem has a basic set of cucumber features along with a rails app. Check them out to see some examples on how to use Bento in your application.
Disclaimer
-
The gem is still in early beta, be careful.
-
Forces you to have a User-model.
-
Only works with Rails 3 and ActiveRecord.
Credits and contributors
-
The contributors of the awesome Devise gem from which I’ve borrowed a lot of ideas and patterns.
-
Elabs for sponsoring
-
Jonas Nicklas for helping me boot strap the engine.
-
All the gems that this gem depends on.
TODO:
-
Make it possible to create new users
-
Make it possible to completely remove users
-
Add inline documentation
-
Make is possible to have another user model then User.
License:
(The MIT License)
Copyright © 2011 Nicklas Ramhöj
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.