Class: TimeEntry
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- TimeEntry
- Includes:
- Redmine::SafeAttributes
- Defined in:
- app/models/time_entry.rb
Overview
Redmine - project management software Copyright © 2006- 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.
Class Method Summary collapse
-
.visible_condition(user, options = {}) ⇒ Object
Returns a SQL conditions string used to find all time entries visible by the specified user.
Instance Method Summary collapse
- #assignable_users ⇒ Object
-
#editable_by?(usr) ⇒ Boolean
Returns true if the time entry can be edited by usr, otherwise false.
-
#editable_custom_field_values(user = nil) ⇒ Object
Returns the custom_field_values that can be edited by the given user.
-
#editable_custom_fields(user = nil) ⇒ Object
Returns the custom fields that can be edited by the given user.
- #hours ⇒ Object
- #hours=(h) ⇒ Object
-
#initialize(attributes = nil, *args) ⇒ TimeEntry
constructor
A new instance of TimeEntry.
- #safe_attributes=(attrs, user = User.current) ⇒ Object
- #set_author_if_nil ⇒ Object
- #set_project_if_nil ⇒ Object
-
#spent_on=(date) ⇒ Object
tyear, tmonth, tweek assigned where setting spent_on attributes these attributes make time aggregations easier.
- #validate_time_entry ⇒ Object
-
#visible?(user = nil) ⇒ Boolean
Returns true if user or current user is allowed to view the time entry.
- #visible_custom_field_values(user = nil) ⇒ Object
Methods included from Redmine::SafeAttributes
#delete_unsafe_attributes, included, #safe_attribute?, #safe_attribute_names
Methods inherited from ApplicationRecord
Constructor Details
#initialize(attributes = nil, *args) ⇒ TimeEntry
Returns a new instance of TimeEntry.
107 108 109 110 111 112 113 |
# File 'app/models/time_entry.rb', line 107 def initialize(attributes=nil, *args) super if new_record? && self.activity.nil? self.activity_id = TimeEntryActivity.default_activity_id(User.current, self.project) self.hours = nil if hours == 0 end end |
Class Method Details
.visible_condition(user, options = {}) ⇒ Object
Returns a SQL conditions string used to find all time entries visible by the specified user
82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/models/time_entry.rb', line 82 def self.visible_condition(user, ={}) Project.allowed_to_condition(user, :view_time_entries, ) do |role, user| if role.time_entries_visibility == 'all' nil elsif role.time_entries_visibility == 'own' && user.id && user.logged? "#{table_name}.user_id = #{user.id}" else '1=0' end end end |
Instance Method Details
#assignable_users ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'app/models/time_entry.rb', line 240 def assignable_users users = [] if project users = project.members.active.preload(:user) users = users.map(&:user).select{|u| u.allowed_to?(:log_time, project)} end users << User.current if User.current.logged? && !users.include?(User.current) users end |
#editable_by?(usr) ⇒ Boolean
Returns true if the time entry can be edited by usr, otherwise false
217 218 219 220 221 |
# File 'app/models/time_entry.rb', line 217 def editable_by?(usr) visible?(usr) && ( (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) ) end |
#editable_custom_field_values(user = nil) ⇒ Object
Returns the custom_field_values that can be edited by the given user
224 225 226 |
# File 'app/models/time_entry.rb', line 224 def editable_custom_field_values(user=nil) visible_custom_field_values(user) end |
#editable_custom_fields(user = nil) ⇒ Object
Returns the custom fields that can be edited by the given user
229 230 231 |
# File 'app/models/time_entry.rb', line 229 def editable_custom_fields(user=nil) editable_custom_field_values(user).map(&:custom_field).uniq end |
#hours ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/models/time_entry.rb', line 191 def hours h = read_attribute(:hours) if h.is_a?(Float) # Convert the float value to a rational with a denominator of 60 to # avoid floating point errors. # # Examples: # 0.38333333333333336 => (23/60) # 23m # 0.9913888888888889 => (59/60) # 59m 29s is rounded to 59m # 0.9919444444444444 => (1/1) # 59m 30s is rounded to 60m (h * 60).round / 60r else h end end |
#hours=(h) ⇒ Object
187 188 189 |
# File 'app/models/time_entry.rb', line 187 def hours=(h) write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) end |
#safe_attributes=(attrs, user = User.current) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'app/models/time_entry.rb', line 115 def safe_attributes=(attrs, user=User.current) if attrs attrs = super(attrs) if issue_id_changed? && issue if issue.visible?(user) && user.allowed_to?(:log_time, issue.project) if attrs[:project_id].blank? && issue.project_id != project_id self.project_id = issue.project_id end @invalid_issue_id = nil elsif user.allowed_to?(:log_time, issue.project) && issue.assigned_to_id_changed? && issue.previous_assignee == User.current current_assignee = issue.assigned_to issue.assigned_to = issue.previous_assignee unless issue.visible?(user) @invalid_issue_id = issue_id end issue.assigned_to = current_assignee else @invalid_issue_id = issue_id end end if user_id_changed? && user_id != && !user.allowed_to?(:log_time_for_other_users, project) @invalid_user_id = user_id else @invalid_user_id = nil end # Delete assigned custom fields not visible by the user editable_custom_field_ids = editable_custom_field_values(user).map {|v| v.custom_field_id.to_s} self.custom_field_values.delete_if do |c| !editable_custom_field_ids.include?(c.custom_field.id.to_s) end end attrs end |
#set_author_if_nil ⇒ Object
155 156 157 |
# File 'app/models/time_entry.rb', line 155 def self. = User.current if .nil? end |
#set_project_if_nil ⇒ Object
151 152 153 |
# File 'app/models/time_entry.rb', line 151 def set_project_if_nil self.project = issue.project if issue && project.nil? end |
#spent_on=(date) ⇒ Object
tyear, tmonth, tweek assigned where setting spent_on attributes these attributes make time aggregations easier
209 210 211 212 213 214 |
# File 'app/models/time_entry.rb', line 209 def spent_on=(date) super self.tyear = spent_on ? spent_on.year : nil self.tmonth = spent_on ? spent_on.month : nil self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil end |
#validate_time_entry ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'app/models/time_entry.rb', line 159 def validate_time_entry if hours errors.add :hours, :invalid if hours < 0 errors.add :hours, :invalid if hours == 0.0 && hours_changed? && !Setting.timelog_accept_0_hours? max_hours = Setting.timelog_max_hours_per_day.to_f if hours_changed? && max_hours > 0.0 logged_hours = other_hours_with_same_user_and_day if logged_hours + hours > max_hours errors.add( :base, I18n.t(:error_exceeds_maximum_hours_per_day, :logged_hours => format_hours(logged_hours), :max_hours => format_hours(max_hours))) end end end errors.add :project_id, :invalid if project.nil? if @invalid_user_id || (user_id_changed? && user_id != && !self.assignable_users.map(&:id).include?(user_id)) errors.add :user_id, :invalid end errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity) if spent_on && spent_on_changed? && user errors.add :base, I18n.t(:error_spent_on_future_date) if !Setting.timelog_accept_future_dates? && (spent_on > user.today) end end |
#visible?(user = nil) ⇒ Boolean
Returns true if user or current user is allowed to view the time entry
95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/time_entry.rb', line 95 def visible?(user=nil) (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user| if role.time_entries_visibility == 'all' true elsif role.time_entries_visibility == 'own' self.user == user else false end end end |