2017-09-10 17:25:29 +05:30
|
|
|
module Users
|
|
|
|
class UpdateService < BaseService
|
|
|
|
include NewUserNotifier
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@user = params.delete(:user)
|
2017-09-10 17:25:29 +05:30
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute(validate: true, &block)
|
|
|
|
yield(@user) if block_given?
|
|
|
|
|
|
|
|
user_exists = @user.persisted?
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
assign_attributes(&block)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if @user.save(validate: validate)
|
|
|
|
notify_success(user_exists)
|
2017-09-10 17:25:29 +05:30
|
|
|
else
|
|
|
|
error(@user.errors.full_messages.uniq.join('. '))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute!(*args, &block)
|
|
|
|
result = execute(*args, &block)
|
|
|
|
|
|
|
|
raise ActiveRecord::RecordInvalid.new(@user) unless result[:status] == :success
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def notify_success(user_exists)
|
|
|
|
notify_new_user(@user, nil) unless user_exists
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def assign_attributes(&block)
|
2018-03-17 18:26:18 +05:30
|
|
|
if @user.user_synced_attributes_metadata
|
|
|
|
params.except!(*@user.user_synced_attributes_metadata.read_only_attributes)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
@user.assign_attributes(params) if params.any?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|