##// END OF EJS Templates
Rails3: model: replace deprecated 'before_validation' method at TimeEntry model...
Toshi MARUYAMA -
r7333:4ffca69d847f
parent child
Show More
@@ -1,111 +1,112
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 # could have used polymorphic association
19 # could have used polymorphic association
20 # project association here allows easy loading of time entries at project level with one database trip
20 # project association here allows easy loading of time entries at project level with one database trip
21 belongs_to :project
21 belongs_to :project
22 belongs_to :issue
22 belongs_to :issue
23 belongs_to :user
23 belongs_to :user
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
24 belongs_to :activity, :class_name => 'TimeEntryActivity', :foreign_key => 'activity_id'
25
25
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
26 attr_protected :project_id, :user_id, :tyear, :tmonth, :tweek
27
27
28 acts_as_customizable
28 acts_as_customizable
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
29 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
30 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
31 :author => :user,
31 :author => :user,
32 :description => :comments
32 :description => :comments
33
33
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
34 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
35 :author_key => :user_id,
35 :author_key => :user_id,
36 :find_options => {:include => :project}
36 :find_options => {:include => :project}
37
37
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
38 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
39 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
40 validates_length_of :comments, :maximum => 255, :allow_nil => true
41 before_validation :set_project_if_nil
41 validate :validate_time_entry
42 validate :validate_time_entry
42
43
43 named_scope :visible, lambda {|*args| {
44 named_scope :visible, lambda {|*args| {
44 :include => :project,
45 :include => :project,
45 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
46 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
46 }}
47 }}
47
48
48 def after_initialize
49 def after_initialize
49 if new_record? && self.activity.nil?
50 if new_record? && self.activity.nil?
50 if default_activity = TimeEntryActivity.default
51 if default_activity = TimeEntryActivity.default
51 self.activity_id = default_activity.id
52 self.activity_id = default_activity.id
52 end
53 end
53 self.hours = nil if hours == 0
54 self.hours = nil if hours == 0
54 end
55 end
55 end
56 end
56
57
57 def before_validation
58 def set_project_if_nil
58 self.project = issue.project if issue && project.nil?
59 self.project = issue.project if issue && project.nil?
59 end
60 end
60
61
61 def validate_time_entry
62 def validate_time_entry
62 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
63 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
63 errors.add :project_id, :invalid if project.nil?
64 errors.add :project_id, :invalid if project.nil?
64 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
65 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
65 end
66 end
66
67
67 def hours=(h)
68 def hours=(h)
68 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
69 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
69 end
70 end
70
71
71 # tyear, tmonth, tweek assigned where setting spent_on attributes
72 # tyear, tmonth, tweek assigned where setting spent_on attributes
72 # these attributes make time aggregations easier
73 # these attributes make time aggregations easier
73 def spent_on=(date)
74 def spent_on=(date)
74 super
75 super
75 if spent_on.is_a?(Time)
76 if spent_on.is_a?(Time)
76 self.spent_on = spent_on.to_date
77 self.spent_on = spent_on.to_date
77 end
78 end
78 self.tyear = spent_on ? spent_on.year : nil
79 self.tyear = spent_on ? spent_on.year : nil
79 self.tmonth = spent_on ? spent_on.month : nil
80 self.tmonth = spent_on ? spent_on.month : nil
80 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
81 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
81 end
82 end
82
83
83 # Returns true if the time entry can be edited by usr, otherwise false
84 # Returns true if the time entry can be edited by usr, otherwise false
84 def editable_by?(usr)
85 def editable_by?(usr)
85 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
86 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
86 end
87 end
87
88
88 # TODO: remove this method in 1.3.0
89 # TODO: remove this method in 1.3.0
89 def self.visible_by(usr)
90 def self.visible_by(usr)
90 ActiveSupport::Deprecation.warn "TimeEntry.visible_by is deprecated and will be removed in Redmine 1.3.0. Use the visible scope instead."
91 ActiveSupport::Deprecation.warn "TimeEntry.visible_by is deprecated and will be removed in Redmine 1.3.0. Use the visible scope instead."
91 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
92 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
92 yield
93 yield
93 end
94 end
94 end
95 end
95
96
96 def self.earilest_date_for_project(project=nil)
97 def self.earilest_date_for_project(project=nil)
97 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
98 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
98 if project
99 if project
99 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
100 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
100 end
101 end
101 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
102 TimeEntry.minimum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
102 end
103 end
103
104
104 def self.latest_date_for_project(project=nil)
105 def self.latest_date_for_project(project=nil)
105 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
106 finder_conditions = ARCondition.new(Project.allowed_to_condition(User.current, :view_time_entries))
106 if project
107 if project
107 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
108 finder_conditions << ["project_id IN (?)", project.hierarchy.collect(&:id)]
108 end
109 end
109 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
110 TimeEntry.maximum(:spent_on, :include => :project, :conditions => finder_conditions.conditions)
110 end
111 end
111 end
112 end
General Comments 0
You need to be logged in to leave comments. Login now