pundit: convert post approvals to pundit.
This commit is contained in:
@@ -1,15 +1,14 @@
|
|||||||
class PostApprovalsController < ApplicationController
|
class PostApprovalsController < ApplicationController
|
||||||
before_action :approver_only, only: [:create]
|
|
||||||
respond_to :html, :xml, :json, :js
|
respond_to :html, :xml, :json, :js
|
||||||
|
|
||||||
def create
|
def create
|
||||||
post = Post.find(params[:post_id])
|
@approval = authorize PostApproval.new(user: CurrentUser.user, post_id: params[:post_id])
|
||||||
@approval = post.approve!
|
@approval.save
|
||||||
respond_with(@approval)
|
respond_with(@approval)
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@post_approvals = PostApproval.paginated_search(params)
|
@post_approvals = authorize PostApproval.paginated_search(params)
|
||||||
@post_approvals = @post_approvals.includes(:user, post: :uploader) if request.format.html?
|
@post_approvals = @post_approvals.includes(:user, post: :uploader) if request.format.html?
|
||||||
|
|
||||||
respond_with(@post_approvals)
|
respond_with(@post_approvals)
|
||||||
|
|||||||
5
app/policies/post_approval_policy.rb
Normal file
5
app/policies/post_approval_policy.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class PostApprovalPolicy < ApplicationPolicy
|
||||||
|
def create?
|
||||||
|
user.is_approver?
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -26,6 +26,14 @@ class PostApprovalsControllerTest < ActionDispatch::IntegrationTest
|
|||||||
assert(!@post.reload.is_deleted?)
|
assert(!@post.reload.is_deleted?)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "not allow non-approvers to approve posts" do
|
||||||
|
@post = create(:post, is_pending: true)
|
||||||
|
post_auth post_approvals_path(post_id: @post.id, format: :js), create(:user)
|
||||||
|
|
||||||
|
assert_response 403
|
||||||
|
assert_equal(true, @post.reload.is_pending?)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "index action" do
|
context "index action" do
|
||||||
|
|||||||
Reference in New Issue
Block a user