2016-08-27 1 views
0

위키 목록이 긴 (게시물과 같은) 앱을 만들고 있습니다. 앱에서 Pundit을 사용하고 있습니다. 페이지 매김을 위해 두 개의 보석, will_paginatekaminari을 발견했습니다.스코프 클래스의 레일에서 페이지 매김을 사용하는 방법은 무엇입니까?

이 같은 WikiController는 모습입니다 :

class WikisController < ApplicationController 
    def index 
    if current_user == nil 
     @wikis = Wiki.visible_to_all 
    else 
     @wikis = policy_scope(Wiki) #pundit 
    end 

visible_to_all이 관리자가 아닌 사용자에게 모든 비 개인 위키를 표시하기 위해 만든 범위입니다.

#on Wiki.rb (model) 
scope :visible_to_all, -> {where(private: [false, nil])} 

어쨌든, 지금은 위키의 과다를하고 페이지를 매기는하고 싶습니다.

class UserController 

    def index 
    @users = User.paginate(:page => params[:page], :per_page => 5) 
    end 

미나리는 컨트롤러에서 이렇게 말한다 : will_paginate 관련 컨트롤러에이 일을 말한다 내가 Wikis 범위를 지정하고 있기 때문에 @wikis 될 것입니다 내 경우 @users

@users = User.order(:name).page params[:page] 

; 그러나 @wikis은 이미 범위 지정에 사용됩니다. 범위를 유지하고 내 Wiki에 페이지 매기기를 적용하려면 어떻게해야합니까?

편집 : wiki_policy.rb :

def resolve 
    wikis = [] 
    if user.role == 'admin' 
     wikis = scope.all # if the user is an admin, show them all the wikis 
    elsif user.role == 'premium' 
     all_wikis = scope.all 
     all_wikis.each do |wiki| 
     if wiki.private == false || wiki.private == nil || wiki.user == user || wiki.users.include?(user) 
      wikis << wiki # if the user is premium, only show them public wikis, or that private wikis they created, or private wikis they are a collaborator on 
     end 
     end 
    else # this is the lowly standard user 
     all_wikis = scope.all 
     wikis = [] 
     all_wikis.each do |wiki| 
     if wiki.private == false || wiki.users.include?(user) 
      wikis << wiki # only show standard users public wikis and private wikis they are a collaborator on 
     end 
     end 
    end 
    wikis # return the wikis array we've built up 
    end 
+0

(!) 참고 : 다음 학자-범위에 범위는 당신이 뭔가를 할 수 있습니다 당신은 학자 범위에 .visible_to_all 범위를 통합 할 수 있을까요? 동일한 current_user는 Pundit 범위에서 (사용자로 명명 된) 범위에서도 사용할 수 있습니다. – ollpu

+0

나는 그것을 찾을 것이다! 이것은 내가 만든 첫 번째 레일 앱 중 하나이기 때문에 가장 효율적이지는 않습니다. 그러나 그것은 좋은 제안입니다! 나는 되돌아 가서 그것을 리팩토링 할 것이다 :) 고마워! – Iggy

답변

0

Wiki.visible_to_allpolicy_scope(Wiki) 모두 furher 다른 기능을 체인으로 범위 수있는 ActiveRecord::Relation 반환합니다.

은 당신이 특정 범위 (또는 관계)에서 페이지를 얻기 위해해야 ​​할 일은, 이런 식으로, 그것에 .paginate를 추가하는 것입니다

@wikis = policy_scope(Wiki).paginate(:page => params[:page], :per_page => 5) 

편집 : visible_to_all를 통합으로 -

def resolve 
    if user.present? 
    # current_user is present, get the normal scope here 
    else 
    scope.where(private: [false, nil]) 
    end 
end 
+0

# Iggy

+0

흥미 롭습니다 ...'policy_scope'는 배열을 리턴합니다. resolve-method (아마도 wiki_policy.rb 파일에)가 어떻게 정의되어 있습니까? – ollpu

+0

wiki_policy.rb의 resolve 메소드를 포함하도록 내 게시물을 편집했습니다. 어떤 형식의 파일이 반환되어야합니까? (배열을 반환하지 않아야합니까?) – Iggy

관련 문제