2016-05-16 55 views
0

我正在建立API终端Grape权威政策_scope与葡萄API

我有以下scope

class JourneyPolicy < ApplicationPolicy 

    def create? 
    user && user.identt_id == record 
    end 

    class Scope 
    attr_reader :user, :scope 

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

    def resolve 
     scope.where(user_id: user.id).latest 
    end 
    end 
end 

而现在,我想用这个policy_scope在我的葡萄资源,我们可以在轨道控制器使用policy_scope(Journey),但我不能使它与葡萄端点工作:

class Journeys < Grape::API 
    resources :journeys do 
    get do 
     @journeys = policy_scope(Journey) 
     present @journeys, with: Entities::Journey 
    end 
    end 
end 

这不是工作,而我得到NoMethodErrorpolicy_scope

我想用葡萄政策和任何帮助,将不胜感激:)

注:

我有下面这是在葡萄端点工作授权像资源代码:

error!("Unauthorized Access!", 401) unless JourneyPolicy.new(journey, uid).create? 

感谢

+0

我我从来没有使用过Rails,但我想方法policy_scope()只存在于Rails中。我不确定,但我相信你应该使用JourneyPolicy.new(journey,uid).create?代替。 – Marlon

回答

0

这可能不是最好的解决办法,但我创建了一个帮手甲基OD在grape手动命名policy_scope像:

def policy_scope(user, scope) 
    policy = "#{scope}Policy::Scope".constantize 
    policy.new(user, scope).resolve 
    end 

现在,我可以从我的葡萄资源,如呼叫policy_scope帮手:

policy_scope(current_user, Journey) 

,它是为我工作

感谢