object.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. ObjectManager::Attribute.where(
  40. object_lookup_id: object,
  41. active: true,
  42. to_create: false,
  43. to_delete: false,
  44. ).reorder('position ASC, name ASC')
  45. end
  46. end
  47. def object
  48. @object ||= ObjectLookup.by_name(object_name)
  49. end
  50. def element_class
  51. @element_class ||= ObjectManager::Element.for_object(object_name)
  52. end
  53. end