Customize new users before save

The old before_create callback fired too late and it was almost
certain to overwrite changes to the model made elsewhere without
very careful orchestration. This moves customization closer
to instance creation and averts many potential problems.

The customize method still needs to take into account that models
can be customized during creation with hash params and check
for those changes for important parameters.

The better way to do this is probably to change the defaults in
the database, but it's less customizable.
This commit is contained in:
Kira 2019-09-12 15:00:03 -07:00
parent 9c998252c5
commit b7ce7a7ae9
2 changed files with 5 additions and 19 deletions

View File

@ -93,7 +93,6 @@ class User < ApplicationRecord
before_update :encrypt_password_on_update
after_save :update_cache
before_create :promote_to_admin_if_first_user
before_create :customize_new_user
#after_create :notify_sock_puppets
after_create :create_user_status
has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy
@ -280,11 +279,6 @@ class User < ApplicationRecord
end
end
def customize_new_user
return if Rails.env.test?
Danbooru.config.customize_new_user(self)
end
def role
level_string.downcase.to_sym
end
@ -877,6 +871,9 @@ class User < ApplicationRecord
self.last_ip_addr ||= CurrentUser.ip_addr
self.enable_keyboard_navigation = true
self.enable_auto_complete = true
return if Rails.env.test?
Danbooru.config.customize_new_user(self)
end
def presenter

View File

@ -112,8 +112,8 @@ module Danbooru
# Set the default level, permissions, and other settings for new users here.
def customize_new_user(user)
user.level = User::Levels::UNACTIVATED
user.comment_threshold = -10
user.level = User::Levels::UNACTIVATED unless user.will_save_change_to_level?
user.comment_threshold = -10 unless user.will_save_change_to_comment_threshold?
user.blacklisted_tags = 'gore
scat
watersports
@ -121,17 +121,6 @@ young -rating:s
loli
shota
fart'
# user.level = User::Levels::MEMBER
# user.can_approve_posts = false
# user.can_upload_free = false
# user.is_super_voter = false
#
# user.base_upload_limit = 10
# user.comment_threshold = -1
# user.blacklisted_tags = ["spoilers", "guro", "scat", "furry -rating:s"].join("\n")
# user.default_image_size = "large"
# user.per_page = 20
# user.disable_tagged_filenames = false
true
end