keep track of post approvals to prevent approval cycles

This commit is contained in:
Albert Yi
2016-10-31 17:51:44 -07:00
parent 03fd48c989
commit 2dbb869188
7 changed files with 159 additions and 4 deletions

View File

@@ -274,7 +274,7 @@ class Post < ActiveRecord::Base
module ApprovalMethods
def is_approvable?
!is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && approver_id != CurrentUser.id
!is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && !PostApproval.approved?(CurrentUser.id, id)
end
def flag!(reason, options = {})
@@ -314,7 +314,7 @@ class Post < ActiveRecord::Base
raise ApprovalError.new("You cannot approve a post you uploaded")
end
if approver_id == CurrentUser.id
if approver_id == CurrentUser.id || PostApproval.approved?(CurrentUser.id, id)
errors.add(:approver, "have already approved this post")
raise ApprovalError.new("You have previously approved this post and cannot approve it again")
end
@@ -324,6 +324,9 @@ class Post < ActiveRecord::Base
self.is_pending = false
self.is_deleted = false
self.approver_id = CurrentUser.id
PostApproval.create(user_id: CurrentUser.id, post_id: id)
save!
end
@@ -1362,7 +1365,7 @@ class Post < ActiveRecord::Base
end
if !CurrentUser.is_admin?
if approver_id == CurrentUser.id
if approver_id == CurrentUser.id || PostApproval.approved?(CurrentUser.id, id)
raise ApprovalError.new("You have previously approved this post and cannot undelete it")
elsif uploader_id == CurrentUser.id
raise ApprovalError.new("You cannot undelete a post you uploaded")

View File

@@ -0,0 +1,12 @@
class PostApproval < ActiveRecord::Base
belongs_to :user
belongs_to :post
def self.prune!
where("created_at < ?", 1.month.ago).delete_all
end
def self.approved?(user_id, post_id)
where(user_id: user_id, post_id: post_id).exists?
end
end