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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 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. index_by(&:id).
  31. values_at(*project_ids). # sort according to stored order
  32. compact
  33. end
  34. def bookmarked_projects
  35. Project.where(id: bookmarked_project_ids).visible.to_a
  36. end
  37. def project_used(project)
  38. return if project.blank? || project.id.blank?
  39. id_array = recently_used_project_ids
  40. id_array.reject!{ |i| i == project.id }
  41. # we dont want bookmarks in the recently used list:
  42. id_array.unshift(project.id) unless bookmark?(project)
  43. self.recently_used_project_ids = id_array[0, recent_projects_count]
  44. end
  45. def bookmark_project(project)
  46. self.recently_used_project_ids = recently_used_project_ids.reject{|id| id == project.id}
  47. self.bookmarked_project_ids = (bookmarked_project_ids << project.id)
  48. end
  49. def delete_project_bookmark(project)
  50. self.bookmarked_project_ids = bookmarked_project_ids.reject do |id|
  51. id == project.id
  52. end
  53. end
  54. def bookmark?(project)
  55. project && project.id && bookmarked_project_ids.include?(project.id)
  56. end
  57. private
  58. def bookmarked_project_ids
  59. pref_project_ids :bookmarked_project_ids
  60. end
  61. def bookmarked_project_ids=(new_ids)
  62. set_pref_project_ids :bookmarked_project_ids, new_ids
  63. end
  64. def recently_used_project_ids
  65. pref_project_ids(:recently_used_project_ids)[0,recent_projects_count]
  66. end
  67. def recently_used_project_ids=(new_ids)
  68. set_pref_project_ids :recently_used_project_ids, new_ids
  69. end
  70. def pref_project_ids(key)
  71. return [] unless @user.logged?
  72. @pref_project_ids[key] ||= (@user.pref[key] || '').split(',').map(&:to_i)
  73. end
  74. def set_pref_project_ids(key, new_values)
  75. return nil unless @user.logged?
  76. old_value = @user.pref[key]
  77. new_value = new_values.uniq.join(',')
  78. if old_value != new_value
  79. @user.pref[key] = new_value
  80. @user.pref.save
  81. end
  82. @pref_project_ids.delete key
  83. nil
  84. end
  85. end
  86. end