comparison app/models/.svn/text-base/time_entry.rb.svn-base @ 441:cbce1fd3b1b7 redmine-1.2

Update to Redmine 1.2-stable branch (Redmine SVN rev 6000)
author Chris Cannam
date Mon, 06 Jun 2011 14:24:13 +0100
parents 07fa8a8b56a8
children
comparison
equal deleted inserted replaced
245:051f544170fe 441:cbce1fd3b1b7
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2008 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
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
42 named_scope :visible, lambda {|*args| {
43 :include => :project,
44 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_time_entries, *args)
45 }}
41 46
42 def after_initialize 47 def after_initialize
43 if new_record? && self.activity.nil? 48 if new_record? && self.activity.nil?
44 if default_activity = TimeEntryActivity.default 49 if default_activity = TimeEntryActivity.default
45 self.activity_id = default_activity.id 50 self.activity_id = default_activity.id
46 end 51 end
47 self.hours = nil if hours == 0 52 self.hours = nil if hours == 0
48 end 53 end
49 end 54 end
50 55
51 def before_validation 56 def before_validation
52 self.project = issue.project if issue && project.nil? 57 self.project = issue.project if issue && project.nil?
53 end 58 end
54 59
55 def validate 60 def validate
56 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000) 61 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
57 errors.add :project_id, :invalid if project.nil? 62 errors.add :project_id, :invalid if project.nil?
58 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) 63 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project)
59 end 64 end
60 65
61 def hours=(h) 66 def hours=(h)
62 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h) 67 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
63 end 68 end
64 69
65 # tyear, tmonth, tweek assigned where setting spent_on attributes 70 # tyear, tmonth, tweek assigned where setting spent_on attributes
66 # these attributes make time aggregations easier 71 # these attributes make time aggregations easier
67 def spent_on=(date) 72 def spent_on=(date)
68 super 73 super
69 if spent_on.is_a?(Time) 74 if spent_on.is_a?(Time)
71 end 76 end
72 self.tyear = spent_on ? spent_on.year : nil 77 self.tyear = spent_on ? spent_on.year : nil
73 self.tmonth = spent_on ? spent_on.month : nil 78 self.tmonth = spent_on ? spent_on.month : nil
74 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil 79 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
75 end 80 end
76 81
77 # Returns true if the time entry can be edited by usr, otherwise false 82 # Returns true if the time entry can be edited by usr, otherwise false
78 def editable_by?(usr) 83 def editable_by?(usr)
79 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project) 84 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
80 end 85 end
81 86
87 # TODO: remove this method in 1.3.0
82 def self.visible_by(usr) 88 def self.visible_by(usr)
89 ActiveSupport::Deprecation.warn "TimeEntry.visible_by is deprecated and will be removed in Redmine 1.3.0. Use the visible scope instead."
83 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do 90 with_scope(:find => { :conditions => Project.allowed_to_condition(usr, :view_time_entries) }) do
84 yield 91 yield
85 end 92 end
86 end 93 end
87 94