Permalink
Browse files

Doc on scoping the resource

  • Loading branch information...
1 parent cf77ced commit 8f079356be1acd8b7ab56990db68cda98d7f2f2f @webmat webmat committed Sep 19, 2011
Showing with 30 additions and 0 deletions.
  1. +30 −0 docs/2-resource-customization.md
View
30 docs/2-resource-customization.md
@@ -1,5 +1,35 @@
# Customize The Resource
+## Scoping the queries
+
+If your administrators have different access levels, you may sometimes want to scope what they have access to. Assuming your User model has the proper has_many relationships, you can simply scope the listings and finders like so:
+
+ ActiveAdmin.register Post do
+ scope_to :current_user
+
+ # or if the association doesn't have the default name.
+ # scope_to :current_user, :association_method => :blog_posts
+ end
+
+That approach limits the posts an admin can access to ```current_user.posts```.
+
+If you want to do something fancier, for example override a default scope, you can also use :association_method parameter with a normal method on your User model. The only requirement is that your method returns an instance of ActiveRecord::Relation.
+
+ class Ad < ActiveRecord::Base
+ default_scope lambda { where :published => true }
+ end
+
+ class User < ActiveRecord::Base
+ def managed_ads
+ # Overrides Ad's default_scope
+ Ad.unscoped
+ end
+ end
+
+ ActiveAdmin.register Ad do
+ scope_to :current_user, :association_method => :managed_ads
+ end
+
## Rename the Resource
By default, any references to the resource (menu, routes, buttons, etc) in the

0 comments on commit 8f07935

Please sign in to comment.