[deploy] Refactor:Remove BackupData Model and Code (#10409)

This commit is contained in:
Molly Struve 2020-09-22 16:45:18 -05:00 committed by GitHub
parent afd89f7211
commit b0e086346d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 1 additions and 99 deletions

View file

@ -84,7 +84,6 @@ module Admin
identity = Identity.find(user_params[:identity_id])
@user = identity.user
begin
BackupData.backup!(identity)
identity.delete
@user.update("#{identity.provider}_username" => nil)
flash[:success] = "The #{identity.provider.capitalize} identity was successfully deleted and backed up."
@ -94,18 +93,6 @@ module Admin
redirect_to "/admin/users/#{@user.id}/edit"
end
def recover_identity
backup = BackupData.find(user_params[:backup_data_id])
@user = backup.instance_user
begin
identity = backup.recover!
flash[:success] = "The #{identity.provider} identity was successfully recovered, and the backup was removed."
rescue StandardError => e
flash[:danger] = e.message
end
redirect_to "/admin/users/#{@user.id}/edit"
end
def send_email
if NotifyMailer.with(params).user_contact_email.deliver_now
redirect_back(fallback_location: users_path)
@ -183,7 +170,7 @@ module Admin
new_note note_for_current_role user_status
pro merge_user_id add_credits remove_credits
add_org_credits remove_org_credits
organization_id identity_id backup_data_id
organization_id identity_id
]
params.require(:user).permit(allowed_params)
end

View file

@ -1,16 +0,0 @@
class BackupData < ApplicationRecord
belongs_to :instance, polymorphic: true
belongs_to :instance_user, class_name: "User"
validates :instance_id, :instance_type, :json_data, presence: true
def self.backup!(instance)
BackupData.create!(instance_type: instance.class.name, instance_id: instance.id,
instance_user_id: instance.user_id, json_data: instance.attributes)
end
def recover!
instance = instance_type.constantize.create!(json_data.to_h)
destroy!
instance
end
end

View file

@ -1,6 +1,5 @@
class Identity < ApplicationRecord
belongs_to :user
has_many :backup_data, as: :instance, class_name: "BackupData", dependent: :destroy
scope :enabled, -> { where(provider: Authentication::Providers.enabled) }

View file

@ -78,8 +78,6 @@ class User < ApplicationRecord
has_many :articles, dependent: :destroy
has_many :audit_logs, dependent: :nullify
has_many :authored_notes, inverse_of: :author, class_name: "Note", foreign_key: :author_id, dependent: :delete_all
has_many :backup_data, foreign_key: "instance_user_id", inverse_of: :instance_user,
class_name: "BackupData", dependent: :delete_all
has_many :badge_achievements, dependent: :destroy
has_many :badges, through: :badge_achievements
has_many :blocked_blocks, class_name: "UserBlock", foreign_key: :blocked_id,

View file

@ -14,7 +14,6 @@ module Users
user.blocked_blocks.delete_all
user.webhook_endpoints.delete_all
user.authored_notes.delete_all
user.backup_data.delete_all
user.display_ad_events.delete_all
user.email_messages.delete_all
user.html_variants.delete_all

View file

@ -84,22 +84,6 @@
<%= f.submit "Delete #{identity.provider.capitalize} Identity", class: "btn btn-danger" %>
<% end %>
<% end %>
<h3>This should be done only do this if you are certain the user is having the specific problem(s) listed above.</h3>
<% if @user.backup_data.any? %>
<hr>
<h2>Recover a deleted identity:</h2>
<ul>
<% @user.backup_data.where(instance_type: "Identity").each do |data| %>
<li>
<%= "#{data.json_data['provider'].capitalize} #{data.instance_type} - UID: #{data.json_data['uid']}" %>
<%= form_for(@user, url: recover_identity_admin_user_path(@user), html: { method: :post, onsubmit: "return confirm('Please confirm you want to recover the #{data.json_data['provider'].capitalize} identity.')" }) do |f| %>
<%= f.hidden_field :backup_data_id, value: data.id %>
<%= f.submit "Recover #{data.json_data['provider'].capitalize} Identity" %>
<% end %>
</li>
<% end %>
</ul>
<% end %>
</ul>
</div>
</div>

View file

@ -103,7 +103,6 @@ Rails.application.routes.draw do
patch "user_status"
post "merge"
delete "remove_identity"
post "recover_identity"
post "send_email"
post "verify_email_ownership"
patch "unlock_access"

View file

@ -1,10 +0,0 @@
FactoryBot.define do
factory :backup_data do
association :instance_user, factory: :user
after(:build) do |backup_data|
backup_data.instance = backup_data.instance_user.identities.first || create(:identity,
user: backup_data.instance_user)
backup_data.json_data = backup_data.instance.attributes
end
end
end

View file

@ -1,11 +0,0 @@
require "rails_helper"
RSpec.describe BackupData, type: :model do
describe "validations" do
it { is_expected.to validate_presence_of(:instance_type) }
it { is_expected.to validate_presence_of(:instance_id) }
it { is_expected.to validate_presence_of(:json_data) }
it { is_expected.to belong_to(:instance) }
it { is_expected.to belong_to(:instance_user).class_name("User") }
end
end

View file

@ -9,8 +9,6 @@ RSpec.describe Identity, type: :model do
it { is_expected.to belong_to(:user) }
it { is_expected.to have_many(:backup_data).class_name("BackupData").dependent(:destroy) }
it { is_expected.to validate_presence_of(:provider) }
it { is_expected.to validate_presence_of(:uid) }
it { is_expected.to validate_presence_of(:user_id) }

View file

@ -109,13 +109,6 @@ RSpec.describe User, type: :model do
.dependent(:delete_all)
end
it do
expect(subject).to have_many(:backup_data)
.class_name("BackupData")
.with_foreign_key("instance_user_id")
.dependent(:delete_all)
end
it do
expect(subject).to have_many(:blocked_blocks)
.class_name("UserBlock")

View file

@ -88,24 +88,6 @@ RSpec.describe "admin/users", type: :request do
end
end
describe "POST admin/users/:id/recover_identity" do
it "recovers a deleted identity" do
identity = user.identities.first
backup = BackupData.backup!(identity)
identity.delete
post "/admin/users/#{user.id}/recover_identity", params: { user: { backup_data_id: backup.id } }
expect(identity).to eq Identity.first
end
it "deletes the backup data" do
identity = user.identities.first
backup = BackupData.backup!(identity)
identity.delete
post "/admin/users/#{user.id}/recover_identity", params: { user: { backup_data_id: backup.id } }
expect { backup.reload }.to raise_error ActiveRecord::RecordNotFound
end
end
describe "PATCH admin/users/:id/unlock_access" do
it "unlocks a locked user account" do
user.lock_access!