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>
|
||||
|
||||
@@ -299,6 +299,7 @@ Rails.application.routes.draw do
|
||||
post :reject
|
||||
end
|
||||
end
|
||||
resource :user_revert, :only => [:new, :create]
|
||||
resources :wiki_pages do
|
||||
member do
|
||||
put :revert
|
||||
|
||||
47
test/unit/user_revert_test.rb
Normal file
47
test/unit/user_revert_test.rb
Normal file
@@ -0,0 +1,47 @@
|
||||
require 'test_helper'
|
||||
|
||||
class UserRevertTest < ActiveSupport::TestCase
|
||||
context "Reverting a user's changes" do
|
||||
setup do
|
||||
@creator = FactoryGirl.create(:user)
|
||||
@user = FactoryGirl.create(:user)
|
||||
CurrentUser.user = @user
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
|
||||
CurrentUser.scoped(@creator) do
|
||||
@parent = FactoryGirl.create(:post)
|
||||
@post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
|
||||
end
|
||||
|
||||
@post.stubs(:merge_version?).returns(false)
|
||||
@post.update_attributes(:tag_string => "bbb ccc xxx", :source => "", :rating => "e")
|
||||
end
|
||||
|
||||
teardown do
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
subject { UserRevert.new(@user.id) }
|
||||
|
||||
should "have the correct data" do
|
||||
assert_equal("bbb ccc xxx", @post.tag_string)
|
||||
assert_equal("", @post.source)
|
||||
assert_equal("e", @post.rating)
|
||||
end
|
||||
|
||||
context "when processed" do
|
||||
setup do
|
||||
subject.process
|
||||
@post.reload
|
||||
end
|
||||
|
||||
should "revert the user's changes" do
|
||||
assert_equal("aaa bbb ccc", @post.tag_string)
|
||||
assert_equal("xyz", @post.source)
|
||||
assert_equal("q", @post.rating)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user