dynamic_field.rb 1.5 KB

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