pundit: convert favorite groups to pundit.

This commit is contained in:
evazion
2020-03-19 18:12:16 -05:00
parent 50fa674a3e
commit 2c4c29b81a
9 changed files with 80 additions and 54 deletions

View File

@@ -160,14 +160,6 @@ class FavoriteGroup < ApplicationRecord
post_ids.include?(post_id)
end
def editable_by?(user)
creator_id == user.id
end
def viewable_by?(user)
creator_id == user.id || is_public
end
def self.available_includes
[:creator]
end

View File

@@ -827,12 +827,12 @@ class Post < ApplicationRecord
when /^-favgroup:(.+)$/i
favgroup = FavoriteGroup.find_by_name_or_id!($1, CurrentUser.user)
raise User::PrivilegeError unless favgroup.editable_by?(CurrentUser.user)
raise User::PrivilegeError unless Pundit.policy!([CurrentUser.user, nil], favgroup).update?
favgroup&.remove!(self)
when /^favgroup:(.+)$/i
favgroup = FavoriteGroup.find_by_name_or_id!($1, CurrentUser.user)
raise User::PrivilegeError unless favgroup.editable_by?(CurrentUser.user)
raise User::PrivilegeError unless Pundit.policy!([CurrentUser.user, nil], favgroup).update?
favgroup&.add!(self)
end