2012-05-30 5 views
2

활성 관리자로 범위 지정 & cancan으로 어떻게 범위 지정을 사용할 수 있습니까? 나는 admin 사용자 & (기관)과 가진 (has_one) 관계가 있고 많은 기관이있는 기관은 기관지금 admin 사용자 로그인 그 때 동일한 기관이있는 모든 단면도를 표시하고 싶다.활성 관리자 cancan으로 범위 지정

다음 링크가 도움이되지 않습니다.

http://activeadmin.info/docs/2-resource-customization.html#scoping_the_queries

답변

4

당신은 단지이, 당신은 문제를 얻는 경우?

# ability.db 

def initialize(user) 
    case 
    # ... 
    when user.super_admin? 
     can :manage, :all 
    when user.admin? 
     can :manage, Profile, :institution_id => user.institution.id 
    # 
    # ... 
end 

이 허용됩니다 Profile.accessible_by(current_user)을, 당신은 최고 관리자가 모든 게시물에 액세스하려면 current_user.profiles

class AdminUser 
    has_one :institution 
    has_many :profiles, :through => :institution 
end 

ActiveAdmin.register Profile do 
    scope_to :current_user #here comes the variable which set in initializer 
end 

, 당신이 사용할 수있는 동일하게 여기있는 : 옵션

ActiveAdmin.register Profile do 
    scope_to :current_user, :association_method => :admin_profiles 
end 

# in class User 
def admin_profiles 
    if super_admin? 
    Profile.unscoped 
    else 
    profiles 
    end 
end 

에게 association_method를 까다로운 해결책은이를 일반화하고 위임자 클래스를 프록시로 사용하여 모든 모델에서 슈퍼 관리자를 선택 해제 할 수 있습니다. 내가 요청할 수 있습니다.

+0

그래서 기관을 통해 관리자와 프로필간에 가상의 관계를 만들어야합니다. 고마워요 !! 당신의 대답은 active admin & cancan에 대한 많은 것들을 지 웁니다 – chaitanya

+0

만약 사용자가 super_admin이라면 어떻게 할 수 있습니까? 예를 들면. 그는 모든 프로필을보아야합니까? – chaitanya

+0

내 업데이 트를 참조하십시오 hth –