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