update.rb 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class Sequencer::Unit::Import::Common::Model::Update < Sequencer::Unit::Base
  3. include ::Sequencer::Unit::Import::Common::Model::Mixin::HandleFailure
  4. prepend ::Sequencer::Unit::Import::Common::Model::Mixin::Skip::Action
  5. skip_any_action
  6. uses :instance, :mapped
  7. provides :action
  8. def process
  9. # check if no instance is given - so we can't update it
  10. return if !instance
  11. # lock the current instance for write access
  12. instance.with_lock do
  13. # delete since we have an update and
  14. # the record is already created
  15. mapped.delete(:created_by_id)
  16. mapped.delete(:created_at)
  17. # assign regular attributes
  18. instance.assign_attributes(mapped)
  19. action = changed? ? :updated : :unchanged
  20. state.provide(:action, action)
  21. end
  22. rescue => e
  23. handle_failure(e)
  24. end
  25. private
  26. def changed?
  27. logger.debug { "Changed instance attributes: #{changes.inspect}" }
  28. changes.present?
  29. end
  30. def changes
  31. @changes ||= begin
  32. if instance.has_changes_to_save?
  33. # dry run
  34. instance.changes_to_save
  35. else
  36. # live run
  37. instance.previous_changes
  38. end
  39. end
  40. end
  41. end