update.rb 1.7 KB

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