Я пытаюсь выяснить, как использовать области политики pundit в моей политике статьи.Rails - с областями Pundit в политике
Я написал политику статьи, в которой находится область действия, а затем в ней есть метод разрешения. Метод разрешения имеет альтернативы на основе того, кем является current_user
.
Моя статья политики:
class ArticlePolicy < ApplicationPolicy
class Scope < Scope
attr_reader :user, :scope
# I now think I don't need these 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 show?
article.state_machine.in_state?(:publish) ||
user == article.user ||
article.state_machine.in_state?(:review) && user.org_approver ||
false
end
end
def create?
article.user.has_role?(:author)
end
def update?
# user && user.article.exists?(article.id) #&& user.article.created_at < 15.minutes.ago
user.present? && user == article.user
# add current state is not published or approved
end
def destroy?
user.present? && user == article.user
# user.admin?
# user.present?
# user && user.article.exists?(article.id)
end
end
private
def article
record
end
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
def approval_required?
true if article.user.has_role?(:author)
# elsif article.user.profile.organisation.onboarding.article_approval == true
# if onboarding (currently in another branch) requires org approval
end
def org_approver
if article.user.has_role? :author
user.has_role? :editor
# if onboarding (currently in another branch) requires org approval, then the approval manager for that org
elsif article.user.has_role? :blogger
user.has_role? :editor if user.profile.organisation.id == article.user.profile.organisation.id
end
end
end
Пример в Пандит документации показывает, как использовать это для индекса, но как я использую метод разрешить для шоу действий? Могу ли я написать несколько методов решения для других действий контроллера?
Thanks так много @ sameera207. Это заставило меня начать решать проблемы. Для других - области должны быть отделены от элементов управления действием. Области могут быть нюансированы для специфики набора данных. Элементы управления действиями должны быть способны к истинному или ложному результату (и если это правда, тогда область применения применяется). – Mel