gitlab-org--gitlab-foss/app/services/users/update_service.rb

45 lines
956 B
Ruby
Raw Normal View History

2017-06-13 05:32:21 -04:00
module Users
class UpdateService < BaseService
include NewUserNotifier
def initialize(user, params = {})
2017-06-13 05:32:21 -04:00
@user = user
@params = params.dup
end
def execute(validate: true, &block)
2017-06-23 11:11:31 -04:00
yield(@user) if block_given?
assign_attributes(&block)
2017-06-13 05:32:21 -04:00
user_exists = @user.persisted?
2017-06-22 10:54:54 -04:00
if @user.save(validate: validate)
notify_new_user(@user, nil) unless user_exists
2017-06-13 05:32:21 -04:00
success
else
error(@user.errors.full_messages.uniq.join('. '))
2017-06-13 05:32:21 -04:00
end
end
2017-06-22 06:10:28 -04:00
def execute!(*args, &block)
2017-06-22 05:27:37 -04:00
result = execute(*args, &block)
raise ActiveRecord::RecordInvalid.new(@user) unless result[:status] == :success
2017-06-22 06:10:28 -04:00
true
end
private
def assign_attributes(&block)
2017-08-29 04:57:41 -04:00
if @user.user_synced_attributes_metadata
params.except!(*@user.user_synced_attributes_metadata.read_only_attributes)
end
@user.assign_attributes(params) if params.any?
end
2017-06-13 05:32:21 -04:00
end
end