pundit: convert post votes to pundit.

Side effects:

* The data-current-user-is-voter <body> attribute has been removed.
* {{upvote:self}} no longer works. {{upvote:<name>}} should be used instead.
This commit is contained in:
evazion
2020-03-19 22:55:28 -05:00
parent 33d81d0d1b
commit 415d9591c5
9 changed files with 46 additions and 42 deletions

View File

@@ -939,7 +939,7 @@ class Post < ApplicationRecord
def add_favorite!(user)
Favorite.add(post: self, user: user)
vote!("up", user) if user.is_voter?
vote!("up", user) if Pundit.policy!([user, nil], PostVote).create?
rescue PostVote::Error
end
@@ -949,7 +949,7 @@ class Post < ApplicationRecord
def remove_favorite!(user)
Favorite.remove(post: self, user: user)
unvote!(user) if user.is_voter?
unvote!(user) if Pundit.policy!([user, nil], PostVote).create?
rescue PostVote::Error
end
@@ -1031,7 +1031,7 @@ class Post < ApplicationRecord
end
def vote!(vote, voter = CurrentUser.user)
unless voter.is_voter?
unless Pundit.policy!([voter, nil], PostVote).create?
raise PostVote::Error.new("You do not have permission to vote")
end

View File

@@ -17,8 +17,7 @@ class User < ApplicationRecord
# Used for `before_action :<role>_only`. Must have a corresponding `is_<role>?` method.
Roles = Levels.constants.map(&:downcase) + [
:banned,
:approver,
:voter
:approver
]
# candidates for removal:
@@ -354,10 +353,6 @@ class User < ApplicationRecord
level >= Levels::ADMIN
end
def is_voter?
is_gold?
end
def is_approver?
can_approve_posts?
end