relation.rb 862 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class FormUpdater::Relation
  3. attr_reader :context, :current_user, :data, :filter_ids
  4. def initialize(context:, current_user:, data: {}, filter_ids: [])
  5. @context = context
  6. @current_user = current_user
  7. @data = data
  8. @filter_ids = filter_ids
  9. end
  10. def options
  11. items.map do |item|
  12. { value: item.id, label: display_name(item) }
  13. end
  14. end
  15. private
  16. def order
  17. { id: :asc }
  18. end
  19. def display_name(item)
  20. item.name
  21. end
  22. def relation_type
  23. raise NotImplementedError
  24. end
  25. def items
  26. @items ||= begin
  27. if filter_ids
  28. relation_type.where(id: filter_ids).reorder(order)
  29. else
  30. # Currently the default is an empty array, later we need some good solution for the admin area.
  31. []
  32. end
  33. end
  34. end
  35. end