你可以设定,让用户有polymorphic association到任何一个学校,一个部门或一个区域。如果此关联为零,则表示用户可以访问所有内容(您提到的国家级别)。
class User < ActiveRecord::Base
belongs_to :administrates, :polymorphic => true
end
class School < ActiveRecord::Base
belongs_to :branch
has_many :users, :as => :administrates
end
class Branch < ActiveRecord::Base
belongs_to :region
has_many :schools
has_many :users, :as => :administrates
end
class Region < ActiveRecord::Base
has_many :branches
has_many :users, :as => :administrates
end
您不能使其对Active Admin完全透明,因为您必须告诉Active Admin使用特定范围。为此,您应该可以在ActiveAdmin.register
区块内获得scope_to
。你必须做一个小魔术,使scope_to
工作,多态关联,但它是可行的:
ActiveAdmin.register School do
scope_to do
Class.new do
def self.schools
case current_user.administrates
when School
School.where(:id => current_user.administrates_id)
when Branch
School.where(:branch_id => current_user.administrates_id)
when Region
School.where(:branch_id => current_user.administrates.branches.map(&:id))
when NilClass
School.scoped
end
end
end
end
end
这基本上意味着,每一次活动管理员会加载一所学校(或学校的索引页上的列表) ,它将通过我们在scope_to
块内创建的匿名类进行范围。
根据您的要求,您应该能够在Branch
和Region
型号上实现类似的功能。
但是,您应该知道,当使用scope_to
时,当前存在an open issue,关于显示当前用户范围之外的资源的过滤器和表单。
您还需要授权才能限制某个级别的用户只能看到该级别及以下的级别(例如,分支级别的用户不应访问区域)。为此,您应该使用CanCan。
有关如何在Active Admin中集成CanCan的信息,请参阅this或this。
我对管理框架的经验是,它们不适合普通的网站用户。然而,我没有ActiveAdmin的经验,所以我不能评论它。不过看起来不错,我会试一试我的下一个项目。 –
它看起来很酷,不是吗?我已经有了RailsAdmin来启动和运行,但是我将退休并从头开始管理自己的管理员 - 试图弄清楚ActiveAdmin是否足够灵活以便成为一个好的基础。 – Jerome
Gotcha ;-)让我们知道你在找什么 –