add basic user revert functionality
This commit is contained in:
28
app/logical/user_revert.rb
Normal file
28
app/logical/user_revert.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
# reverts all changes made by a user
|
||||
class UserRevert
|
||||
THRESHOLD = 1_000
|
||||
class TooManyChangesError < RuntimeError ; end
|
||||
|
||||
attr_reader :user_id
|
||||
|
||||
def initialize(user_id)
|
||||
@user_id = user_id
|
||||
end
|
||||
|
||||
def process
|
||||
validate!
|
||||
revert_post_changes
|
||||
end
|
||||
|
||||
def validate!
|
||||
if PostVersion.where(updater_id: user_id).count > THRESHOLD
|
||||
raise TooManyChangesError.new("This user has too many changes to be reverted")
|
||||
end
|
||||
end
|
||||
|
||||
def revert_post_changes
|
||||
PostVersion.where(updater_id: user_id).find_each do |x|
|
||||
x.undo!
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user