add basic user revert functionality
This commit is contained in:
15
app/controllers/user_reverts_controller.rb
Normal file
15
app/controllers/user_reverts_controller.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class UserRevertsController < ApplicationController
|
||||
before_filter :janitor_only
|
||||
|
||||
def new
|
||||
@user = User.find(params[:user_id])
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find(params[:user_id])
|
||||
revert = UserRevert.new(user.id)
|
||||
revert.process
|
||||
redirect_to(user_path(user.id))
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
@@ -149,7 +149,7 @@ class UserPresenter
|
||||
end
|
||||
|
||||
def post_version_count(template)
|
||||
template.link_to(user.post_update_count, template.post_versions_path(:search => {:updater_id => user.id}))
|
||||
template.link_to(user.post_update_count, template.post_versions_path(:lr => user.id, :search => {:updater_id => user.id}))
|
||||
end
|
||||
|
||||
def note_version_count(template)
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
<% else %>
|
||||
<%= render "listing", :post_versions => @post_versions %>
|
||||
|
||||
<% if params[:lr] && CurrentUser.is_janitor? %>
|
||||
<p><%= link_to "Revert this user's changes", new_user_revert_path(:user_id => params[:lr]) %></p>
|
||||
<% end %>
|
||||
|
||||
<%= numbered_paginator(@post_versions) %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
19
app/views/user_reverts/new.html.erb
Normal file
19
app/views/user_reverts/new.html.erb
Normal file
@@ -0,0 +1,19 @@
|
||||
<div id="c-users">
|
||||
<div id="a-new">
|
||||
<h1>Revert Changes</h1>
|
||||
|
||||
<p>You are about to revert all changes made by <%= link_to_user @user %>. Continue?</p>
|
||||
|
||||
<%= form_tag(user_revert_path) do %>
|
||||
<%= hidden_field_tag :user_id, params[:user_id] %>
|
||||
|
||||
<%= submit_tag "Yes" %>
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<%= render "users/secondary_links" %>
|
||||
|
||||
<% content_for(:page_title) do %>
|
||||
User Revert - <%= Danbooru.config.app_name %>
|
||||
<% end %>
|
||||
@@ -56,7 +56,12 @@
|
||||
|
||||
<tr>
|
||||
<th>Post Changes</th>
|
||||
<td><%= presenter.post_version_count(self) %></td>
|
||||
<td>
|
||||
<%= presenter.post_version_count(self) %>
|
||||
<% if CurrentUser.is_janitor? %>
|
||||
[<%= link_to "revert all", new_user_revert_path(user_id: user.id) %>]
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
|
||||
Reference in New Issue
Block a user