0

ユーザーの役割に応じて、さまざまな種類の Wiki をインデックス ビューに表示したいと考えています。adminおよび/ゲストユーザーのポリシーはstandard正常に機能しますが、プレミアムユーザーとコラボレーションに関しては少し面倒です. 私のアプリでは、 Collaborators を private に追加する機能がありWikisます。したがって、プレミアム ユーザーは、自分の非公開 Wiki、公開 Wiki、および共同編集している非公開 Wiki を見ることができるはずですが、共同編集者である非公開 Wiki は表示されません。ポリシーまたはモデルの関連付けと関係があるのでしょうか? 私を助けてください

ウィキ#インデックス

  def index
    @wikis = Kaminari.paginate_array(policy_scope(Wiki)).page(params[:page]).per(10)
  end

ユーザーモデル

class User < ActiveRecord::Base
  has_many :wikis
  has_many :collaborators
  belongs_to :collaborators
....

ウィキモデル

    class Wiki < ActiveRecord::Base
      belongs_to :user
      has_many :collaborators
      has_many :users, through: :collaborators
....

コラボレーターモデル

class Collaborator < ActiveRecord::Base
  belongs_to :user
  belongs_to :wiki
end

Wiki_policy

   class Scope
     attr_reader :user, :scope

     def initialize(user, scope)
       @user = user
       @scope = scope
     end

     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.owner == user || wiki.collaborators.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.collaborators.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
   end

コンソールに入ると

last = Wiki.last 
last.collaborators 

私はこれを得る:

   => #<ActiveRecord::Associations::CollectionProxy [#<Collaborator id: 7, user_id: 8, wiki_id: 104, created_at: "2016-04-24 08:07:20", updated_at: "2016-04-24 08:07:20">]>
4

1 に答える 1