REFACTOR: DRY up code and add better test coverage.

This commit is contained in:
Guo Xiang Tan 2017-10-23 11:16:14 +08:00
parent 87e4c2a90f
commit e91a631978
2 changed files with 27 additions and 12 deletions

View File

@ -110,15 +110,15 @@ class UserUpdater
update_muted_users(attributes[:muted_usernames]) update_muted_users(attributes[:muted_usernames])
end end
saved = (!save_options || user.user_option.save) && user_profile.save && user.save if (saved = (!save_options || user.user_option.save) && user_profile.save && user.save) &&
(attributes[:name].present? && old_user_name.casecmp(attributes.fetch(:name)) != 0) ||
(attributes[:name].blank? && old_user_name.present?)
if saved StaffActionLogger.new(@actor).log_name_change(
# log name changes user.id,
if attributes[:name].present? && old_user_name.downcase != attributes.fetch(:name).downcase old_user_name,
StaffActionLogger.new(@actor).log_name_change(user.id, old_user_name, attributes.fetch(: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 end
end end

View File

@ -200,10 +200,25 @@ describe UserUpdater do
it "logs the action" do it "logs the action" do
user_without_name = Fabricate(:user, name: nil) user_without_name = Fabricate(:user, name: nil)
user = Fabricate(:user, name: 'Billy Bob') user = Fabricate(:user, name: 'Billy Bob')
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1) expect do
expect { UserUpdater.new(acting_user, user).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(0) # make sure it does not log a dupe UserUpdater.new(acting_user, user).update(name: 'Jim Tom')
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 end.to change { UserHistory.count }.by(1)
expect { UserUpdater.new(acting_user, user_without_name).update(name: 'Jim Tom') }.to change { UserHistory.count }.by(1)
expect do
UserUpdater.new(acting_user, user).update(name: 'JiM TOm')
end.to_not change { UserHistory.count }
expect do
UserUpdater.new(acting_user, user_without_name).update(bio_raw: 'foo bar')
end.to_not change { UserHistory.count }
expect do
UserUpdater.new(acting_user, user_without_name).update(name: 'Jim Tom')
end.to change { UserHistory.count }.by(1)
expect do
UserUpdater.new(acting_user, user).update(name: '')
end.to change { UserHistory.count }.by(1)
end end
end end
end end