Class: CommandTower::Services::Admin::Users::UpdateName

Inherits:
CommandTower::Services::ApplicationService show all
Defined in:
app/services/command_tower/services/admin/users.rb

Constant Summary

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from CommandTower::Services::ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/services/command_tower/services/admin/users.rb', line 94

def call
  normalized_first = first_name.to_s.strip
  normalized_last = last_name.to_s.strip
  first_changed = normalized_first != user.first_name.to_s
  last_changed = normalized_last != user.last_name.to_s

  unless first_changed || last_changed
    context.user = user
    context.changed = false
    return
  end

  application_error = nil

  ActiveRecord::Base.transaction do
    if first_changed
      application_error = mutate(first_name: normalized_first)
      raise ActiveRecord::Rollback if application_error
    end

    if last_changed
      application_error = mutate(last_name: normalized_last)
      raise ActiveRecord::Rollback if application_error
    end
  end

  if application_error
    context.fail!(application_error:)
    return
  end

  context.user = user.reload
  context.changed = true
end