You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_jump_box.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module Redmine
  19. class ProjectJumpBox
  20. def initialize(user)
  21. @user = user
  22. @pref_project_ids = {}
  23. end
  24. def recent_projects_count
  25. @user.pref.recently_used_projects
  26. end
  27. def recently_used_projects
  28. project_ids = recently_used_project_ids
  29. Project.where(id: project_ids).
  30. visible.
  31. index_by(&:id).
  32. values_at(*project_ids). # sort according to stored order
  33. compact
  34. end
  35. def bookmarked_projects
  36. Project.where(id: bookmarked_project_ids).visible.to_a
  37. end
  38. def project_used(project)
  39. return if project.blank? || project.id.blank?
  40. id_array = recently_used_project_ids
  41. id_array.reject!{|i| i == project.id}
  42. # we dont want bookmarks in the recently used list:
  43. id_array.unshift(project.id) unless bookmark?(project)
  44. self.recently_used_project_ids = id_array[0, recent_projects_count]
  45. end
  46. def bookmark_project(project)
  47. self.recently_used_project_ids = recently_used_project_ids.reject{|id| id == project.id}
  48. self.bookmarked_project_ids = (bookmarked_project_ids << project.id)
  49. end
  50. def delete_project_bookmark(project)
  51. self.bookmarked_project_ids = bookmarked_project_ids.reject do |id|
  52. id == project.id
  53. end
  54. end
  55. def bookmark?(project)
  56. project && project.id && bookmarked_project_ids.include?(project.id)
  57. end
  58. private
  59. def bookmarked_project_ids
  60. pref_project_ids :bookmarked_project_ids
  61. end
  62. def bookmarked_project_ids=(new_ids)
  63. set_pref_project_ids :bookmarked_project_ids, new_ids
  64. end
  65. def recently_used_project_ids
  66. pref_project_ids(:recently_used_project_ids)[0,recent_projects_count]
  67. end
  68. def recently_used_project_ids=(new_ids)
  69. set_pref_project_ids :recently_used_project_ids, new_ids
  70. end
  71. def pref_project_ids(key)
  72. return [] unless @user.logged?
  73. @pref_project_ids[key] ||= (@user.pref[key] || '').split(',').map(&:to_i)
  74. end
  75. def set_pref_project_ids(key, new_values)
  76. return nil unless @user.logged?
  77. old_value = @user.pref[key]
  78. new_value = new_values.uniq.join(',')
  79. if old_value != new_value
  80. @user.pref[key] = new_value
  81. @user.pref.save
  82. end
  83. @pref_project_ids.delete key
  84. nil
  85. end
  86. end
  87. end