add support for deleting user accounts
This commit is contained in:
@@ -4,7 +4,11 @@ class Favorite < ActiveRecord::Base
|
||||
|
||||
# this is necessary because there's no trigger for deleting favorites
|
||||
def self.destroy_all(hash)
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
||||
if hash[:user_id] && hash[:post_id]
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]} and post_id = #{hash[:post_id]}")
|
||||
elsif hash[:user_id]
|
||||
connection.execute("delete from favorites_#{hash[:user_id] % 100} where user_id = #{hash[:user_id]}")
|
||||
end
|
||||
end
|
||||
|
||||
def self.add(post, user)
|
||||
|
||||
@@ -616,6 +616,35 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
module DeletionMethods
|
||||
def delete!
|
||||
scramble_password
|
||||
remove_all_favorites
|
||||
rename_for_delete
|
||||
end
|
||||
|
||||
def rename_for_delete
|
||||
alt_name = "user#{id}"
|
||||
n = 0
|
||||
while User.where(:name => alt_name).exists?
|
||||
alt_name = "user#{id}_#{n}"
|
||||
n += 1
|
||||
end
|
||||
update_attribute(:name, alt_name)
|
||||
|
||||
end
|
||||
|
||||
def scramble_password
|
||||
update_attribute(:bcrypt_password_hash, User.bcrypt(SecureRandom.hex(16)))
|
||||
end
|
||||
|
||||
def remove_all_favorites
|
||||
Post.tag_match("fav:#{name}").find_each do |post|
|
||||
Favorite.remove(post, self)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
include BanMethods
|
||||
include NameMethods
|
||||
include PasswordMethods
|
||||
@@ -629,6 +658,7 @@ class User < ActiveRecord::Base
|
||||
include InvitationMethods
|
||||
include ApiMethods
|
||||
extend SearchMethods
|
||||
include DeletionMethods
|
||||
|
||||
def initialize_default_image_size
|
||||
self.default_image_size = "large"
|
||||
|
||||
Reference in New Issue
Block a user