2017-06-13 09:32:21 +00:00
|
|
|
module Users
|
|
|
|
class UpdateService < BaseService
|
2017-06-23 09:34:07 +00:00
|
|
|
def initialize(user, params = {})
|
2017-06-13 09:32:21 +00:00
|
|
|
@user = user
|
|
|
|
@params = params.dup
|
|
|
|
end
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
def execute(validate: true, &block)
|
2017-06-23 15:11:31 +00:00
|
|
|
yield(@user) if block_given?
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
assign_attributes(&block)
|
2017-06-13 09:32:21 +00:00
|
|
|
|
2017-06-22 14:54:54 +00:00
|
|
|
if @user.save(validate: validate)
|
2017-06-13 09:32:21 +00:00
|
|
|
success
|
|
|
|
else
|
2017-06-14 13:32:25 +00:00
|
|
|
error(@user.errors.full_messages.uniq.join('. '))
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-22 10:10:28 +00:00
|
|
|
def execute!(*args, &block)
|
2017-06-22 09:27:37 +00:00
|
|
|
result = execute(*args, &block)
|
2017-06-14 09:35:58 +00:00
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
raise ActiveRecord::RecordInvalid.new(@user) unless result[:status] == :success
|
2017-06-22 10:10:28 +00:00
|
|
|
|
|
|
|
true
|
2017-06-14 09:35:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-06-23 09:34:07 +00:00
|
|
|
def assign_attributes(&block)
|
2017-06-14 09:35:58 +00:00
|
|
|
@user.assign_attributes(params) if params.any?
|
|
|
|
end
|
2017-06-13 09:32:21 +00:00
|
|
|
end
|
|
|
|
end
|