dynamic_field.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. module Import
  3. module OTRS
  4. class DynamicField
  5. def initialize(dynamic_field)
  6. @internal_name = self.class.convert_name(dynamic_field['Name'])
  7. return if already_imported?(dynamic_field)
  8. return if skip?(dynamic_field)
  9. initialize_attribute_config(dynamic_field)
  10. init_callback(dynamic_field)
  11. add
  12. end
  13. def self.convert_name(dynamic_field_name)
  14. dynamic_field_name.underscore.sub(%r{_id(s)?\z}, '_no\1')
  15. end
  16. private
  17. def init_callback(_)
  18. raise 'No init callback defined for this dynamic field!'
  19. end
  20. def already_imported?(dynamic_field)
  21. attribute = ObjectManager::Attribute.get(
  22. object: dynamic_field['ObjectType'],
  23. name: @internal_name,
  24. )
  25. attribute ? true : false
  26. end
  27. def initialize_attribute_config(dynamic_field)
  28. @attribute_config = {
  29. object: dynamic_field['ObjectType'],
  30. name: @internal_name,
  31. display: dynamic_field['Label'],
  32. screens: {
  33. view: {
  34. '-all-' => {
  35. shown: true,
  36. },
  37. },
  38. },
  39. active: true,
  40. editable: dynamic_field['InternalField'] == '0',
  41. position: dynamic_field['FieldOrder'],
  42. created_by_id: 1,
  43. updated_by_id: 1,
  44. }
  45. end
  46. def skip?(_dynamic_field)
  47. false
  48. end
  49. def add
  50. ObjectManager::Attribute.add(@attribute_config)
  51. ObjectManager::Attribute.migration_execute(false)
  52. end
  53. end
  54. end
  55. end