2009-11-27 3 views
1

적용 역할과 객체의 목록을 얻을 :내가 이런 식으로 생각 be9의 acl9

def self.obj_list(opts = {:include => [] , :exclude => []}) 
    # Returns an array with all objects with roles applied 
    # +:exclude+:: (array,string) optional object type to exclude from list 
    # +:include+:: (array,string) optional object type to include in list 
    # Example: 
    # Role.obj_list(:include => ["Device", "User"]) 
    # Role.obj_list(:exclude => ["User"]) 

    inc = opts[:include].to_a 
    exc = opts[:exclude].to_a 

    objs = [] 
    if inc.empty? 

     self.all.each do |r| 
     unless r.authorizable_type.nil? 
      objs << r.authorizable_type.constantize.find(r.authorizable_id) unless exc.include?(r.authorizable_type) 
     end 
     end 

    else 

     self.all.each do |r| 
     unless r.authorizable_type.nil? 
      objs << r.authorizable_type.constantize.find(r.authorizable_id) if inc.include?(r.authorizable_type) 
     end 
     end 

    end 
    objs 
    end 

답변

0

당신은 SQL에 포함/제외 물건을 할 where 절을 사용할 수 있습니다 : 당신이 다형성의 레일 자신의 처리를 얻을 것이다 authorizable를 사용하여

(inc.empty? 
? where.not(:authorizable_type => exc) 
: where(:authorizable_type => inc) 
).map(&:authorizable) 

실제 개체 만 반환되도록 보장하므로 nil

을 확인할 필요가 없습니다.