##// END OF EJS Templates
Merged r16284 (#23803)....
Jean-Philippe Lang -
r15921:0807506e18b8
parent child
Show More
@@ -1,160 +1,164
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class TimeEntry < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20 # could have used polymorphic association
21 21 # project association here allows easy loading of time entries at project level with one database trip
22 22 belongs_to :project
23 23 belongs_to :issue
24 24 belongs_to :user
25 25 belongs_to :activity, :class_name => 'TimeEntryActivity'
26 26
27 27 attr_protected :user_id, :tyear, :tmonth, :tweek
28 28
29 29 acts_as_customizable
30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 acts_as_event :title => Proc.new { |o|
31 related = o.issue if o.issue && o.issue.visible?
32 related ||= o.project
33 "#{l_hours(o.hours)} (#{related.event_title})"
34 },
31 35 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
32 36 :author => :user,
33 37 :group => :issue,
34 38 :description => :comments
35 39
36 40 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
37 41 :author_key => :user_id,
38 42 :scope => joins(:project).preload(:project)
39 43
40 44 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
41 45 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
42 46 validates_length_of :comments, :maximum => 1024, :allow_nil => true
43 47 validates :spent_on, :date => true
44 48 before_validation :set_project_if_nil
45 49 validate :validate_time_entry
46 50
47 51 scope :visible, lambda {|*args|
48 52 joins(:project).
49 53 where(TimeEntry.visible_condition(args.shift || User.current, *args))
50 54 }
51 55 scope :on_issue, lambda {|issue|
52 56 joins(:issue).
53 57 where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
54 58 }
55 59
56 60 safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
57 61
58 62 # Returns a SQL conditions string used to find all time entries visible by the specified user
59 63 def self.visible_condition(user, options={})
60 64 Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
61 65 if role.time_entries_visibility == 'all'
62 66 nil
63 67 elsif role.time_entries_visibility == 'own' && user.id && user.logged?
64 68 "#{table_name}.user_id = #{user.id}"
65 69 else
66 70 '1=0'
67 71 end
68 72 end
69 73 end
70 74
71 75 # Returns true if user or current user is allowed to view the time entry
72 76 def visible?(user=nil)
73 77 (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
74 78 if role.time_entries_visibility == 'all'
75 79 true
76 80 elsif role.time_entries_visibility == 'own'
77 81 self.user == user
78 82 else
79 83 false
80 84 end
81 85 end
82 86 end
83 87
84 88 def initialize(attributes=nil, *args)
85 89 super
86 90 if new_record? && self.activity.nil?
87 91 if default_activity = TimeEntryActivity.default
88 92 self.activity_id = default_activity.id
89 93 end
90 94 self.hours = nil if hours == 0
91 95 end
92 96 end
93 97
94 98 def safe_attributes=(attrs, user=User.current)
95 99 if attrs
96 100 attrs = super(attrs)
97 101 if issue_id_changed? && issue
98 102 if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
99 103 if attrs[:project_id].blank? && issue.project_id != project_id
100 104 self.project_id = issue.project_id
101 105 end
102 106 @invalid_issue_id = nil
103 107 else
104 108 @invalid_issue_id = issue_id
105 109 end
106 110 end
107 111 end
108 112 attrs
109 113 end
110 114
111 115 def set_project_if_nil
112 116 self.project = issue.project if issue && project.nil?
113 117 end
114 118
115 119 def validate_time_entry
116 120 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
117 121 errors.add :project_id, :invalid if project.nil?
118 122 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
119 123 errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
120 124 end
121 125
122 126 def hours=(h)
123 127 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
124 128 end
125 129
126 130 def hours
127 131 h = read_attribute(:hours)
128 132 if h.is_a?(Float)
129 133 h.round(2)
130 134 else
131 135 h
132 136 end
133 137 end
134 138
135 139 # tyear, tmonth, tweek assigned where setting spent_on attributes
136 140 # these attributes make time aggregations easier
137 141 def spent_on=(date)
138 142 super
139 143 self.tyear = spent_on ? spent_on.year : nil
140 144 self.tmonth = spent_on ? spent_on.month : nil
141 145 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
142 146 end
143 147
144 148 # Returns true if the time entry can be edited by usr, otherwise false
145 149 def editable_by?(usr)
146 150 visible?(usr) && (
147 151 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
148 152 )
149 153 end
150 154
151 155 # Returns the custom_field_values that can be edited by the given user
152 156 def editable_custom_field_values(user=nil)
153 157 visible_custom_field_values
154 158 end
155 159
156 160 # Returns the custom fields that can be edited by the given user
157 161 def editable_custom_fields(user=nil)
158 162 editable_custom_field_values(user).map(&:custom_field).uniq
159 163 end
160 164 end
General Comments 0
You need to be logged in to leave comments. Login now