object.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class ObjectManager::Object
  3. attr_reader :object_name
  4. def initialize(object_name)
  5. @object_name = object_name
  6. end
  7. =begin
  8. get user based list of used object attributes
  9. object = ObjectManager::Object.new('Ticket')
  10. attribute_list = object.attributes(user)
  11. returns:
  12. [
  13. { name: 'api_key', display: 'API Key', tag: 'input', null: true, edit: true, maxlength: 32 },
  14. { name: 'api_ip_regexp', display: 'API IP RegExp', tag: 'input', null: true, edit: true },
  15. { name: 'api_ip_max', display: 'API IP Max', tag: 'input', null: true, edit: true },
  16. ]
  17. =end
  18. def attributes(user, record = nil, data_only: true, skip_permission: false)
  19. @attributes ||= begin
  20. attribute_records.each_with_object([]) do |attribute_record, result|
  21. element = element_class.new(
  22. user: user,
  23. attribute: attribute_record,
  24. record: record,
  25. skip_permission: skip_permission,
  26. )
  27. next if !element.visible?
  28. if data_only
  29. result.push element.data
  30. else
  31. result.push element
  32. end
  33. end
  34. end
  35. end
  36. private
  37. def attribute_records
  38. @attribute_records ||= begin
  39. Auth::RequestCache.fetch_value("ObjectManager::Object/attribute_records/#{object}") do
  40. ObjectManager::Attribute.where(
  41. object_lookup_id: object,
  42. active: true,
  43. to_create: false,
  44. to_delete: false,
  45. ).reorder('position ASC, name ASC')
  46. end
  47. end
  48. end
  49. def object
  50. @object ||= ObjectLookup.by_name(object_name)
  51. end
  52. def element_class
  53. @element_class ||= ObjectManager::Element.for_object(object_name)
  54. end
  55. end