Я пытаюсь понять, как писать разрешения эксперта в моем приложении Rails 4.
У меня есть модель статьи с политикой статьи. Статья политика имеет:
class ArticlePolicy < ApplicationPolicy
attr_reader :user, :scope
def initialize(user, record, scope)
@scope = scope
super(user, record)
end
class Scope < Scope
def resolve
if user == article.user
scope.where(user_id: user_id)
elsif approval_required?
scope.where(article.state_machine.in_state?(:review)).(user.has_role?(:org_approver))
else
article.state_machine.in_state?(:publish)
end
end
end
# TO DO - check if this is correct - I'm not sure.
# I now think I don't need the index actions because I have changed the action in the articles controller to look for policy scope.
# def index?
# article.state_machine.in_state?(:publish)
# end
def article
record
end
Контроллер статей имеет:
def index
@articles = policy_scope(Article)
# query = params[:query].presence || "*"
# @articles = Article.search(query)
end
Я слежу за документами экспертов, относящимися к областям, и пытаюсь понять, почему действие индекса, показанное в документах политики, не работает для меня. Я пробовал следующее (как показано в документах):
<% policy_scope(@user.articles).sort_by(&:created_at).in_groups_of(2) do |group| %>
но я получаю эту ошибку:
undefined local variable or method `article' for #<ArticlePolicy::Scope:0x007fff08ae9f48>
Может ли кто-нибудь увидеть, где я ошибся?
Я не уверен, что @user.articles прав. В моей конструкции статьи принадлежат пользователям, но в моем действии index я хочу показать каждому пользователю статьи, которые мои области позволяют им видеть.