Здравствуйте, я новичок в рубинах на рельсах и в настоящее время работаю над упражнением, в котором у меня есть 3 типа пользователей (Admin , модератор и участник). Я использую жемчужину Pundit с Devise Gem. Меня попросили определить классы области Pundit, чтобы сделать сообщения доступными в соответствии с ролью пользователя.В рельсах, определяющих подкласс класса Pundit для отображения разных наборов сообщений в зависимости от типа пользователя
Администратор и модератор могут видеть все сообщения. Подписанный пользователем может видеть только сообщения. Гость не может видеть сообщения.
Вот PostsController:
class PostsController < ApplicationController
def index
@posts = policy_scope(Post.all)
authorize @posts
end
def show
@post = Post.find(params[:id])
end
def new
@post = Post.new
authorize @post
end
def create
@post = current_user.posts.build(params.require(:post).permit(:title, :body))
authorize @post
if @post.save
flash[:notice] = "Post was saved"
redirect_to @post
else
flash[:error] = "There was an error saving the post. Please try again"
render :new
end
end
def edit
@post = Post.find(params[:id])
authorize @post
end
def update
@post = Post.find(params[:id])
authorize @post
if @post.update_attributes(params.require(:post).permit(:title, :body))
flash[:notice] = "Post was updated."
redirect_to @post
else
flash[:error] = "There was an error saving the post.Please try again."
render :edit
end
end
end
Here's my application policy:
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
end
def index?
false
end
def show?
scope.where(:id => record.id).exists?
end
def create?
user.present?
end
def new?
create?
end
def update?
user.present? && (record.user == user || user.admin?)
end
def edit?
update?
end
def destroy?
update?
end
def scope
record.class
end
class Scope
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
scope
end
end
end
И мой пост политик:
class PostPolicy < ApplicationPolicy
class Scope < Scope
def resolve
if user.admin? || user.moderator?
scope.all
else
scope.where(:id => user.id).exists?
end
end
end
def index?
user.admin? || user.id?
end
end
Кроме того, там, где я могу прочитать или узнать больше о размахе политике с Пандитом и разрешение на рельсах?
Вы действительно не сказали, что может быть проблемой? вы можете понять, как выполнить упражнение, или вы застряли на нем? Вы можете узнать больше о Pundit [там] (https://github.com/elabs/pundit) –