##// END OF EJS Templates
Removed dead code, Rails4 handles that in its attribute writer....
Jean-Philippe Lang -
r13334:8992dc9f93dc
parent child
Show More
@@ -1,129 +1,126
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 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 30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
31 31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
32 32 :author => :user,
33 33 :group => :issue,
34 34 :description => :comments
35 35
36 36 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
37 37 :author_key => :user_id,
38 38 :scope => preload(:project)
39 39
40 40 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
41 41 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
42 42 validates_length_of :comments, :maximum => 255, :allow_nil => true
43 43 validates :spent_on, :date => true
44 44 before_validation :set_project_if_nil
45 45 validate :validate_time_entry
46 46
47 47 scope :visible, lambda {|*args|
48 48 joins(:project).
49 49 where(Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args))
50 50 }
51 51 scope :on_issue, lambda {|issue|
52 52 joins(:issue).
53 53 where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
54 54 }
55 55
56 56 safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
57 57
58 58 def initialize(attributes=nil, *args)
59 59 super
60 60 if new_record? && self.activity.nil?
61 61 if default_activity = TimeEntryActivity.default
62 62 self.activity_id = default_activity.id
63 63 end
64 64 self.hours = nil if hours == 0
65 65 end
66 66 end
67 67
68 68 def safe_attributes=(attrs, user=User.current)
69 69 if attrs
70 70 attrs = super(attrs)
71 71 if issue_id_changed? && attrs[:project_id].blank? && issue && issue.project_id != project_id
72 72 if user.allowed_to?(:log_time, issue.project)
73 73 self.project_id = issue.project_id
74 74 end
75 75 end
76 76 end
77 77 attrs
78 78 end
79 79
80 80 def set_project_if_nil
81 81 self.project = issue.project if issue && project.nil?
82 82 end
83 83
84 84 def validate_time_entry
85 85 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
86 86 errors.add :project_id, :invalid if project.nil?
87 87 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
88 88 end
89 89
90 90 def hours=(h)
91 91 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
92 92 end
93 93
94 94 def hours
95 95 h = read_attribute(:hours)
96 96 if h.is_a?(Float)
97 97 h.round(2)
98 98 else
99 99 h
100 100 end
101 101 end
102 102
103 103 # tyear, tmonth, tweek assigned where setting spent_on attributes
104 104 # these attributes make time aggregations easier
105 105 def spent_on=(date)
106 106 super
107 if spent_on.is_a?(Time)
108 self.spent_on = spent_on.to_date
109 end
110 107 self.tyear = spent_on ? spent_on.year : nil
111 108 self.tmonth = spent_on ? spent_on.month : nil
112 109 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
113 110 end
114 111
115 112 # Returns true if the time entry can be edited by usr, otherwise false
116 113 def editable_by?(usr)
117 114 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
118 115 end
119 116
120 117 # Returns the custom_field_values that can be edited by the given user
121 118 def editable_custom_field_values(user=nil)
122 119 visible_custom_field_values
123 120 end
124 121
125 122 # Returns the custom fields that can be edited by the given user
126 123 def editable_custom_fields(user=nil)
127 124 editable_custom_field_values(user).map(&:custom_field).uniq
128 125 end
129 126 end
General Comments 0
You need to be logged in to leave comments. Login now