relation.rb 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2023 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. options = []
  12. items.each do |item|
  13. options.push({ value: item.id, label: display_name(item) })
  14. end
  15. options
  16. end
  17. private
  18. def order
  19. { id: :asc }
  20. end
  21. def display_name(item)
  22. item.name
  23. end
  24. def relation_type
  25. raise NotImplementedError
  26. end
  27. def items
  28. @items ||= begin
  29. if filter_ids
  30. relation_type.where(id: filter_ids).order(order)
  31. else
  32. # Currently the default is an empty array, later we need some good solution for the admin area.
  33. []
  34. end
  35. end
  36. end
  37. end