base.rb 2.0 KB

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