base.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. module Import
  3. class Zendesk
  4. module ObjectAttribute
  5. class Base
  6. def initialize(object, name, attribute)
  7. initialize_data_option(attribute)
  8. init_callback(attribute)
  9. add(object, name, attribute)
  10. end
  11. private
  12. def init_callback(_attribute); end
  13. def add(object, name, attribute)
  14. ObjectManager::Attribute.add( attribute_config(object, name, attribute) )
  15. ObjectManager::Attribute.migration_execute(false)
  16. rescue
  17. # rubocop:disable Style/SpecialGlobalVars
  18. raise $!, "Problem with ObjectManager Attribute '#{name}': #{$!}", $!.backtrace
  19. # rubocop:enable Style/SpecialGlobalVars
  20. end
  21. def attribute_config(object, name, attribute)
  22. {
  23. object: object.to_s,
  24. name: name,
  25. display: attribute.title,
  26. data_type: data_type(attribute),
  27. data_option: @data_option,
  28. editable: !attribute.removable,
  29. active: attribute.active,
  30. screens: screens(attribute),
  31. position: attribute.position,
  32. created_by_id: 1,
  33. updated_by_id: 1,
  34. }
  35. end
  36. def screens(attribute)
  37. config = {
  38. view: {
  39. '-all-' => {
  40. shown: true,
  41. },
  42. }
  43. }
  44. return config if !attribute.visible_in_portal && attribute.required_in_portal
  45. {
  46. edit: {
  47. Customer: {
  48. shown: attribute.visible_in_portal,
  49. null: !attribute.required_in_portal,
  50. },
  51. }.merge(config)
  52. }
  53. end
  54. def initialize_data_option(attribute)
  55. @data_option = {
  56. null: !attribute.required,
  57. note: attribute.description,
  58. }
  59. end
  60. def data_type(attribute)
  61. attribute.type
  62. end
  63. end
  64. end
  65. end
  66. end