权威宝石:“策略范围”中的“呼叫应用程序策略”方法

时间:2018-07-10 22:52:46

标签: ruby-on-rails-5 pundit

在我的应用程序策略中,我有一个方法'user_is_manager?'

application_poilcy.rb

class ApplicationPolicy
attr_reader :user, :record

def initialize(user, record)
  @user = user
  @record = record
end
.
.
.
  def user_is_owner_of_record?
   @user.id == record.created_by?
  end

  def user_is_manager?
    @user.role_id == 1
  end
end

我需要调用方法“ user_is_manager?”来自“ ApplicationPolicy”,位于“ post_policy”范围内

post_policy.rb

class PostPolicy < ApplicationPolicy
 attr_reader :user, :record

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

  class Scope < Scope

    def resolve
      if user_is_manager?
        scope.all
      else
        raise Pundit::NotAuthorizedError, 'not allowed to view this action'
      end
    end
  end

我怎么称呼它?

0 个答案:

没有答案