= Admin Dashboard and Authentication (padrino-admin)
=== Overview
Padrino has a beautiful Ajax Admin, with these fatures:
Orm Agnostic:: Adapters for datamapper, activerecord, mongomapper, couchdb (now only: datamapper and activerecord)
Authentication:: Support for Account authentication, Account Permission managment
Scaffold:: You can simply create a new "admin interface" simply providing a Model
Ajax Uploads:: You can upload file, manage them and attach them to any model in a quick and simple way (coming soon)
=== Admin Usage
Create a project:
$ padrino-gen app fun-test
$ cd fun-test
For create the admin application:
fun-test$ padrino-gen admin
Now follow admin instructions so:
* edit your config/database.rb
* run padrino rake dm:auto:migrate
* run padrino rake seed
Your admin now is "complete", you can start your server with padrino start and point your browser to /admin!
For create a new "scaffold" you need to provide only a Model for them like:
fun-test$ padrino-gen model post --skip-migration // edit your post.rb model and add some fields
fun-test$ padrino-gen rake dm:auto:migrate
fun-test$ padrino-gen admin_page Post
fun-test$ padrino start // and go to yourserver.local/admin
That's all!!
=== Admin Authentication
Padrino Admin use a model Account for manage role, membership and permissions take the following example:
access_control.roles_for :any do |role|
role.allow "/sessions"
# role.deny "/deny/this/always"
end
access_control.roles_for :admin do |role, account|
role.allow "/"
role.project_module :accounts do |project|
project.menu :list, "/admin/accounts.js"
project.menu :new, "/admin/accounts/new"
end
end
access_control.roles_for :editor do |role, account|
role.project_module :posts do |project|
project.menu :list, "/admin/posts.js"
project.menu :new, "/admin/posts/new"
end
role.project_module :comments do |project|
project.menu :list, "/admin/comments.js"
project.menu :new, "/admin/comments/new"
end
end
In this example we grant "/session" (and each subpaths like /sessions/new) for all users logged and unlogged.
Account with role admin can manage only accounts because have access to "/admin/accounts/**" paths
Account with role editor can manage only post/comments because have access to "/admin/posts/**", "/admin/posts/**" paths
Another good fature of Padrino admin is that when you define a Project Module role you also build the Menu Tree of the Admin.
Trust us that in future you appreciate so much this feature.
=== Admin Uploads
Padrino admin has a builtin upload managment that leave you to be free as possible.
fun-test$ padrino-gen admin_uploader
fun-test$ rake dm:auto:upgrade # or ar:migrate
Finish! Now you can browse into your admin and you can see a new menu called +upload+ where you can see all your uploads,
upload other file, delete ...
All upload definitions are defined in lib/uploader.rb, here you can preproces your attachments (like resize) or manage versions.
See http://github.com/jnicklas/carrierwave
==== Attach Many Uploads to a Model
If you want attach in a model ex: Account many uploads you can do that in a simple way add a habtm relation!
# app/models/account.rb
has n, :uploads
or for ActiveRecord
# app/models/account.rb
has_and_belongs_to_many :uploads
Now edit the form and add these lines:
# admin/views/accounts/_form.haml
%tr
%td=f.label :uploads
%td=f.open_window_grid :upload_ids, :with => :uploads, :get => :id, :show => :file, :multiple => true, :item => :panel
*open_window_grid* is a padrino-admin method that open an extjs window contains your +grids+ so in this case:
open window grid for :+account+ model in method :+upload_ids+ with the help of controller :+uploads+ and
get as a value the :+id+ and display :+file+.
:+multiple+ indicate that we need more than one :+id+.
:+item+ tell to the grid who is the container, we need to explicit this because we have two container in our upload.js
See view helpers[http://github.com/padrino/padrino-framework/blob/master/padrino-admin/lib/padrino-admin/helpers/view.rb#L145] for more docs.
That's all! Now run mingrations and browse accounts for see our uploader.
==== Attach One Upload to a Model
The process is the same as above you need only define in your model:
# app/models/account.rb
belongs_to :upload
and add this to your account form:
# admin/views/accounts/_form.haml
%tr
%td=f.label :upload
%td=f.open_window_grid :upload_id, :with => :uploads, :get => :id, :show => :file, :item => :panel
Remember to run migrations before start your server.
== Copyright
Copyright (c) 2010 Padrino. See LICENSE for details.