FIX: handle new user when logging name change

This commit is contained in:
Arpit Jalan 2017-03-01 13:42:17 +05:30
parent ddcd060552
commit e27b1b98d1
3 changed files with 17 additions and 10 deletions

View File

@ -146,7 +146,7 @@ class StaffActionLogger
def log_site_text_change(subject, new_text=nil, old_text=nil, opts={})
raise Discourse::InvalidParameters.new(:subject) unless subject.present?
UserHistory.create( params(opts).merge({
UserHistory.create!( params(opts).merge({
action: UserHistory.actions[:change_site_text],
subject: subject,
previous_value: old_text,

View File

@ -53,6 +53,9 @@ class UserUpdater
user_profile.profile_background = attributes.fetch(:profile_background) { user_profile.profile_background }
user_profile.card_background = attributes.fetch(:card_background) { user_profile.card_background }
old_user_name = user.name.present? ? user.name : ""
user.name = attributes.fetch(:name) { user.name }
user.locale = attributes.fetch(:locale) { user.locale }
user.date_of_birth = attributes.fetch(:date_of_birth) { user.date_of_birth }
@ -94,20 +97,21 @@ class UserUpdater
end
User.transaction do
# log name changes
if attributes[:name].present? && user.name.downcase != attributes.fetch(:name).downcase
StaffActionLogger.new(@actor).log_name_change(user.id, user.name, attributes.fetch(:name))
elsif attributes[:name].blank? && user.name.present?
StaffActionLogger.new(@actor).log_name_change(user.id, user.name, "")
end
user.name = attributes.fetch(:name) { user.name }
if attributes.key?(:muted_usernames)
update_muted_users(attributes[:muted_usernames])
end
saved = (!save_options || user.user_option.save) && user_profile.save && user.save
DiscourseEvent.trigger(:user_updated, user) if saved
if saved
DiscourseEvent.trigger(:user_updated, user)
# log name changes
if attributes[:name].present? && old_user_name.downcase != attributes.fetch(:name).downcase
StaffActionLogger.new(@actor).log_name_change(user.id, old_user_name, attributes.fetch(:name))
elsif attributes[:name].blank? && old_user_name.present?
StaffActionLogger.new(@actor).log_name_change(user.id, old_user_name, "")
end
end
saved
end
end

View File

@ -192,9 +192,12 @@ describe UserUpdater do
end
it "logs the action" do
user_without_name = Fabricate(:user, name: nil)
user = Fabricate(:user, name: 'Billy Bob')
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe
expect { UserUpdater.new(acting_user, user_without_name).update(bio_raw: 'foo bar') }.to change { UserHistory.count }.by(0) # make sure user without name (name = nil) does not raise an error
expect { UserUpdater.new(acting_user, user_without_name).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
end
end
end