This repository has been archived by the owner on Aug 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Routes are now defined by using bento_for :resource_name
- Loading branch information
Nicklas Ramhöj
committed
Nov 12, 2010
1 parent
c0243dd
commit 300c03b
Showing
4 changed files
with
114 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,81 @@ | ||
= Bento | ||
Bento is a Rails account management engine. | ||
|
||
== Disclaimer | ||
Don't expect everything to work exactly as described below. The gem is still in early beta. | ||
|
||
== Installation | ||
gem install bundler | ||
add gem 'bento' to your Gemfile | ||
bundle install | ||
rails generate bento:install | ||
rails generate bento account | ||
rake db:migrate | ||
Install the gem with bundler: | ||
gem install bundler | ||
add gem 'bento' to your Gemfile | ||
bundle install | ||
|
||
== Model | ||
If you run: | ||
rails generate bento account | ||
your Account model will call bento_account which will load account functionality into our model. | ||
If your model isn't named Account you need to override the model bento uses in the generated initializer. | ||
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. | ||
|
||
== 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 | ||
Bento sets up the following routes that you can use, complete with views and controllers: | ||
* [GET] sign_up_accounts - account and user creation in once | ||
* [GET] new_accounts - for just account creation | ||
* [GET] accounts - show | ||
* [GET] account - index | ||
* [POST] accounts - create | ||
* [PUT] account - update | ||
* [DELETE] account - destroy | ||
=== 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`). | ||
|
||
== Views and controllers | ||
=== Access the current account: | ||
There is a current_<account model name> (i.e. current_account) helper in your controllers and views which returns the currently logged in user's account. | ||
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 | ||
|
||
=== Automatic scoping | ||
=== Controllers | ||
You can tell your controllers to scope your resource to the current account. | ||
class MyController < ApplicationController | ||
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. | ||
|
||
=== Admin managed account | ||
Bento can give you a complete CRUD for creating accounts. To customize the views use | ||
=== Views | ||
Bento gives you a complete CRUD for managing accounts. To customize the views use: | ||
rails generate bento:views | ||
If your current_user respond to admin? These actions will only be accessible by users that respond with true. | ||
|
||
== Configuring controllers | ||
|
||
You can customize the accounts controller by following these steps: | ||
|
||
1) Create your custom accounts controller: | ||
class AccountsController < Bento::AccountsController | ||
end | ||
|
||
2) Tell the router to use this controller: | ||
resources :accounts | ||
|
||
3) And since we changed the controller, it won't use the gem's views so make sure to make some and put in app/views/accounts. | ||
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 CustomAccountsController < Bento::AccountsController | ||
class AccountsController < Bento::AccountsController | ||
protected | ||
def after_create_url | ||
custom_account_path(resource) | ||
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: | ||
* Remove the requirement to generate view files when overriding the accounts controller. | ||
* Make is possible to have another user model then User. | ||
* Complement the acceptance tests with controller specs. | ||
* Make it possible for a user to belong to more then one account. | ||
* Set up routes trough bento_for | ||
* Make it possible for a user to belong to more then one account. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
module ActionDispatch::Routing | ||
class Mapper | ||
def bento_for(*resources) | ||
resources.map!(&:to_sym) | ||
resources.each do |resource| | ||
Bento::Controllers::Helpers.define_helpers(resource) | ||
def bento_for(*resource_names) | ||
resource_names.map!(&:to_sym) | ||
resource_names.each do |resource_name| | ||
Bento::Controllers::Helpers.define_helpers(resource_name) | ||
|
||
resources(resource_name, :controller => account_controller(resource_name)) do | ||
collection { get :sign_up } | ||
end | ||
end | ||
end | ||
|
||
protected | ||
|
||
def account_controller(resource_name) | ||
if resource_name == :accounts and not overridden_accounts_contoller? then "bento/accounts" else nil end | ||
end | ||
|
||
def overridden_accounts_contoller? | ||
File.exist?(Rails.root.join("app", "controllers", "accounts_controller.rb")) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,55 @@ | ||
require 'spec_helper' | ||
|
||
class ApplicationController < ActionController::Base; end | ||
class AccountsController < ApplicationController | ||
def show | ||
render :text => "Show" | ||
class SitesController < ActionController::Base | ||
def show; render(:text => "s"); end | ||
def index; render(:text => "i"); end | ||
def new; render(:text => "n"); end | ||
def edit; render(:text => "e"); end | ||
def create; redirect_to sites_url; end | ||
def update; redirect_to sites_url; end | ||
def destroy; redirect_to sites_url; end | ||
end | ||
|
||
context "with the default bento controller" do | ||
describe Bento::AccountsController do | ||
before { controller.stubs(:authenticate_user!) } | ||
let(:account) { Account.make } | ||
|
||
describe "#bento_for" do | ||
it "defines resource routes" do | ||
with_routing do |map| | ||
map.draw { bento_for :accounts } | ||
|
||
get(:show, :id => account.id); response.should be_success | ||
get(:index); response.should be_success | ||
get(:sign_up); response.should be_success | ||
get(:new, :id => account.id); response.should be_success | ||
get(:edit, :id => account.id); response.should be_success | ||
post(:create); response.should be_redirect | ||
put(:update, :id => account.id); response.should be_redirect | ||
put(:destroy, :id => account.id); response.should be_redirect | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe AccountsController do | ||
describe "#bento_for" do | ||
it "defines resource routes" do | ||
with_routing do |map| | ||
map.draw { resources :accounts } | ||
context "with a custonly defined controller" do | ||
describe SitesController do | ||
describe "#bento_for" do | ||
it "defines resource routes" do | ||
with_routing do |map| | ||
map.draw { bento_for :sites } | ||
|
||
get :show, :id => 1 | ||
response.should be_success | ||
get(:show, :id => 1); response.should be_success | ||
get(:index); response.should be_success | ||
get(:new, :id => 1); response.should be_success | ||
get(:edit, :id => 1); response.should be_success | ||
post(:create); response.should be_redirect | ||
put(:update, :id => 1); response.should be_redirect | ||
put(:destroy, :id => 1); response.should be_redirect | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |