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