categories_controller.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class KnowledgeBase::CategoriesController < KnowledgeBase::BaseController
  3. before_action :load_knowledge_base, only: %i[reorder_root_categories reorder_categories reorder_answers]
  4. def reorder_root_categories
  5. reorder_records @knowledge_base.categories.root, params[:ordered_ids], KnowledgeBase::Category
  6. end
  7. def reorder_categories
  8. reorder_records @category.children, params[:ordered_ids], KnowledgeBase::Category
  9. end
  10. def reorder_answers
  11. reorder_records @category.answers, params[:ordered_ids], KnowledgeBase::Answer
  12. end
  13. private
  14. def reorder_records(collection, ids, klass)
  15. # Check if ids for models in collection are present
  16. all_ids_present = collection.map(&:id).sort == ids.sort
  17. raise Exceptions::UnprocessableEntity, __('Provide position of all items in scope') if !all_ids_present
  18. klass.acts_as_list_no_update do
  19. ids.each_with_index do |id, index|
  20. collection
  21. .find { |item| item.id == id }
  22. .update!(position: index)
  23. end
  24. end
  25. assets = ApplicationModel::CanAssets.reduce(collection, {})
  26. render json: assets
  27. end
  28. def load_knowledge_base
  29. @knowledge_base = KnowledgeBase.find params[:knowledge_base_id]
  30. @category = @knowledge_base.categories.find params[:id] if params.key? :id
  31. end
  32. end