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.3KB

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